Stata使用bysort为IV回归创建输出

时间:2016-09-13 12:00:10

标签: stata

所以我通过采访年运行2SLS模型,我有很多面试年和不同的模型。我想首先介绍第一阶段的结果,然后再让读者放心,他们坚定地转向有趣的结果。

表A的示例(第一阶段):

年DV Coef SE F N

1 A 0.5 0.1 100 1000

2 A 0.8 0.2 10 1500

3 B -0.6 0.4 800 800

表B的主要结果看起来与F-Stat相同。

我在网上搜索了如何在Stata中自动创建这些表格,但是尽管发现了许多问题,我还没有找到适合我的答案。从那些不同的帖子和帮助文件中,我构建了几乎就在那里的东西。

它通过一些变量(代码中的步骤A)将F-Stat与主结果一起创建。但是,当我继续为第一阶段做同样的事情时,它只会在恢复估计时保存最后一波。我理解为什么Stata会这样做,但我想不出一种方法可以说服它做我想做的事。

clear all

*Install user-written commands
ssc install outreg2, replace
ssc install ivreg210, replace

*load data
sysuse auto, clear

*run example model (obviously the model itself is bogus)
********************************************************
*Step A: creates the IV results by foreign plus the F-Statistic
bys foreign:  ///
    outreg2 using output1-IV-F, label excel stats(coef se) dec(2) adds(F-Test, e(widstat)) nocons nor2 keep(mpg) replace: ///
    ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
*Step B: creates the first stage results in a seperate table
bys foreign:  ///
    ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
    est restore _ivreg210_mpg   
    outreg2 using output1_1st-stage, replace keep(rep78)

cap erase output1-IV-F
cap erase output1_1st-stage

理想情况下,我只会运行一次模型并在第一阶段表中使用F-Stat,但我可以手动修复它。我遇到的最大问题是如何在使用bysort时存储估算值。如果有人对此有任何建议,我将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:0)

Regex pattern = new Regex(LogFormat.GetLineRegex(logFileFormat), RegexOptions.Compiled);

然后您可以存储您想要以后使用的任何结果,即使是在一个副词之后。

ssc install estout

答案 1 :(得分:-1)

这是一个全面的解决方案。它有效,但实际上并不是我正在寻找的正确解决方案。 "技巧"是作为一个单独的模型运行第一阶段。

clear all

*Install user-written commands
ssc install outreg2, replace
ssc install ivreg210, replace

*load data
sysuse auto, clear

*run example model (obviously the model itself is bogus)
********************************************************
*Step A: creates the IV results by foreign plus the F-Statistic
bys foreign:  ///
    outreg2 using output1-IV-F, label excel stats(coef se) dec(2) adds(F-Test, e(widstat)) nocons nor2 keep(mpg) replace: ///
    ivreg210 price headroom trunk (mpg=rep78 ), savefirst first

*Step B: creates the first stage results in a seperate table
bys foreign:  ///
    ivreg210 price headroom trunk (mpg=rep78 ), savefirst first
    est restore _ivreg210_mpg   
    outreg2 using output1_1st-stage1, replace keep(rep78)

*************   
/* NEW BIT */   
*************   
*Step C: creates the first stage results in a seperate table
bys foreign:  ///
    outreg2 using output1_1st_NEW, label excel stats(coef se) dec(2) nocons nor2 keep(rep78) replace: ///
    reg mpg headroom trunk rep78

cap erase output1-IV-F
cap erase output1_1st-stage1
cap erase output1_1st_NEW