Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
918 views
in Technique[技术] by (71.8m points)

stata - How to add ttest results to esttab

I am constructing a table of means and p-values from ttest. How can I get all of this in the same esttab table? Here is a MWE:

Get the sample, save it as a temporary file, create a local with the variables we will consider, create a local that is the length of the first local:

sysuse auto2, clear
*create two groups: 0 and 1
gen group = _n<37
tempfile a
save `a'

local vars "price headroom trunk weight"
local vars_n: word count `vars'

ssc install estout
eststo clear

Calculate the means of group 0 (column 1) and group 2 (column 2):

*group 0 means
use `a', clear
keep if group==0
eststo: estpost sum `vars'

*group 1 means
use `a', clear
keep if group==1
eststo: estpost sum `vars'

Conduct t-tests for each variable (is there an easier way to do this?):

*t-test
*create blank matrix
matrix pval = J(`vars_n',1,.) 
use `a', clear
forvalues i=1/`vars_n' {
    local var `: word `i' of `vars''
    ttest `var', by(group)
    *add the two-sided p-value to matrix
    matrix pval[`i',1]=r(p)
}

This previous block of code saves the p-values (column 3) into a matrix.

Use esttab to output the results:

esttab, cells(mean(fmt(2))) collabels(none) nodepvars nonumber replace label

esttab matrix(pval, fmt(2 0))

My issue is that I need to have the p-values in the same esttab as the means, but I currently have them in a matrix. How can I use something like eststo: estpost to get them so that I can use esttab (as opposed to esttab matrix)? Or is there a better way to do all of this? My goal is to run esttab, cells(mean(fmt(2))) collabels(none) nodepvars nonumber replace label and have it create a table with the first two columns being the means and the third column being the p-values.

question from:https://stackoverflow.com/questions/66067865/how-to-add-ttest-results-to-esttab

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

All the information you need is in estpost ttest, so an easy solution would be this:

sysuse auto2, clear
gen group = _n<37
local vars price headroom trunk weight
estpost ttest `vars', by(group)
esttab ., cells("mu_1 mu_2 p") nonumber label


-----------------------------------------------------------
                                                           
                             mu_1         mu_2            p
-----------------------------------------------------------
Price                    5847.526     6500.639     .3445597
Headroom (in.)           2.828947     3.166667     .0861206
Trunk space (.. ft.)     12.39474     15.19444     .0041618
Weight (lbs.)            2654.474     3404.722     .0000115
-----------------------------------------------------------
Observations                   74                          
-----------------------------------------------------------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...