Announcement

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

  • Performing Cox regression followed by Harrells C index. I want to perform this using Statsby but I am having problems.

    Hello,

    I am using Stata 18

    I have a dataset (example with 2 users' data below, although I have 115 users in the original dataset).

    Many users (user_id), each have evaluated 203 patients (patient_id) and provided a score (uip). The variable uip, represents the probability (0-100) of having a certain type of cancer. The idea is to then perform survival analysis (stcox) with uip as the single variable.


    I can run this:


    Code:
    tempfile results
    
    statsby _b r(harrell_c) se = _se[uip], by(user_id) saving(`results'): stcox uip
    
    use `results',clear
    And then do this (to get the Hazards Ratios, p values and 95% confidence intervals):

    Code:
    gen hr = exp(b)
    local z95 = -invnorm(0.025)
    gen ci_upper = exp(b + `z95'*se)
    gen ci_lower = exp(b - `z95'*se)
    The problem is, that I want to also compute Harrells C index (estat concordance) after each Cox regression but the above is not working; I am not getting the C index value. I then tried this:

    Code:
    program define harrell, rclass
    stcox uip_pioped_before
    estat concordance
    return scalar harrell_c = r(C)
    exit
    end
    
    tempfile results
    
    statsby _b r(harrell_c), by(user_id) saving(`results'): harrell
    
    use `results',clear
    But I simply have the same values for every user - its as if the results for each user are getting overwritten.

    Any help with this please?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long(uip user_id patient_id) byte(_st _d) int _t byte _t0
     75 21   1 1 1 1557 0
     80 21   2 1 1 1121 0
     20 21   3 1 1 1760 0
     80 21   4 1 0  417 0
      5 21   5 1 1 1482 0
     20 21   6 1 1  390 0
     80 21   7 1 1  696 0
    100 21   8 1 1  122 0
     60 21   9 1 1  378 0
      0 21  10 1 0 2235 0
     80 21  11 1 1 4181 0
      0 21  12 1 1 2131 0
     20 21  13 1 1  381 0
     20 21  14 1 1  802 0
     15 21  15 1 1 2513 0
     20 21  16 1 1  511 0
      5 21  17 1 1  298 0
     10 21  18 1 0 2359 0
      5 21  19 1 1  524 0
     40 21  20 1 1 1189 0
     15 21  21 1 0 2291 0
     20 21  22 1 1 1100 0
      0 21  23 1 0 2235 0
      0 21  24 1 0 1844 0
      5 21  25 1 1 1051 0
     15 21  26 1 1 1078 0
    100 21  27 1 1   41 0
     20 21  28 1 0  588 0
      0 21  29 1 1  873 0
     20 21  30 1 1 1303 0
     65 21  31 1 1  952 0
    100 21  32 1 1 1790 0
      0 21  33 1 0 2327 0
     80 21  34 1 1 1358 0
     30 21  35 1 1  460 0
      0 21  36 1 1  909 0
     90 21  37 1 1  205 0
      0 21  38 1 0 2184 0
     90 21  39 1 1  104 0
      0 21  40 1 0 1923 0
     90 21  41 1 1 1688 0
      0 21  42 1 1 1659 0
     20 21  43 1 1  261 0
      0 21  44 1 1 2630 0
     20 21  45 1 1 1488 0
     10 21  46 1 1  148 0
     10 21  47 1 1   46 0
     10 21  48 1 1  527 0
      0 21  49 1 1 2276 0
      0 21  50 1 0 2567 0
     10 21  51 1 1 1442 0
     10 21  52 1 1 1140 0
     20 21  53 1 1  419 0
     10 21  54 1 1 2079 0
      0 21  55 1 1 2222 0
     20 21  56 1 1  746 0
     10 21  57 1 1 1002 0
     20 21  58 1 1 1833 0
     10 21  59 1 0 1240 0
     20 21  60 1 1  242 0
     40 21  61 1 1  643 0
     10 21  62 1 1  295 0
     70 21  63 1 1  325 0
      0 21  64 1 1  925 0
      0 21  65 1 1 2433 0
      0 21  66 1 1 1464 0
      0 21  67 1 0 2024 0
     10 21  68 1 0 2296 0
      0 21  69 1 1 2029 0
     80 21  70 1 0 2702 0
      0 21  71 1 0 2816 0
      0 21  72 1 1 1093 0
     10 21  73 1 1 1756 0
     90 21  74 1 1 1264 0
      0 21  75 1 1 1745 0
     20 21  76 1 1 2418 0
     20 21  77 1 1  596 0
      5 21  78 1 1 2461 0
     10 21  79 1 1  250 0
     20 21  80 1 0 2463 0
     50 21  81 1 1 1392 0
      0 21  82 1 1 1014 0
      0 21  83 1 0 1000 0
      0 21  84 1 1 2715 0
      0 21  85 1 0 2208 0
      0 21  86 1 1 1471 0
      0 21  87 1 1 1779 0
    100 21  88 1 1 1245 0
     80 21  89 1 0 3180 0
      0 21  90 1 1 1772 0
     90 21  91 1 0 2377 0
     90 21  92 1 0 2262 0
     10 21  93 1 1 1867 0
     10 21  94 1 1 1392 0
     10 21  95 1 1 1665 0
     30 21  96 1 0  584 0
      0 21  97 1 0 2192 0
      0 21  98 1 0 2159 0
     40 21  99 1 1  656 0
     10 21 100 1 0 1701 0
    100 21 101 1 1  319 0
     95 21 102 1 1 1166 0
     90 21 103 1 1 1962 0
      5 21 104 1 0 1776 0
     10 21 105 1 1 1614 0
     10 21 106 1 1  869 0
     15 21 107 1 0  599 0
      0 21 108 1 0 1649 0
      0 21 109 1 0 1076 0
     20 21 110 1 0 1562 0
     20 21 111 1 1 1856 0
      0 21 112 1 0 2674 0
      0 21 113 1 1 2869 0
     15 21 114 1 1 2210 0
      0 21 115 1 1 2268 0
      0 21 116 1 0 2685 0
     10 21 117 1 1 1982 0
      0 21 118 1 1 1790 0
     75 21 119 1 1  582 0
     10 21 120 1 0 3912 0
      0 21 121 1 1 2822 0
     30 21 122 1 1 1096 0
      0 21 123 1 0 2546 0
      0 21 124 1 0 2430 0
      0 21 125 1 0 3262 0
     20 21 126 1 1 1055 0
     60 21 127 1 1 1893 0
      5 21 128 1 0 1968 0
      0 21 129 1 0 3580 0
      5 21 130 1 1 1102 0
      0 21 131 1 1 1246 0
     80 21 132 1 0 2415 0
     30 21 133 1 1  624 0
      0 21 134 1 1 1705 0
      0 21 135 1 1 1241 0
      0 21 136 1 0 2009 0
     10 21 137 1 1  779 0
    100 21 138 1 1 1212 0
     15 21 139 1 1  671 0
     20 21 140 1 0 2748 0
      0 21 141 1 1 2635 0
     20 21 142 1 1 1704 0
     20 21 143 1 0 3114 0
     10 21 144 1 1 1596 0
      0 21 145 1 1  652 0
     30 21 146 1 1 2322 0
     10 21 147 1 1  304 0
     20 21 148 1 1  774 0
      0 21 149 1 0 3072 0
    100 21 150 1 0 1328 0
     20 21 151 1 1 1507 0
     10 21 152 1 1  991 0
      0 21 153 1 0 3985 0
     90 21 154 1 1  147 0
     35 21 155 1 0  428 0
      0 21 156 1 1  711 0
      0 21 157 1 1 1470 0
     20 21 158 1 1 1146 0
     10 21 159 1 1 1744 0
     20 21 160 1 0  323 0
     10 21 161 1 1 1109 0
     15 21 162 1 0  832 0
      0 21 163 1 1 1515 0
     20 21 164 1 0 1911 0
     20 21 165 1 1  167 0
     25 21 166 1 1 1209 0
     20 21 167 1 1 1069 0
      0 21 168 1 1 4213 0
     15 21 169 1 1 1841 0
      5 21 170 1 0 2133 0
      0 21 171 1 0 2027 0
     60 21 172 1 1  963 0
      5 21 173 1 1 1215 0
     20 21 174 1 1 1355 0
     70 21 175 1 0 2175 0
      5 21 176 1 1 2135 0
     90 21 177 1 0 1398 0
      0 21 178 1 0 2740 0
      0 21 179 1 1 3064 0
      5 21 180 1 1  709 0
     20 21 181 1 1 1122 0
      0 21 182 1 0 1871 0
     25 21 183 1 1  564 0
    100 21 184 1 1   65 0
     20 21 185 1 1  511 0
     20 21 186 1 1  720 0
    100 21 187 1 1  726 0
    100 21 188 1 1  276 0
      0 21 189 1 1 1910 0
      0 21 190 1 0 2692 0
      0 21 191 1 0 1884 0
      0 21 192 1 1 1206 0
     25 21 193 1 0 1231 0
     40 21 194 1 1  113 0
     15 21 195 1 1 1610 0
     15 21 196 1 0  181 0
      0 21 197 1 0 2592 0
     20 21 198 1 0  898 0
    100 21 199 1 1  620 0
      0 21 200 1 0  447 0
      0 21 201 1 0 3529 0
     20 21 202 1 1 1000 0
     10 21 203 1 1  472 0
     70 32   1 1 1 1557 0
    100 32   2 1 1 1121 0
      0 32   3 1 1 1760 0
     10 32   4 1 0  417 0
      0 32   5 1 1 1482 0
      0 32   6 1 1  390 0
     60 32   7 1 1  696 0
      0 32   8 1 1  122 0
      0 32   9 1 1  378 0
      0 32  10 1 0 2235 0
     10 32  11 1 1 4181 0
      0 32  12 1 1 2131 0
     65 32  13 1 1  381 0
      0 32  14 1 1  802 0
      0 32  15 1 1 2513 0
      0 32  16 1 1  511 0
      0 32  17 1 1  298 0
      0 32  18 1 0 2359 0
     50 32  19 1 1  524 0
      0 32  20 1 1 1189 0
      0 32  21 1 0 2291 0
     40 32  22 1 1 1100 0
      0 32  23 1 0 2235 0
      0 32  24 1 0 1844 0
      0 32  25 1 1 1051 0
      0 32  26 1 1 1078 0
    100 32  27 1 1   41 0
      0 32  28 1 0  588 0
      0 32  29 1 1  873 0
      0 32  30 1 1 1303 0
     80 32  31 1 1  952 0
    100 32  32 1 1 1790 0
      0 32  33 1 0 2327 0
     70 32  34 1 1 1358 0
    100 32  35 1 1  460 0
      0 32  36 1 1  909 0
     10 32  37 1 1  205 0
      0 32  38 1 0 2184 0
     10 32  39 1 1  104 0
     10 32  40 1 0 1923 0
    100 32  41 1 1 1688 0
      0 32  42 1 1 1659 0
      0 32  43 1 1  261 0
      0 32  44 1 1 2630 0
      0 32  45 1 1 1488 0
      0 32  46 1 1  148 0
     20 32  47 1 1   46 0
     10 32  48 1 1  527 0
      0 32  49 1 1 2276 0
      0 32  50 1 0 2567 0
      0 32  51 1 1 1442 0
      0 32  52 1 1 1140 0
     40 32  53 1 1  419 0
     50 32  54 1 1 2079 0
      0 32  55 1 1 2222 0
      0 32  56 1 1  746 0
      0 32  57 1 1 1002 0
    100 32  58 1 1 1833 0
      0 32  59 1 0 1240 0
     80 32  60 1 1  242 0
     20 32  61 1 1  643 0
      0 32  62 1 1  295 0
      0 32  63 1 1  325 0
      0 32  64 1 1  925 0
      0 32  65 1 1 2433 0
      0 32  66 1 1 1464 0
     10 32  67 1 0 2024 0
      0 32  68 1 0 2296 0
      0 32  69 1 1 2029 0
     10 32  70 1 0 2702 0
      0 32  71 1 0 2816 0
      0 32  72 1 1 1093 0
     60 32  73 1 1 1756 0
     80 32  74 1 1 1264 0
      0 32  75 1 1 1745 0
     60 32  76 1 1 2418 0
     40 32  77 1 1  596 0
     20 32  78 1 1 2461 0
     60 32  79 1 1  250 0
     40 32  80 1 0 2463 0
    100 32  81 1 1 1392 0
      0 32  82 1 1 1014 0
      0 32  83 1 0 1000 0
      0 32  84 1 1 2715 0
      0 32  85 1 0 2208 0
      0 32  86 1 1 1471 0
      5 32  87 1 1 1779 0
     80 32  88 1 1 1245 0
     85 32  89 1 0 3180 0
      0 32  90 1 1 1772 0
     60 32  91 1 0 2377 0
     80 32  92 1 0 2262 0
      5 32  93 1 1 1867 0
     80 32  94 1 1 1392 0
     20 32  95 1 1 1665 0
     25 32  96 1 0  584 0
     40 32  97 1 0 2192 0
      0 32  98 1 0 2159 0
     15 32  99 1 1  656 0
     20 32 100 1 0 1701 0
    100 32 101 1 1  319 0
    100 32 102 1 1 1166 0
     70 32 103 1 1 1962 0
     10 32 104 1 0 1776 0
    100 32 105 1 1 1614 0
     10 32 106 1 1  869 0
     25 32 107 1 0  599 0
      0 32 108 1 0 1649 0
      0 32 109 1 0 1076 0
     70 32 110 1 0 1562 0
     30 32 111 1 1 1856 0
     30 32 112 1 0 2674 0
      0 32 113 1 1 2869 0
     10 32 114 1 1 2210 0
     90 32 115 1 1 2268 0
     25 32 116 1 0 2685 0
      0 32 117 1 1 1982 0
     10 32 118 1 1 1790 0
     80 32 119 1 1  582 0
     30 32 120 1 0 3912 0
      0 32 121 1 1 2822 0
     30 32 122 1 1 1096 0
      0 32 123 1 0 2546 0
      0 32 124 1 0 2430 0
      0 32 125 1 0 3262 0
     70 32 126 1 1 1055 0
     70 32 127 1 1 1893 0
     90 32 128 1 0 1968 0
      0 32 129 1 0 3580 0
     25 32 130 1 1 1102 0
     10 32 131 1 1 1246 0
     10 32 132 1 0 2415 0
     30 32 133 1 1  624 0
      0 32 134 1 1 1705 0
     10 32 135 1 1 1241 0
      0 32 136 1 0 2009 0
     20 32 137 1 1  779 0
    100 32 138 1 1 1212 0
     60 32 139 1 1  671 0
     10 32 140 1 0 2748 0
      0 32 141 1 1 2635 0
     50 32 142 1 1 1704 0
     10 32 143 1 0 3114 0
    100 32 144 1 1 1596 0
      0 32 145 1 1  652 0
     10 32 146 1 1 2322 0
      0 32 147 1 1  304 0
     20 32 148 1 1  774 0
      0 32 149 1 0 3072 0
     80 32 150 1 0 1328 0
     40 32 151 1 1 1507 0
     10 32 152 1 1  991 0
      0 32 153 1 0 3985 0
     10 32 154 1 1  147 0
     80 32 155 1 0  428 0
     20 32 156 1 1  711 0
      0 32 157 1 1 1470 0
     70 32 158 1 1 1146 0
      0 32 159 1 1 1744 0
     30 32 160 1 0  323 0
      0 32 161 1 1 1109 0
     10 32 162 1 0  832 0
     10 32 163 1 1 1515 0
     10 32 164 1 0 1911 0
      0 32 165 1 1  167 0
     10 32 166 1 1 1209 0
     10 32 167 1 1 1069 0
      0 32 168 1 1 4213 0
     10 32 169 1 1 1841 0
      0 32 170 1 0 2133 0
      0 32 171 1 0 2027 0
     30 32 172 1 1  963 0
     30 32 173 1 1 1215 0
     20 32 174 1 1 1355 0
     40 32 175 1 0 2175 0
      0 32 176 1 1 2135 0
     20 32 177 1 0 1398 0
      0 32 178 1 0 2740 0
      0 32 179 1 1 3064 0
      0 32 180 1 1  709 0
     70 32 181 1 1 1122 0
      0 32 182 1 0 1871 0
     20 32 183 1 1  564 0
    100 32 184 1 1   65 0
     30 32 185 1 1  511 0
     10 32 186 1 1  720 0
     60 32 187 1 1  726 0
    100 32 188 1 1  276 0
     60 32 189 1 1 1910 0
      0 32 190 1 0 2692 0
      0 32 191 1 0 1884 0
      0 32 192 1 1 1206 0
     40 32 193 1 0 1231 0
     70 32 194 1 1  113 0
      0 32 195 1 1 1610 0
     20 32 196 1 0  181 0
      0 32 197 1 0 2592 0
     50 32 198 1 0  898 0
    100 32 199 1 1  620 0
     20 32 200 1 0  447 0
      0 32 201 1 0 3529 0
     10 32 202 1 1 1000 0
     30 32 203 1 1  472 0
    end
    Last edited by Simon Walsh; 17 May 2023, 08:10.

  • #2
    Could someone please have a look at this question? What is happening is that my harrel program is getting run over the entire dataset and not grouping observations by user_id

    Comment


    • #3
      What stset command did you use here? (Although it's been the source of some friendly jokes, I don't use the Cox model myself.)

      Comment


      • #4
        Hi Professor Cox,

        I used

        Code:
         stset time_hrct_censor_days, failure(mortality_status==1) scale(1)
        For other analysis, stcox works - so I think my set up is ok, although your input, if otherwise, is appreciated.

        I have been playing around with the statsby command and in my case, if I do:

        Code:
        statsby _b, by(user_id) saving(`results'): stcox uip_pioped_before
        Then the saved file contains _b for stcox uip_pioped_before, for each user (in other words, the group by user_id works)

        But if I try to wrap up the stcox command with the estat concordance in a program (shown above) and run

        Code:
        statsby _b, by(user_id) saving(`results'): cox_and_harrell
        It basically runs stcox uip_pioped_before over and over, for each user, where the actually command is run for the whole dataset (i.e. it isn't grouped by the user_id). This results in a file with the same result for every user. In the example dataset above, it should run the cox_and_harrell function for each user (there are 2 users, 21 and 32, with 203 observations for each) but instead runs stcox uip_pioped_before for the full 406 observations and gives me the same results, twice (i.e. for each of the 2 users).

        I hope this makes sense - I am assuming its an issue with syntax but your expertise is hugely valued.












        Last edited by Simon Walsh; 22 May 2023, 05:55.

        Comment


        • #5
          My expertise here is distinctly limited, but I am puzzled that your stset command lumps all users together. That seems to contradict the rest of your code and may be pivotal here.

          Comment


          • #6
            Yes. I see your point. I will prepare the data in long form and then run the
            Code:
            stset
            I can see how a flaw in this could give me this bizarre result, but I can explain why using:
            Code:
            statsby
            with an off the shelf command:
            Code:
            stcox
            works but my program doesn’t.

            Comment


            • #7
              Since you've made your own wrapper command to run the Cox regression followed by concordance, you will need to enable your program to accept subsetting -if- expressions. Here's what I would do, using your data example from above. Without it, -statsby- will rerun your program with the entire dataset over and over, without actually selecting the subset for each by-group. The example below includes -noisily- as an option to -statsby- so that you can see the selection is happening.

              Code:
              rename _t time
              rename _d fail
              drop _*
              stset time, failure(fail)
              
              cap program drop harrell
              program define harrell, rclass
                syntax [if] [in]
                marksample touse
                stset time, failure(fail) if(`touse')
               
                stcox uip
                estat concordance
                return scalar harrell_c = r(C)
              end
              
              tempfile results
              statsby _b harrell_c=r(harrell_c), noi by(user_id) saving(`results'): harrell
              
              mkf Results
              cwf Results
              use `results'
              list

              Comment


              • #8
                Hi Leonardo -

                This works perfectly - now I need to study the code and your answer to learn from it, but you have really helped me. Thanks to Nick Cox also

                Comment

                Working...
                X