Announcement

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

  • Add testparm p-value to automated output for Cox models

    I am trying to create an automated output table for the results of a Cox model and would like to replace the p-values (_r_p) for each level of a categorical variable with the single p-value obtained using the testparm command.

    This is some of the code I have drafted so far but I can't seem to swap the p-values:

    collect _r_b _r_ci _r_p, ///
    name(MyModels) ///
    tag(model[(1)] colname[1.age]) ///
    : stcox age


    collect get _r_b _r_ci, ///
    name(MyModels) ///
    tag(model[(1)]) ///
    : stcox i.age_gp
    collect get r(p), tag(model[(1)] colname[(age_gp)] ) : testparm i.age_gp

    collect _r_b _r_ci _r_p, ///
    name(MyModels) ///
    tag(model[(1)]) ///
    : stcox sex


    collect layout (colname) (result[_r_b _r_ci _r_p]) (model[(1)]), name(MyModels)
    collect style row stack, spacer delimiter(" x ")
    collect style cell, nformat(%5.2f)
    collect style cell result[_r_p], nformat(%5.3f) minimum(0.001)
    collect style cell result[_r_ci], sformat("(%s)") cidelimiter(,)
    collect style cell cell_type[item column-header], halign(center)

    Any advice would be greatly appreciated.

  • #2
    It is difficult to give advice on your code without a dataset to play with.

    In the following I modify a publicly available dataset to have the variables used in your code. I point out my code edits with commends and some highlighing in blue. Also, I'm assuming you want the p-value from testparm to show up at the base level of the factor variable.
    Code:
    webuse catheter
    stset
    gen sex = female
    
    egen age_gp = cut(age), at(10 20 30 40 50 60 70) icodes
    tab age_gp
    label define age_gp ///
         0 "10 - 19" ///
         1 "20 - 29" ///
         2 "30 - 39" ///
         3 "40 - 49" ///
         4 "50 - 59" ///
         5 "60 - 69"
    label values age_gp age_gp
    
    * once you create a new collection, it becomes the default;
    * no need for option -name()- 
    collect create MyModels, replace
    
    * I removed -colname[1.age]- from -tag()-; it is not being used
    collect _r_b _r_ci _r_p, ///
        tag(model[(1)]) ///
        : stcox age
    
    
    collect get _r_b _r_ci, ///
        tag(model[(1)]) ///
        : stcox i.age_gp
    * name the result _r_p to correspond with the p-value column in the
    * following layout;
    * change the -tag()- option to use the -colname- level that corresponds
    * with the first (base) level of variable age_gp
    collect get _r_p=(r(p)), ///
        tag(model[(1)] colname[0.age_gp] ) ///
        : testparm i.age_gp
    
    collect _r_b _r_ci _r_p, ///
        tag(model[(1)]) ///
        : stcox sex
    
    collect layout (colname) (result[_r_b _r_ci _r_p]) (model[(1)])
    collect style row stack, spacer delimiter(" x ")
    collect style cell, nformat(%5.2f)
    collect style cell result[_r_p], nformat(%5.3f) minimum(0.001)
    collect style cell result[_r_ci], sformat("(%s)") cidelimiter(,)
    collect style cell cell_type[item column-header], halign(center)
    
    * proper label for -stcox- reported estimates
    collect label levels result _r_b "Hazard ratio", modify
    
    collect preview
    Here is the resulting table.
    Code:
    -----------------------------------------------
                | Hazard ratio    95% CI    p-value
    ------------+----------------------------------
    Patient age |     1.00     (0.99, 1.02)  0.614
                |
    10 - 19     |     1.00                   0.111
    20 - 29     |     4.50     (1.26,16.09)  0.021
    30 - 39     |     0.96     (0.34, 2.69)  0.934
    40 - 49     |     0.92     (0.35, 2.40)  0.868
    50 - 59     |     1.61     (0.66, 3.92)  0.291
    60 - 69     |     1.53     (0.54, 4.34)  0.425
                |
    sex         |     0.45     (0.25, 0.80)  0.006
    -----------------------------------------------

    Comment


    • #3
      Thank you Jeff, that is great and is now working for me. I did want to automate the output to remove the other p-values for each level of the categorical variables. Do you know if that is possible?
      Thanks,
      Karen

      Comment


      • #4
        There are a number of ways to accomplish this. I think it will be simpler to flag the p-values you want to see rather than remove/recode p-values that you do not.

        In the following I add a new dimension (named show) to the collection that flags which p-values to show. The additional code is highlighted in blue.
        Code:
        webuse catheter
        stset
        gen sex = female
        
        egen age_gp = cut(age), at(10 20 30 40 50 60 70) icodes
        tab age_gp
        label define age_gp ///
             0 "10 - 19" ///
             1 "20 - 29" ///
             2 "30 - 39" ///
             3 "40 - 49" ///
             4 "50 - 59" ///
             5 "60 - 69"
        label values age_gp age_gp
        
        * once you create a new collection, it becomes the default;
        * no need for option -name()-
        collect create MyModels, replace
        
        * I removed -colname[1.age]- from -tag()-; it is not being used
        collect _r_b _r_ci _r_p, ///
            tag(model[(1)]) ///
            : stcox age
        
        collect get _r_b _r_ci, ///
            tag(model[(1)]) ///
            : stcox i.age_gp
        * name the result _r_p to correspond with the p-value column in the
        * following layout;
        * change the -tag()- option to use the -colname- level that corresponds
        * with the first (base) level of variable age_gp
        collect get _r_p=(r(p)), ///
            tag(model[(1)] colname[0.age_gp] ) ///
            : testparm i.age_gp
        
        collect _r_b _r_ci _r_p, ///
            tag(model[(1)]) ///
            : stcox sex
        
        * add tag for the p-values you want to show; the dimension 'show' can be
        * any new name you want, but '_hide' is a special level that -collect-
        * automatically hides from the header when used in a layout
        collect addtag show[_hide], fortags(result[_r_p]#colname[age sex 0.age_gp])
        
        * update the layout to add the 'show' condition for p-values
        collect layout (colname) (result[_r_b _r_ci] show#result[_r_p]) (model[(1)])
        collect style row stack, spacer delimiter(" x ")
        collect style cell, nformat(%5.2f)
        collect style cell result[_r_p], nformat(%5.3f) minimum(0.001)
        collect style cell result[_r_ci], sformat("(%s)") cidelimiter(,)
        collect style cell cell_type[item column-header], halign(center)
        
        * proper label for -stcox- reported estimates
        collect label levels result _r_b "Hazard ratio", modify
        
        collect preview
        Here is the resulting table.
        Code:
        -----------------------------------------------
                    | Hazard ratio    95% CI    p-value
        ------------+----------------------------------
        Patient age |     1.00     (0.99, 1.02)  0.614 
                    |                                  
        10 - 19     |     1.00                   0.111 
        20 - 29     |     4.50     (1.26,16.09)        
        30 - 39     |     0.96     (0.34, 2.69)        
        40 - 49     |     0.92     (0.35, 2.40)        
        50 - 59     |     1.61     (0.66, 3.92)        
        60 - 69     |     1.53     (0.54, 4.34)        
                    |                                  
        sex         |     0.45     (0.25, 0.80)  0.006 
        -----------------------------------------------

        Comment


        • #5
          Thank you, that was very helpful.

          Comment

          Working...
          X