latex - 将 esttab 与 ttest 一起使用

标签 latex stata

我正在运行 ttest使用 estpost 命令并将结果导出到 LaTeX和社区贡献的命令 esttab .

我正在测试几年的平均值(变量 height ,通过 child gender )的差异,并希望这些年份垂直(按行)而不是水平显示。

我的代码如下:

foreach i in 2009 2010 2013 {
    use "`i'.dta", clear
    global year `i'
    eststo _$year : estpost ttest height, by(child_gender)
}

esttab . using "trends.tex", nonumber append

2009年数据:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 156
1 135
0 189
1 168
0 157
1 189
1 135
1 145
0 124
1 139
end

2010年数据:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 151
1 162
0 157
1 134
0 157
1 189
1 135
1 145
0 143
1 166
end

2013年数据:
* Example generated by -dataex-. To install: ssc install dataex
clear
input float(child_gender height)
0 177
0 135
0 189
0 168
0 157
1 189
1 135
1 145
1 124
1 127
end

我希望输出安排如下(但在 LaTeX 中):

enter image description here

关于如何进行这项工作的任何建议?

最佳答案

可以在下面找到执行此操作的方法。您需要使用选项来进一步完善表格。

首先定义程序append_ttests ,这是appendmodels的快速修改版本, Ben Jann 的 program for stacking models :

program append_ttests, eclass
    version 8
    syntax namelist
    tempname b V tmp
    foreach name of local namelist {
        qui est restore `name'
        mat `tmp' = e(b)
        local eq1: coleq `tmp'
        gettoken eq1 : eq1
        mat `tmp' = `tmp'[1,"`eq1':"]
        local cons = colnumb(`tmp',"_cons")
        if `cons'<. & `cons'>1 {
            mat `tmp' = `tmp'[1,1..`cons'-1]
        }
        mat `b' = nullmat(`b') , `tmp'

        mat `tmp' = e(t)
        mat `tmp' = `tmp'["`eq1':","`eq1':"]
        if `cons'<. & `cons'>1 {
            mat `tmp' = `tmp'[1..`cons'-1,1..`cons'-1]
        }
        capt confirm matrix `V'
        if _rc {
            mat `V' = `tmp'
        }
        else {
            mat `V' = ///
            ( `V'  \ ///
             `tmp' )
        } 
    }
    mat `b' = `b''
    mat A = `b' , `V'
    mat rown A = `0'
    ereturn matrix results = A
    eret local cmd "append_ttests"
end

然后运行您的循环并附加 t 检验:
foreach i in 2009 2010 2013 {
    use "`i'.dta", clear
    estpost ttest height, by(child_gender)
    estimates store year`i' 
 }

append_ttests year2009 year2010 year2013

查看结果如下:
esttab e(results), nonumber mlabels(none) ///
                   varlabels(year2009 2009 year2010 2010 year2013 2013) ///
                   collabels("Height" "t statistic")

--------------------------------------
                   Height  t statistic
--------------------------------------
2009             4.666667     .3036859
2010            -3.166667    -.2833041
2013                 21.2     1.415095
--------------------------------------

添加 tex查看 LaTeX 的选项输出。

关于latex - 将 esttab 与 ttest 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56547006/

相关文章:

latex - 以垂直显示而不是水平显示绘制表格

vim - 在vim中为latex插件添加语法高亮

java - MathML 到 LaTeX 的转换

android - 在 LaTeX 中包含 Android 代码的最简单方法是什么?

plot - 如何在同一张图上获得多个系数

r - 复杂的条件变异 : Create new variable with conditional mutating using only past observations for a given participant?

r - stata (11) 中逐个因子交互的语法是什么?

stata - 清除宏

将多个文件读入多个数据框

image - 在 LaTeX 中 float 图像后如何清除?