Stata-ttest的输出结果 [英] Stata -- output results of ttest

查看:1467
本文介绍了Stata-ttest的输出结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有group_id的数据(1、2、3,依此类推).我必须通过group_id运行t检验.下面,我提供了我想用于每个group_id的代码.如何修改它,以便可以遍历group_ids并获得所有t检验相结合的输出?将不胜感激.谢谢.

I have data by group_id (1,2,3 and so on). I have to run a t test by group_id. Below I am providing the code I think I will be using for each group_id. How can I modify it so that I can loop over group_ids and get an output with all the t tests combined? Will appreciate any help. Thanks.

ttest return, by(test_indicator) unequal

推荐答案

by前缀可以为每个group_id重复t检验,但是很难与其他代码结合使用.这是一个使用levelsofforeach遍历每个id,运行ttests和post结果到临时数据集的快速示例.然后将新的数据集加载并导出到.xls文件.

The by prefix can repeat the t-test for each group_id, but it is hard to combine with other codes. Here's a quick example of using levelsof and foreach to loop through each id, run ttests, and post the results to a temporary dataset. The new dataset is then loaded and exported into an .xls file.

sysuse auto,clear
recode rep78 (1/3=1)(4/5=2),gen(id) // artificial group id
drop if missing(id) // ensure all casea have ids

tempname ttestparm
tempfile outfile

postfile `ttestparm' obs n1 n2 mu1 mu2 sd1 sd2 diff_b diff_se diff_p using `outfile',replace
levelsof id,local(idvals)
foreach i of local idvals {
    ttest mpg if id==`i', by(foreign)
    post `ttestparm' (`i') (`r(N_1)') (`r(N_2)') (`r(mu_1)') (`r(mu_2)') (`r(sd_1)') (`r(sd_2)') (`r(mu_1)'-`r(mu_2)') (`r(se)') (`r(p)')
}

postclose `ttestparm'
preserve
use `outfile',clear

export excel using "ttestout.xls", firstrow(variables) replace
restore

请注意,preserve/restore组合仅在需要处理更多原始数据的情况下才有用.

Note that the preserve/restore combo is only useful if there is more work to do with the original data.

这篇关于Stata-ttest的输出结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆