回归循环并将特定系数存储在新数据集中(Stata)

时间:2018-08-02 15:35:46

标签: loops regression stata coefficients

我有多个回归可以在双循环(带有ID和年份)中运行,然后将特定系数保存在新数据集中以进行进一步分析。我不是Stata的高级用户,因此一直未能找到获取所需数据集的方法。这是我到目前为止所做的(非常基本)

forvalues x=1000/3320 {
    forvalues y=2007/2014 {
        display as text "`x' - `y'"
        regress lnY lnL lnK lnM if (ID==`x' & Year==`y'), vce(robust)
    }
}

接下来,我要以以下格式在新数据集中存储lnL的ID,年份和系数

ID      Year    beta_L
1000    2007    coefficient value
1000    2008    coefficient value
.       .       .
.       .       .
1001    2007    coefficient value
.       .       .
and so on...

我正在处理的数据示例

ID   Year  lnY       lnL       lnK
1000 2012  5.594402  .6931472  5.493681
1000 2014 11.203094   3.89182  5.173674
1000 2009   9.12585 3.5263605  7.963082
1000 2014  8.042182   2.70805  6.599499
1000 2012  8.260284 2.6390574  5.794077
1000 2013  8.057291 1.0986123  5.455157
1000 2013  6.319292  .6931472  5.746913
1000 2009  8.667063  3.465736  5.716908
1000 2008  6.493629  .6931472   7.79329
1000 2007 9.5332575 3.2580965  6.708956
1000 2013  9.940035 3.5263605  8.670599
1000 2013  5.212381  .6931472  5.404938
1000 2012  5.996401 1.3862944  5.465735
1000 2010  8.229244  2.890372   6.46925
1000 2011 10.998017  3.135494  3.845514
1000 2014  5.207485  1.609438  2.720516
1000 2009   7.75353 1.0986123  2.724515
1000 2012  7.315685 3.4011974  5.967614
1000 2013 11.464064  6.361302 10.841784
1000 2011  7.753608  .6931472  3.232409
1000 2010  8.638702 2.6390574 4.3438053
1000 2012  9.776585  4.744932  8.152783
1000 2007   7.00119  1.609438  5.629177
1000 2009  8.411424   2.70805    6.3282
1000 2011  4.791777  .6931472  3.018835
1000 2009 11.549725  6.516193 10.913407
1000 2010  9.745195 3.9318256   6.40688
1000 2009  9.267692  3.583519  8.845691
1000 2014   8.39663   2.70805  6.152485
1000 2014  9.058921 3.8286414  8.965775
1100 2009  7.899781 2.6390574   8.794945
1100 2007   7.00119 2.1972246   8.033159
1100 2012  5.956254  .6931472   7.295885
1100 2011  2.899934  .6931472   8.412895
1100 2012  7.844419 2.6390574   8.702268
1100 2013  10.08861  3.912023   9.594462
1100 2010  7.868637 2.6390574    8.73182
1100 2013  7.904302 2.6390574  8.6678095
1100 2012 1.7197353  .6931472   8.357231
1100 2012   7.65021  1.609438   6.136364
1100 2010  9.976413    3.7612   9.558953
1100 2014  8.952582 2.6390574   7.544822
1100 2008  9.878766  3.871201   8.280781
1100 2013  8.562812 2.6390574   7.648747
1100 2012  8.616093  2.890372    7.70997
1100 2012  5.730094 1.3862944   6.518791
1100 2014  5.171491  .6931472     5.6845
1100 2011  5.898294  .6931472   2.372208
1100 2008  7.985871  2.833213   8.833616
1100 2009  5.905118  .6931472  3.2353406
1100 2009 3.1379786   1.94591   8.658174
1100 2014  8.041589 2.6390574   8.622295
1100 2013  5.707078 1.0986123   6.481857
1100 2013  7.748852 1.3862944   6.026944
1100 2007  8.270083  2.772589   8.322694
1100 2014  7.742864 1.3862944   6.011713
1100 2011  7.875942  2.564949   8.754793
1100 2010  8.572438  2.890372   8.072155
1100 2010  5.049856 1.7917595   8.547916
1100 2009  9.903218   3.73767   9.558193
1100 2014  6.008263 1.0986123   6.434088

1 个答案:

答案 0 :(得分:4)

Statalist上有数百篇有关此类问题的文章,statsby已明确涵盖,因此您的搜索策略需要升级。

您的循环将使您进行2321 x 8回归,因此18568,并显示完整结果,而您只希望每个回归系数一个。

一种更直接的方法是

statsby bL=_b[lnL] , by(ID year): ///
regress lnY lnL lnK lnM, vce(robust)