Announcement

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

  • Regression table: Estimations from multiple models into one column

    Hi!
    In medical research, regression tables like the example below are common - I would like to automatically create them. Notably, the unadjusted column contains results from two univariable models.

    I looked into collect or etable and it's straightforward to create a table with 3 columns (2 univariable models, 1 multivariable) but that's not the intended result. Does anyone know how to do this?

    Click image for larger version

Name:	Bildschirmfoto 2024-06-07 um 07.50.53.png
Views:	1
Size:	32.4 KB
ID:	1755565





    I used to create them using estout and then the resulting file in excel to half-manually create the final table as above.

    Code:
    webuse cancer
    stset study died
    
    foreach var of varlist drug age  {
             stcox  `var'
             estimates store `var'
        }
    
    stcox drug age
    estimates store multi
    
    estout drug age multi using coxtable.txt, cells(b(fmt(2)))  eform   replace
    estout drug age multi using coxtable.txt, cells(ci(fmt(2))) eform   append
    estout drug age multi using coxtable.txt, cells(p(fmt(3)))  eform   append

    which outputs the file:

    Code:
      drug  age multi
      b b b
    drug  0.26    0.22
    age   1.08  1.12
      drug  age multi
      ci95  ci95  ci95
    drug  0.15,0.46   0.12,0.40
    age   1.01,1.15 1.04,1.20
      drug  age multi
      p p p
    drug  0.000   0.000
    age   0.023 0.002


    Last edited by Fabian Fortner; 07 Jun 2024, 00:56.

  • #2
    Thanks for the working example.

    The following code uses collect to reproduce the table in #1. Note the use of the collect: prefix with option tags(), this allows us to stack the univariate results using our own dimension named fit.
    Code:
    webuse cancer
    stset study died
    
    foreach var of varlist drug age {
        collect, tags(fit[Unadjusted]) : stcox `var'
    }
    
    collect, tags(fit[Adjusted]) : stcox drug age
    
    * use suggested formatting
    collect style column, dups(center)
    collect style cell result[_r_b], nformat(%9.2f)
    collect style cell result[_r_ci], nformat(%4.2f) cidelimiter(", ") sformat("(%s)")
    collect style cell result[_r_p], nformat(%9.3f) minimum(.001)
    
    * use suggested result labels
    collect label levels result _r_b "HR", modify
    collect label levels result _r_ci "(__LEVEL__%)", modify
    collect label levels result _r_p "p", modify
    
    * show variable names instead of their labels
    collect style header colname, level(value)
    
    * turn off all borders
    collect style cell border_block, border(all, pattern(none))
    * add border in column header cells above the results
    collect style cell cell_type[column-header]#result, border(top, pattern(solid))
    
    * arrange the items into a table
    collect layout (colname) (fit#result[_r_b _r_ci _r_p])
    Here is the resulting table.
    Code:
                Unadjusted                Adjusted
         -------------------------------------------------
           HR     (95%)         p   HR     (95%)         p
    drug 0.26 (0.15, 0.46) <0.001 0.22 (0.12, 0.40) <0.001
    age  1.08 (1.01, 1.15)  0.023 1.12 (1.04, 1.20)  0.002

    Comment


    • #3
      Hi Jeff! Thank you very much!!!

      Comment

      Working...
      X