Announcement

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

  • use of collect to customize a table

    Hello everyone,

    I would like to export the regression table created like this:

    Code:
    clear all
    webuse nhanes2l
    
    collect : logistic highbp age i.sex
    
    collect style cell result[_r_b _r_ci], nformat(%4.2f)
    collect style cell result[_r_ci], cidelimiter(" - ") sformat("[%s]")
    
    collect style row stack , nobinder spacer
    
    collect style header result, title(label)
    
    collect style column, dups(center)
    
    collect label levels result _r_b "OR", modify
    
    collect layout (colname[age sex]) (result[_r_b _r_ci _r_p])
    and obtain this:

    Code:
                    
            Result             
        OR    95% CI    p    value
                    
    Age (years)    1.05    [1.05 - 1.05]        0.000
                    
    Sex                     
    Male    1.00                 
    Female    0.65    [0.60 - 0.71]        0.000
    I just want to know if it's possible to add two columns before the one with OR. One with the total frequencies and one with the number of events by category (for example, in this case, the event would be highbp=1). While we're at it, is there a solution to remove the .00 from the ORs of the reference categories?

    Thanks in advance.


  • #2
    My edits are highlighted in blue.
    Code:
    clear all
    webuse nhanes2l
    
    collect : logistic highbp age i.sex
    
    collect style cell result[_r_b _r_ci], nformat(%4.2f)
    collect style cell result[_r_ci], cidelimiter(" - ") sformat("[%s]")
    
    collect style row stack , nobinder spacer
    
    collect style header result, title(label)
    
    collect style column, dups(center)
    
    collect label levels result _r_b "OR", modify
    
    collect layout (colname[age sex]) (result[_r_b _r_ci _r_p])
    
    * remove base level of factor variables
    collect style showbase off
    
    * get tabulated values -- creates new collection named Table
    table (highbp), ///
        stat(count age) ///
        stat(fvfrequency sex)
    
    * combine collections into new collection named Both; assuming the
    * collection with the -logistic- results is named "default"
    collect combine Both = default Table
    
    * new composite result that will allow us to put the continuous
    * non-missing counts and factor frequencies in the same column
    collect composite define myfreq = count fvfrequency, trim
    * hide result title and myfreq label
    collect style header result, title(hide)
    collect style header result[myfreq], level(hide)
    * look at the levels and labels of our grouping outcome variable, and make
    * some label changes
    collect label list highbp, all
    collect label levels highbp 1 "Has high BP" .m "Sample", modify
    * show "<0.001" for p-values that are ...
    collect style cell result[_r_p], minimum(.001)
    * new layout that shows the requested tabulated values
    collect layout ///
        (colname[age sex]) ///
        (highbp[1 .m]#result[myfreq] result[_r_b _r_ci _r_p])
    Here is the resulting table.
    Code:
    --------------------------------------------------------------------
                |  Has high BP   Sample     OR       95% CI      p-value
    ------------+-------------------------------------------------------
    Age (years) |        4,376   10,351   1.05   [1.05 - 1.05]    <0.001
                |
    Sex         |
      Male      |        2,304    4,915
      Female    |        2,072    5,436   0.65   [0.60 - 0.71]    <0.001
    --------------------------------------------------------------------
    Last edited by Jeff Pitblado (StataCorp); 09 Feb 2024, 09:56.

    Comment


    • #3
      If you have missing value patterns in the variables that results the total sample size for "Age" being larger than the sample size reported by logistic, then add an if e(sample) in the call to table.

      Comment

      Working...
      X