Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Stata使用bysort创建IV回归的输出_Stata - Fatal编程技术网

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

Stata使用bysort创建IV回归的输出,stata,Stata,因此,我每年都在运行一个2SLS模型,我有很多年的面试时间和不同的模型。我想先介绍第一阶段的结果,然后在向读者保证它们是可靠的之后,再转到有趣的结果 表A第一阶段示例: 年份DV系数SE F N 1A 0.50.1100 1000 2 A 0.80.2 10 1500 3 B-0.6 0.4 800 表B中的主要结果在没有F-Stat的情况下看起来是一样的 我在网上搜索了如何在Stata中自动创建这些表,但尽管找到了许多问题,我还是没有找到适合我的答案。从这些不同的帖子和帮助文件中,我构建了一些

因此,我每年都在运行一个2SLS模型,我有很多年的面试时间和不同的模型。我想先介绍第一阶段的结果,然后在向读者保证它们是可靠的之后,再转到有趣的结果

表A第一阶段示例:

年份DV系数SE F N

1A 0.50.1100 1000

2 A 0.80.2 10 1500

3 B-0.6 0.4 800

表B中的主要结果在没有F-Stat的情况下看起来是一样的

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

它通过代码中的某个变量步骤A,为F-Stat的主要结果创建我想要的表。然而,当我继续为第一阶段做同样的事情时,它只会在我恢复估计时保存最后一个波动。我理解斯塔塔为什么这样做,但我想不出一个办法来说服它做我想做的事

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

谢谢

ssc install estout
然后,您可以存储任何您想要的结果以供以后使用,即使在bysort之后也是如此

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

这是一个全面的解决方案。这是可行的,但确实不是我一直/正在寻找的正确解决方案。诀窍是将第一阶段作为单独的模型运行

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

谢谢你的回复!然而,我不明白这是如何回答这个问题的?从outreg更改为eststo并不能解决使用bysort时stata只记住最后一次估计的问题。在这里查看您自己的示例:ssc安装estout,替换sysuse auto,清除BY foreign:reg price mpg,对我的结果esttab my_result/*您在esttab中忘记了一个t*/如果我误解了什么,请告诉我。如果使用eststo比使用outreg2更容易,我很乐意使用eststo。不过,我猜问题在于Stata如何存储估算结果。忘了那个兄弟吧。请参阅我的解决方案请参阅我对您的解决方案不起作用的原因的解释?我很高兴知道您是否能够解释您的解决方案为什么/如何回答我提出的问题。当你一开始不理解我的问题时,投票支持我解决我自己的问题,并与我分享它,这是相当。。。。