Announcement

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

  • Creating mean, sd, and t-test table

    I want to produce a table that looks something like this:
    Variable Name Before Treatment After Treatment Difference b/w Before and After Treatment
    X (Mean) (Mean) (Difference in Mean)
    (SD) (SD) [t-stat of the difference in mean]
    Y (Mean) (Mean) (Difference in Mean)
    (SD) (SD) [t-stat of the difference in mean]
    Do summary stats, take the output of summary stats to get mean and SD, then use a t-test of the difference, etc. And then copy-paste all the values one by one.

    Or is there an easy Stata code?

    I have around 30 variables for which I would like to export the results in this format. Can anyone help me doing this?

  • #2
    You mention treatment, which made me think your data might be paired, but then state you have around 30 variables so that implies to me you have long data with a variable that identifies the "before" and "after" treatment observations. In the following I assume you have long data.

    If you have Stata 17 or newer, you can use the new collect commands to collect results from ttest and build your own custom table. Here is an example using the auto data.
    Code:
    clear all
    
    webuse fuel
    
    * example 
    ttest mpg1 == mpg2
    return list
    * note following results of interest:
    * r(mu_1) -- before treatment Mean
    * r(sd_1) -- before treatment SD
    * r(mu_2) -- after treatment Mean
    * r(sd_2) -- after treatment SD
    * r(t) - for mu_1 - mu_2
    * there is no r() scalar contining the difference, but we can compute it
    * directly from r(mu_1) and r(mu_2)
    
    collect r(mu_1) r(mu_2) diff=(r(mu_1)-r(mu_2)) ///
        r(sd_1) r(sd_2) r(t) ///
        , tags(var[mpg]) ///
        : ttest mpg1 == mpg2
    
    collect addtag result_row[1], fortags(result[mu_1 mu_2 diff])
    collect addtag result_row[2], fortags(result[sd_1 sd_2 t])
    collect style header result_row, title(hide) level(hide)
    
    collect composite define col1 = mu_1 sd_1
    collect composite define col2 = mu_2 sd_2
    collect composite define col3 = diff t
    collect style autolevels result col1 col2 col3, clear
    collect label levels result ///
        col1 "Before Treatment" ///
        col2 "After Treatment" ///
        col3 "Difference", modify
    
    collect style cell result[sd_1 sd_2], sformat("(%s)")
    collect style cell result[t], sformat("[%s]")
    
    collect style cell result[col1 col2 col3], nformat(%9.2f)
    
    collect layout (var#result_row) (result)
    Here is the resulting table.
    Code:
    -----------------------------------------------------
          | Before Treatment After Treatment Before-After
    ------+----------------------------------------------
    mpg   |            19.83           24.77        -4.95
          |           (4.74)          (6.61)      t=-3.63
    turn  |            41.44           35.41         6.03
          |           (3.97)          (1.50)       t=6.90
    trunk |            14.75           11.41         3.34
          |           (4.31)          (3.22)       t=3.27
    -----------------------------------------------------
    For more information about collect and building customizable tables. in Stata type help collect.

    Comment

    Working...
    X