【问题标题】:Stata creating output for IV regression with bysortStata 使用 bysort 为 IV 回归创建输出
【发布时间】:2016-09-13 12:00:10
【问题描述】:

所以我按面试年份运行 2SLS 模型,我有很多面试年份和不同的模型。我想先展示第一阶段的结果,然后在向读者保证他们是可靠的之后继续讨论有趣的结果。

表A示例(第一阶段):

年 DV 系数 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 时存储估计值。如果有人对此有任何建议,我将不胜感激。

谢谢!

【问题讨论】:

    标签: stata


    【解决方案1】:
    ssc install estout
    

    然后您可以存储您想要的任何结果以供以后使用,即使在经过排序之后也是如此。

    eststo clear
    sysuse auto, clear
    bysort foreign: eststo: reg price weight mpg
    esttab, label nodepvar nonumber
    

    【讨论】:

    • 感谢您的回复!但是,我不明白这如何回答这个问题?从 outreg 更改为 eststo 并不能解决使用 bysort stata 时只记住最后一次估计的问题。在此处查看您自己的示例: ssc install estout, replace sysuse auto, clear bys foreign :reg price mpg, robust eststo my_result esttab my_result /* you forgot a t in esttab */ 如果我误解了什么,请告诉我。如果它比使用 outreg2 更容易,我很高兴更改为 eststo。不过,我的猜测是问题在于 Stata 如何存储估计结果。
    【解决方案2】:

    这是一个迂回的解决方案。它有效,但确实不是我正在/正在寻找的正确解决方案。 “诀窍”是将第一阶段作为单独的模型运行。

    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
    

    【讨论】:

    • 忘掉那个兄弟吧。查看我的解决方案
    • 查看我对您的解决方案为何不起作用的解释?我很高兴知道您是否可以解释您的解决方案为什么/如何回答我提出的问题。当你一开始不理解问题时,投票支持我解决我自己的问题并分享它是相当的......
    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 2015-02-17
    相关资源
    最近更新 更多