Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • choosing Top 10

    I have a list of insurance data. I want to identify and keep top 10 used insurance package. example: "LB0002 : PTCA with Baremetal Stent" should be assigned rank 1.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str122 package_procedure_name float(count_p num)
    "FP0036 : URSL (First Otr)"                      10  1
    "FP0036 : URSL (First Otr)"                      10  4
    "FP0036 : URSL (First Otr)"                      10  2
    "FP0036 : URSL (First Otr)"                      10 10
    "FP0036 : URSL (First Otr)"                      10  5
    "FP0036 : URSL (First Otr)"                      10  3
    "FP0036 : URSL (First Otr)"                      10  7
    "FP0036 : URSL (First Otr)"                      10  6
    "FP0036 : URSL (First Otr)"                      10  9
    "FP0036 : URSL (First Otr)"                      10  8
    "LB0002 : PTCA with Baremetal Stent"             52 23
    "LB0002 : PTCA with Baremetal Stent"             52 11
    "LB0002 : PTCA with Baremetal Stent"             52 26
    "LB0002 : PTCA with Baremetal Stent"             52 35
    "LB0002 : PTCA with Baremetal Stent"             52  2
    "LB0002 : PTCA with Baremetal Stent"             52 14
    "LB0002 : PTCA with Baremetal Stent"             52 28
    "LB0002 : PTCA with Baremetal Stent"             52  7
    "LB0002 : PTCA with Baremetal Stent"             52 13
    "LB0002 : PTCA with Baremetal Stent"             52 37
    "LB0002 : PTCA with Baremetal Stent"             52 22
    "LB0002 : PTCA with Baremetal Stent"             52 43
    "LB0002 : PTCA with Baremetal Stent"             52 34
    "LB0002 : PTCA with Baremetal Stent"             52 10
    "LB0002 : PTCA with Baremetal Stent"             52 30
    "LB0002 : PTCA with Baremetal Stent"             52 36
    "LB0002 : PTCA with Baremetal Stent"             52 40
    "LB0002 : PTCA with Baremetal Stent"             52 41
    "LB0002 : PTCA with Baremetal Stent"             52 24
    "LB0002 : PTCA with Baremetal Stent"             52  4
    "LB0002 : PTCA with Baremetal Stent"             52 49
    "LB0002 : PTCA with Baremetal Stent"             52  1
    "LB0002 : PTCA with Baremetal Stent"             52 29
    "LB0002 : PTCA with Baremetal Stent"             52 18
    "LB0002 : PTCA with Baremetal Stent"             52  9
    "LB0002 : PTCA with Baremetal Stent"             52 16
    "LB0002 : PTCA with Baremetal Stent"             52 17
    "LB0002 : PTCA with Baremetal Stent"             52 47
    "LB0002 : PTCA with Baremetal Stent"             52 15
    "LB0002 : PTCA with Baremetal Stent"             52  6
    "LB0002 : PTCA with Baremetal Stent"             52 38
    "LB0002 : PTCA with Baremetal Stent"             52 45
    "LB0002 : PTCA with Baremetal Stent"             52 27
    "LB0002 : PTCA with Baremetal Stent"             52 46
    "LB0002 : PTCA with Baremetal Stent"             52  5
    "LB0002 : PTCA with Baremetal Stent"             52 48
    "LB0002 : PTCA with Baremetal Stent"             52 33
    "LB0002 : PTCA with Baremetal Stent"             52 12
    "LB0002 : PTCA with Baremetal Stent"             52  3
    "LB0002 : PTCA with Baremetal Stent"             52 21
    "LB0002 : PTCA with Baremetal Stent"             52 52
    "LB0002 : PTCA with Baremetal Stent"             52 39
    "LB0002 : PTCA with Baremetal Stent"             52 50
    "LB0002 : PTCA with Baremetal Stent"             52 51
    "LB0002 : PTCA with Baremetal Stent"             52 20
    "LB0002 : PTCA with Baremetal Stent"             52 31
    "LB0002 : PTCA with Baremetal Stent"             52 19
    "LB0002 : PTCA with Baremetal Stent"             52 42
    "LB0002 : PTCA with Baremetal Stent"             52 44
    "LB0002 : PTCA with Baremetal Stent"             52  8
    "LB0002 : PTCA with Baremetal Stent"             52 32
    "LB0002 : PTCA with Baremetal Stent"             52 25
    "LB0004 : ASD Device Closure"                     5  2
    "LB0004 : ASD Device Closure"                     5  4
    "LB0004 : ASD Device Closure"                     5  1
    "LB0004 : ASD Device Closure"                     5  5
    "LB0004 : ASD Device Closure"                     5  3
    "LB0020 : Coronary Bypass Surgery"               25  4
    "LB0020 : Coronary Bypass Surgery"               25 20
    "LB0020 : Coronary Bypass Surgery"               25  1
    "LB0020 : Coronary Bypass Surgery"               25 15
    "LB0020 : Coronary Bypass Surgery"               25 21
    "LB0020 : Coronary Bypass Surgery"               25 12
    "LB0020 : Coronary Bypass Surgery"               25  7
    "LB0020 : Coronary Bypass Surgery"               25  9
    "LB0020 : Coronary Bypass Surgery"               25 11
    "LB0020 : Coronary Bypass Surgery"               25  5
    "LB0020 : Coronary Bypass Surgery"               25 14
    "LB0020 : Coronary Bypass Surgery"               25 19
    "LB0020 : Coronary Bypass Surgery"               25 18
    "LB0020 : Coronary Bypass Surgery"               25 22
    "LB0020 : Coronary Bypass Surgery"               25 25
    "LB0020 : Coronary Bypass Surgery"               25  2
    "LB0020 : Coronary Bypass Surgery"               25 24
    "LB0020 : Coronary Bypass Surgery"               25 17
    "LB0020 : Coronary Bypass Surgery"               25  6
    "LB0020 : Coronary Bypass Surgery"               25  3
    "LB0020 : Coronary Bypass Surgery"               25 13
    "LB0020 : Coronary Bypass Surgery"               25 16
    "LB0020 : Coronary Bypass Surgery"               25  8
    "LB0020 : Coronary Bypass Surgery"               25 10
    "LB0020 : Coronary Bypass Surgery"               25 23
    "LB0026 : Mitral Valve Replacement (With Valve)" 12  2
    "LB0026 : Mitral Valve Replacement (With Valve)" 12 10
    "LB0026 : Mitral Valve Replacement (With Valve)" 12  8
    "LB0026 : Mitral Valve Replacement (With Valve)" 12  1
    "LB0026 : Mitral Valve Replacement (With Valve)" 12 11
    "LB0026 : Mitral Valve Replacement (With Valve)" 12 12
    "LB0026 : Mitral Valve Replacement (With Valve)" 12  5
    "LB0026 : Mitral Valve Replacement (With Valve)" 12  7
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 2000 observations
    Use the count() option to list more

    Last edited by snigdha kashyap; 28 May 2024, 10:00.

  • #2

    Code:
    gsort -count_p package
    
    gen wanted = sum(count_p != count_p[_n-1] | package != package[_n-1])
    This is arbitrary to the extent that tied frequencies map to different ranks.

    See also https://journals.sagepub.com/doi/pdf...6867X221106436

    Comment


    • #3
      Thanks a lot, Nick! It works. Can you also please explain the code?

      Comment


      • #4
        Clever. Using sort or egen/rank makes a mess of it.

        gsort is sorting by count_p (with the - indicating largest to smallest) and package (since packages may have same count_p).

        The gen command creates a running index of the sorted data, including package so you get a different running index across packages, even when they have the same group_p.

        I think leaving package out of the gen command would allow for ties in group_p to have the same "rank", but that may or may not be what you want.

        Comment


        • #5
          In several senses the link in #2 already explains the principles. These linked references aren't just decorative; they were written to be read!

          Regardless, here is another way to do it. The explanation will work around the problem that George Ford alludes to, which bites with a naive application of ranking.

          Let's rank mpg in groups in the auto data as a not too silly example. There are 21 distinct values to which we will assign ranks 1 to 21.

          To ensure that we do that we tag each value exactly once; rank the tagged values; and then spread the ranks as desired.

          To get ranks that go downwards, just apply rank to the negation, so call rank(-mpg).

          Code:
          . sysuse auto, clear
          (1978 automobile data)
          
          . tab mpg
          
              Mileage |
                (mpg) |      Freq.     Percent        Cum.
          ------------+-----------------------------------
                   12 |          2        2.70        2.70
                   14 |          6        8.11       10.81
                   15 |          2        2.70       13.51
                   16 |          4        5.41       18.92
                   17 |          4        5.41       24.32
                   18 |          9       12.16       36.49
                   19 |          8       10.81       47.30
                   20 |          3        4.05       51.35
                   21 |          5        6.76       58.11
                   22 |          5        6.76       64.86
                   23 |          3        4.05       68.92
                   24 |          4        5.41       74.32
                   25 |          5        6.76       81.08
                   26 |          3        4.05       85.14
                   28 |          3        4.05       89.19
                   29 |          1        1.35       90.54
                   30 |          2        2.70       93.24
                   31 |          1        1.35       94.59
                   34 |          1        1.35       95.95
                   35 |          2        2.70       98.65
                   41 |          1        1.35      100.00
          ------------+-----------------------------------
                Total |         74      100.00
          
          . ret li
          
          scalars:
                            r(N) =  74
                            r(r) =  21
          
          . egen tag = tag(mpg)
          
          . egen rank = rank(mpg) if tag
          (53 missing values generated)
          
          . su rank
          
              Variable |        Obs        Mean    Std. dev.       Min        Max
          -------------+---------------------------------------------------------
                  rank |         21          11    6.204837          1         21
          
          . bysort mpg (tag) : replace rank = rank[_N]
          (53 real changes made)
          
          . tab rank
          
              rank of |
              (mpg)   |      Freq.     Percent        Cum.
          ------------+-----------------------------------
                    1 |          2        2.70        2.70
                    2 |          6        8.11       10.81
                    3 |          2        2.70       13.51
                    4 |          4        5.41       18.92
                    5 |          4        5.41       24.32
                    6 |          9       12.16       36.49
                    7 |          8       10.81       47.30
                    8 |          3        4.05       51.35
                    9 |          5        6.76       58.11
                   10 |          5        6.76       64.86
                   11 |          3        4.05       68.92
                   12 |          4        5.41       74.32
                   13 |          5        6.76       81.08
                   14 |          3        4.05       85.14
                   15 |          3        4.05       89.19
                   16 |          1        1.35       90.54
                   17 |          2        2.70       93.24
                   18 |          1        1.35       94.59
                   19 |          1        1.35       95.95
                   20 |          2        2.70       98.65
                   21 |          1        1.35      100.00
          ------------+-----------------------------------
                Total |         74      100.00
          .
          Note that the tag variable has values 0 and 1 (only); so when we sort on that tag variable within groups, the observation with tag 1 is last in its group.

          Comment


          • #6
            For yet another approach, see myaxis from the Stata Journal as written up within https://journals.sagepub.com/doi/pdf...6867X211045582

            Here is a worked example:

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input str122 package_procedure_name float(count_p num)
            "FP0036 : URSL (First Otr)"                      10  1
            "FP0036 : URSL (First Otr)"                      10  4
            "FP0036 : URSL (First Otr)"                      10  2
            "FP0036 : URSL (First Otr)"                      10 10
            "FP0036 : URSL (First Otr)"                      10  5
            "FP0036 : URSL (First Otr)"                      10  3
            "FP0036 : URSL (First Otr)"                      10  7
            "FP0036 : URSL (First Otr)"                      10  6
            "FP0036 : URSL (First Otr)"                      10  9
            "FP0036 : URSL (First Otr)"                      10  8
            "LB0002 : PTCA with Baremetal Stent"             52 23
            "LB0002 : PTCA with Baremetal Stent"             52 11
            "LB0002 : PTCA with Baremetal Stent"             52 26
            "LB0002 : PTCA with Baremetal Stent"             52 35
            "LB0002 : PTCA with Baremetal Stent"             52  2
            "LB0002 : PTCA with Baremetal Stent"             52 14
            "LB0002 : PTCA with Baremetal Stent"             52 28
            "LB0002 : PTCA with Baremetal Stent"             52  7
            "LB0002 : PTCA with Baremetal Stent"             52 13
            "LB0002 : PTCA with Baremetal Stent"             52 37
            "LB0002 : PTCA with Baremetal Stent"             52 22
            "LB0002 : PTCA with Baremetal Stent"             52 43
            "LB0002 : PTCA with Baremetal Stent"             52 34
            "LB0002 : PTCA with Baremetal Stent"             52 10
            "LB0002 : PTCA with Baremetal Stent"             52 30
            "LB0002 : PTCA with Baremetal Stent"             52 36
            "LB0002 : PTCA with Baremetal Stent"             52 40
            "LB0002 : PTCA with Baremetal Stent"             52 41
            "LB0002 : PTCA with Baremetal Stent"             52 24
            "LB0002 : PTCA with Baremetal Stent"             52  4
            "LB0002 : PTCA with Baremetal Stent"             52 49
            "LB0002 : PTCA with Baremetal Stent"             52  1
            "LB0002 : PTCA with Baremetal Stent"             52 29
            "LB0002 : PTCA with Baremetal Stent"             52 18
            "LB0002 : PTCA with Baremetal Stent"             52  9
            "LB0002 : PTCA with Baremetal Stent"             52 16
            "LB0002 : PTCA with Baremetal Stent"             52 17
            "LB0002 : PTCA with Baremetal Stent"             52 47
            "LB0002 : PTCA with Baremetal Stent"             52 15
            "LB0002 : PTCA with Baremetal Stent"             52  6
            "LB0002 : PTCA with Baremetal Stent"             52 38
            "LB0002 : PTCA with Baremetal Stent"             52 45
            "LB0002 : PTCA with Baremetal Stent"             52 27
            "LB0002 : PTCA with Baremetal Stent"             52 46
            "LB0002 : PTCA with Baremetal Stent"             52  5
            "LB0002 : PTCA with Baremetal Stent"             52 48
            "LB0002 : PTCA with Baremetal Stent"             52 33
            "LB0002 : PTCA with Baremetal Stent"             52 12
            "LB0002 : PTCA with Baremetal Stent"             52  3
            "LB0002 : PTCA with Baremetal Stent"             52 21
            "LB0002 : PTCA with Baremetal Stent"             52 52
            "LB0002 : PTCA with Baremetal Stent"             52 39
            "LB0002 : PTCA with Baremetal Stent"             52 50
            "LB0002 : PTCA with Baremetal Stent"             52 51
            "LB0002 : PTCA with Baremetal Stent"             52 20
            "LB0002 : PTCA with Baremetal Stent"             52 31
            "LB0002 : PTCA with Baremetal Stent"             52 19
            "LB0002 : PTCA with Baremetal Stent"             52 42
            "LB0002 : PTCA with Baremetal Stent"             52 44
            "LB0002 : PTCA with Baremetal Stent"             52  8
            "LB0002 : PTCA with Baremetal Stent"             52 32
            "LB0002 : PTCA with Baremetal Stent"             52 25
            "LB0004 : ASD Device Closure"                     5  2
            "LB0004 : ASD Device Closure"                     5  4
            "LB0004 : ASD Device Closure"                     5  1
            "LB0004 : ASD Device Closure"                     5  5
            "LB0004 : ASD Device Closure"                     5  3
            "LB0020 : Coronary Bypass Surgery"               25  4
            "LB0020 : Coronary Bypass Surgery"               25 20
            "LB0020 : Coronary Bypass Surgery"               25  1
            "LB0020 : Coronary Bypass Surgery"               25 15
            "LB0020 : Coronary Bypass Surgery"               25 21
            "LB0020 : Coronary Bypass Surgery"               25 12
            "LB0020 : Coronary Bypass Surgery"               25  7
            "LB0020 : Coronary Bypass Surgery"               25  9
            "LB0020 : Coronary Bypass Surgery"               25 11
            "LB0020 : Coronary Bypass Surgery"               25  5
            "LB0020 : Coronary Bypass Surgery"               25 14
            "LB0020 : Coronary Bypass Surgery"               25 19
            "LB0020 : Coronary Bypass Surgery"               25 18
            "LB0020 : Coronary Bypass Surgery"               25 22
            "LB0020 : Coronary Bypass Surgery"               25 25
            "LB0020 : Coronary Bypass Surgery"               25  2
            "LB0020 : Coronary Bypass Surgery"               25 24
            "LB0020 : Coronary Bypass Surgery"               25 17
            "LB0020 : Coronary Bypass Surgery"               25  6
            "LB0020 : Coronary Bypass Surgery"               25  3
            "LB0020 : Coronary Bypass Surgery"               25 13
            "LB0020 : Coronary Bypass Surgery"               25 16
            "LB0020 : Coronary Bypass Surgery"               25  8
            "LB0020 : Coronary Bypass Surgery"               25 10
            "LB0020 : Coronary Bypass Surgery"               25 23
            end
            
            myaxis rank=package_procedure_name, sort(mean count_p) descending 
            
            tab rank 
            
            tab rank, nolabel
            Code:
            . tab rank 
            
                        package_procedure_name |      Freq.     Percent        Cum.
            -----------------------------------+-----------------------------------
            LB0002 : PTCA with Baremetal Stent |         52       56.52       56.52
              LB0020 : Coronary Bypass Surgery |         25       27.17       83.70
                     FP0036 : URSL (First Otr) |         10       10.87       94.57
                   LB0004 : ASD Device Closure |          5        5.43      100.00
            -----------------------------------+-----------------------------------
                                         Total |         92      100.00
            
            . 
            . tab rank, nolabel 
            
            package_pro |
            cedure_name |      Freq.     Percent        Cum.
            ------------+-----------------------------------
                      1 |         52       56.52       56.52
                      2 |         25       27.17       83.70
                      3 |         10       10.87       94.57
                      4 |          5        5.43      100.00
            ------------+-----------------------------------
                  Total |         92      100.00

            Comment

            Working...
            X