循环访问Stata中的一组reg3模型

时间:2018-09-05 01:37:53

标签: loops stata

要遍历Stata中的一组普通回归,我知道我可以使用以下代码。首先定义几个本地人,然后使用forv

local v1 "y1 x1 x2 "
local v2 "y2 x3 x4"
local v3 "y3 x5 x6"

// regressions
forv i=1/2 {
    reg `v`i''
}

我的问题是对reg3怎么做?我猜想我应该能够执行以下操作

local v1 "(y1 x1 x2) (y2 x3 x4) "
local v2 "(y2 x3 x4) (y3 x5 x6)"

// regressions
forv i=1/2 {
    reg3 `v`i''
}

但是,我收到错误消息“找不到最后的估计值r(301);”

欢迎任何建议!

1 个答案:

答案 0 :(得分:2)

当您使用不带任何必需参数的估算命令时,通常会发生该错误。例如,如果我打开一个新的Stata版本,然后键入reg3,我将得到:

. reg3
last estimates not found
r(301);

但是,您的代码在编写时是完全有效的

. clear

. set seed 9042018

. set obs 10
number of observations (_N) was 0, now 10

. 
. forvalues i=1/6 {
  2.         gen x`i'=uniform()
  3.         gen y`i'=uniform()
  4. }

. 
. local v1 "(y1 x1 x2) (y2 x3 x4)"

. local v2 "(y2 x3 x4) (y3 x5 x6)"

. 
. // regressions
. forv i=1/2 {
  2.         reg3 `v`i''
  3. }

Three-stage least-squares regression
--------------------------------------------------------------------------
Equation             Obs   Parms        RMSE    "R-sq"       chi2        P
--------------------------------------------------------------------------
y1                    10       2    .2036362    0.3092       4.46   0.1073
y2                    10       2    .3072859    0.1303       1.49   0.4745
--------------------------------------------------------------------------

------------------------------------------------------------------------------
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
y1           |
          x1 |  -.3124269   .2502829    -1.25   0.212    -.8029723    .1781185
          x2 |  -.4351287    .236802    -1.84   0.066     -.899252    .0289946
       _cons |   .9107828   .1956381     4.66   0.000     .5273392    1.294226
-------------+----------------------------------------------------------------
y2           |
          x3 |   .1470169   .5794549     0.25   0.800    -.9886939    1.282728
          x4 |   .6000975   .5086198     1.18   0.238    -.3967789    1.596974
       _cons |   .2083595   .4934943     0.42   0.673    -.7588715    1.175591
------------------------------------------------------------------------------
Endogenous variables:  y1 y2 
Exogenous variables:   x1 x2 x3 x4 
------------------------------------------------------------------------------

Three-stage least-squares regression
--------------------------------------------------------------------------
Equation             Obs   Parms        RMSE    "R-sq"       chi2        P
--------------------------------------------------------------------------
y2                    10       2    .3083462    0.1243       1.45   0.4835
y3                    10       2    .2391397    0.1213       1.23   0.5416
--------------------------------------------------------------------------

------------------------------------------------------------------------------
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
y2           |
          x3 |  -.0055726   .5760862    -0.01   0.992    -1.134681    1.123536
          x4 |   .5426255   .5067634     1.07   0.284    -.4506124    1.535863
       _cons |    .324529   .4910202     0.66   0.509    -.6378528    1.286911
-------------+----------------------------------------------------------------
y3           |
          x5 |   .0497209   .4631086     0.11   0.915    -.8579553    .9573972
          x6 |     .29023   .2693022     1.08   0.281    -.2375926    .8180526
       _cons |   .4143936   .2388091     1.74   0.083    -.0536637    .8824508
------------------------------------------------------------------------------
Endogenous variables:  y2 y3 
Exogenous variables:   x3 x4 x5 x6 
------------------------------------------------------------------------------

. 

由于人们很少有这样的名字的变量,我怀疑您没有完全显示您键入的内容。我建议您在reg3之前加入这样的一行:

display `"reg3 `v`i''"'

这将显示缺少参数的任何问题。