Announcement

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

  • Using factor variables in frame loop for cox models

    Hi, I have several variables that I am running a univariate analysis on. I would like the results to export the following columns into a word or excel format (HR STD_ERR Z PVALUE LB95 UB95). Below, I have posted my dataex along with my code. When I get the results of the loop I am running, it is very different than when I run stcox i.'v' for each variable alone. Can anyone tell me what I am doing wrong? I would prefer not to do each one individually as I have several categorical variables with different categories.
    ---------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(gender cad mi dvt stroke dm htn) float(died subject_uid time) byte(_st _d) double _t byte _t0
    1 0 0 0 0 2 1 1 165       .001 1 1 .0010000000474974513 0
    0 1 0 0 0 2 1 1 169 .032786883 1 1  .032786883413791656 0
    1 1 1 0 0 2 1 1 275 .032786883 1 1  .032786883413791656 0
    1 0 0 1 0 0 1 1  92 .032786883 1 1  .032786883413791656 0
    1 0 0 0 1 2 1 1 212 .032786883 1 1  .032786883413791656 0
    0 1 0 0 0 2 1 1 256  .06557377 1 1   .06557376682758331 0
    1 0 0 0 0 2 1 0 148  21.114754 1 0    21.11475372314453 0
    0 1 1 0 0 2 1 0 402  21.344263 1 0   21.344263076782227 0
    0 0 0 0 0 2 1 0 145   21.37705 1 0    21.37704849243164 0
    0 1 1 1 1 2 1 0 131  21.409836 1 0   21.409835815429688 0
    1 1 1 0 0 2 1 0 171  21.540983 1 0   21.540983200073242 0
    0 0 0 0 0 2 1 0 327  21.540983 1 0   21.540983200073242 0
    0 1 0 0 0 2 1 0 130   21.57377 1 0    21.57377052307129 0
    0 1 0 0 0 0 1 0 136  21.639345 1 0   21.639345169067383 0
    0 1 1 0 1 2 1 0 125   21.67213 1 0   21.672130584716797 0
    0 1 0 0 0 0 1 0 120         22 1 0                   22 0
    0 0 0 1 0 0 0 0 324  22.065575 1 0   22.065574645996094 0
    1 1 0 0 0 2 1 0 119   22.22951 1 0   22.229507446289063 0
    1 0 0 0 0 0 1 0 110   22.42623 1 0    22.42622947692871 0
    0 1 1 0 1 1 1 0 310  22.459017 1 0   22.459016799926758 0
    0 1 1 0 0 2 1 0 292   22.52459 1 0    22.52458953857422 0
    0 0 0 0 0 0 0 0 107   22.52459 1 0    22.52458953857422 0
    0 0 0 0 0 0 0 0 313    22.7541 1 0   22.754098892211914 0
    0 0 0 0 0 2 1 0 308  22.786884 1 0   22.786884307861328 0
    1 1 0 1 0 2 1 0  96  22.918034 1 0   22.918033599853516 0
    0 1 1 0 0 0 1 0 251  23.114754 1 0    23.11475372314453 0
    1 1 0 0 0 2 1 0  88   23.18033 1 0   23.180328369140625 0
    0 1 0 0 0 2 1 0 295    23.2459 1 0   23.245901107788086 0
    1 1 0 0 1 2 1 0 290  23.409836 1 0   23.409835815429688 0
    1 1 1 0 0 0 1 0 287  23.540983 1 0   23.540983200073242 0
    1 0 0 0 0 2 1 0  86   23.57377 1 0    23.57377052307129 0
    0 0 0 0 0 0 1 0  78   23.60656 1 0   23.606557846069336 0
    0 1 0 0 0 2 1 0  81   23.60656 1 0   23.606557846069336 0
    0 0 0 0 0 0 1 0 284  23.639345 1 0   23.639345169067383 0
    1 1 1 0 0 2 1 0 281   23.80328 1 0    23.80327796936035 0
    0 1 1 1 0 2 1 0  74  23.836065 1 0     23.8360652923584 0
    1 1 0 0 0 0 1 0 279  23.836065 1 0     23.8360652923584 0
    0 1 0 0 0 2 1 0 276   23.86885 1 0   23.868852615356445 0
    1 1 1 0 0 2 1 0 269   23.90164 1 0   23.901639938354492 0
    1 0 0 0 0 2 0 0 272   23.90164 1 0   23.901639938354492 0
    0 0 0 0 0 2 1 0 263  24.360655 1 0   24.360654830932617 0
    0 1 0 0 0 0 1 0 315   24.42623 1 0    24.42622947692871 0
    0 1 0 0 1 2 1 0 260  24.491804 1 0   24.491804122924805 0
    0 1 1 0 1 2 1 0 258  24.557377 1 0   24.557376861572266 0
    0 0 0 0 1 2 1 0  71  24.557377 1 0   24.557376861572266 0
    0 1 1 0 0 2 1 0  68  24.590164 1 0   24.590164184570313 0
    0 1 0 1 0 2 1 0 254  24.590164 1 0   24.590164184570313 0
    0 1 1 0 0 0 1 0 248   24.85246 1 0   24.852458953857422 0
    1 0 0 0 0 0 1 0 243  24.983606 1 0   24.983606338500977 0
    1 1 0 0 1 2 1 0 235   25.18033 1 0   25.180328369140625 0
    1 1 1 1 1 2 1 0 232    25.2459 1 0   25.245901107788086 0
    0 1 1 0 0 2 1 0 230   25.27869 1 0   25.278688430786133 0
    1 0 0 0 0 0 1 0 226   25.67213 1 0   25.672130584716797 0
    0 1 1 0 1 0 1 0  66   25.70492 1 0   25.704917907714844 0
    0 0 0 0 1 1 1 0 224  25.737705 1 0    25.73770523071289 0
    1 1 1 0 0 2 1 0  63  26.163935 1 0     26.1639347076416 0
    0 1 0 0 0 0 1 0  60   26.22951 1 0   26.229507446289063 0
    1 1 1 0 0 2 1 0 209   26.32787 1 0   26.327869415283203 0
    0 1 0 1 0 1 1 0 206  26.557377 1 0   26.557376861572266 0
    1 0 0 1 0 0 1 0 203  27.344263 1 0   27.344263076782227 0
    0 0 1 0 0 0 1 0 175  27.508196 1 0   27.508195877075195 0
    0 1 0 1 0 0 1 0 193   27.80328 1 0    27.80327796936035 0
    0 0 0 0 0 0 1 0 190  28.065575 1 0   28.065574645996094 0
    1 1 1 0 0 2 1 0 188   28.19672 1 0    28.19672203063965 0
    0 1 1 0 0 0 1 0  58   28.22951 1 0   28.229507446289063 0
    1 1 0 0 0 0 0 0  55   28.39344 1 0   28.393442153930664 0
    0 1 0 0 0 0 0 0 185   28.39344 1 0   28.393442153930664 0
    0 0 0 0 1 0 0 0 182   28.42623 1 0    28.42622947692871 0
    0 1 0 0 0 2 1 0  52  28.491804 1 0   28.491804122924805 0
    0 1 1 0 0 0 1 0  47   28.52459 1 0    28.52458953857422 0
    1 1 0 1 0 0 1 0 179   28.62295 1 0    28.62295150756836 0
    0 0 0 0 1 0 1 0 168  29.114754 1 0    29.11475372314453 0
    0 0 0 0 0 2 1 0  45   29.18033 1 0   29.180328369140625 0
    0 1 1 1 0 2 1 0 162   29.57377 1 0    29.57377052307129 0
    0 1 0 0 0 0 1 0 159   29.67213 1 0   29.672130584716797 0
    0 0 0 0 0 0 1 0  43   29.67213 1 0   29.672130584716797 0
    1 0 1 0 0 0 1 0 156   29.80328 1 0    29.80327796936035 0
    1 1 0 0 0 0 1 0 150  30.590164 1 0   30.590164184570313 0
    1 0 0 0 0 0 1 0 147  31.016394 1 0   31.016393661499023 0
    0 1 0 0 0 0 1 0 142   31.27869 1 0   31.278688430786133 0
    0 0 0 0 0 2 1 0 139  31.442623 1 0   31.442623138427734 0
    1 0 0 0 0 0 1 0  40  31.442623 1 0   31.442623138427734 0
    0 1 1 0 0 0 1 0  37  31.442623 1 0   31.442623138427734 0
    1 0 0 1 0 2 1 0 135   31.60656 1 0   31.606557846069336 0
    1 0 0 0 0 2 1 0 129   31.90164 1 0   31.901639938354492 0
    0 1 0 0 0 0 1 0  34  32.360657 1 0    32.36065673828125 0
    0 0 0 1 1 2 1 0  31  32.393444 1 0     32.3934440612793 0
    0 0 0 0 0 0 1 0  28   32.42623 1 0   32.426231384277344 0
    1 0 0 1 0 0 0 0 124   32.42623 1 0   32.426231384277344 0
    1 1 0 0 0 0 0 0 122   32.52459 1 0    32.52458953857422 0
    1 0 0 0 0 0 1 0 118  32.754097 1 0    32.75409698486328 0
    0 1 0 0 0 0 1 0 116   32.85246 1 0    32.85245895385742 0
    0 1 0 0 1 2 1 0 115   32.98361 1 0    32.98360824584961 0
    1 1 0 1 1 0 1 0 109   33.18033 1 0   33.180328369140625 0
    0 1 0 0 0 0 1 0 112  33.245903 1 0    33.24590301513672 0
    1 1 0 1 0 2 1 0 106   33.34426 1 0   33.344261169433594 0
    0 1 1 0 0 2 1 0  25  33.540985 1 0   33.540985107421875 0
    0 1 1 0 0 2 1 0  65  33.967213 1 0    33.96721267700195 0
    0 1 0 0 0 2 1 0  95  33.967213 1 0    33.96721267700195 0
    1 0 0 0 0 0 1 0  89  34.393444 1 0     34.3934440612793 0
    end
    label values gender gender_
    label def gender_ 0 "Male", modify
    label def gender_ 1 "Female", modify
    label values cad cad_
    label def cad_ 0 "No", modify
    label def cad_ 1 "Yes", modify
    label values mi mi_
    label def mi_ 0 "No", modify
    label def mi_ 1 "Yes", modify
    label values dvt dvt_
    label def dvt_ 0 "No", modify
    label def dvt_ 1 "Yes", modify
    label values stroke stroke_
    label def stroke_ 0 "No", modify
    label def stroke_ 1 "Yes", modify
    label values dm dm_
    label def dm_ 0 "No", modify
    label def dm_ 1 "Type 1", modify
    label def dm_ 2 "Type 2", modify
    label values htn htn_
    label def htn_ 0 "No", modify
    label def htn_ 1 "Yes", modify
    label values died yn
    label def yn 0 "No", modify
    label def yn 1 "Yes", modify
    ------------------ copy up to and including the previous line ------------------


    ---------------------- copy starting from the next line -----------------------
    Code:
    stset time, failure(died == 1) id(subject_uid)
    
    frame create results str64 Independentvariable float(HR STD_ERR Z PVALUE LB95 UB95)
    foreach v of varlist gender dm htn cad mi dvt stroke {
        stcox i.`v'
        matrix M = r(table)
        levelsof `v', local(levels)
        foreach L of local levels {
            local cname = "`L'.`v'"
            capture scalar b  = M["b","`cname'"]
            if _rc continue
            scalar se = M["se","`cname'"]
            scalar z  = M["z","`cname'"]
            scalar p  = M["pvalue","`cname'"]
            scalar ll = M["ll","`cname'"]
            scalar ul = M["ul","`cname'"]
            frame post results ///
                ("`v'=`L'") ///
                (exp(b)) ///
                (se) ///
                (z) ///
                (p) ///
                (exp(ll)) ///
                (exp(ul))
        }
    }
    
    frame change results
    format HR LB95 UB95 %5.3f
    format STD_ERR Z PVALUE %05.3f
    quietly compress
    list, noobs clean
    ------------------ copy up to and including the previous line ------------------

  • #2
    This is happening because you are exponentiating the values of b, ll, and ul. Folowing -stcox-, the matrix r(table) that you are working from contains the results in the hazard ratio metric already: you don't need to, and clearly should not, then exponentiate those. That sort of exponentiation is only needed when using a command that outputs coefficients from a model with a log-link (e.g. -logit-, -poisson-) in which case you would need to exponentiate those to get the desired result (an odds ratio in the -logit- case, an incidence rate ratio for -poisson-)

    Just get rid of the exponentiations and you will be fine.

    Comment


    • #3
      This worked perfectly Clyde! Thank you so much.

      Comment

      Working...
      X