发现重要价值时的结束循环:Stata?

时间:2018-01-07 01:09:37

标签: loops matrix stata

你能不能帮我解决一下:当我在回归中发现特定系数的第一个正值和有效值时,我是否告诉Stata在迭代中结束循环。

这是一个使用公开数据集的小样本,它显示了我想要做的事情:在下面的例子中,我希望stata在找到" year"时停止循环。系数是积极的和重要的。

set more off
clear all
clear matrix
use http://www.stata-press.com/data/r13/abdata
forvalues i=1/8{
xtabond n w k ys year, lags(`i') noconstant
matrix b = e(b)'
mat byear = b["year",1]
if `i'==1  matrix byear=b["year",1]
else matrix byear=(byear\ b["year",1])
}

你能帮忙找出如何告诉stata在遇到条件时停止循环。

谢谢

1 个答案:

答案 0 :(得分:0)

这是一些似乎可以做你想要的代码。我必须将置信度设置为80(默认值为95),以便在超过最大滞后数之前终止。

set more off
clear all
clear matrix
set level 80
use http://www.stata-press.com/data/r13/abdata
forvalues i=1/8{
quietly xtabond n w k ys year, lags(`i') noconstant
matrix t = r(table)
scalar b = t[rownumb(t,"b"),colnumb(t,"year")]
scalar p = t[rownumb(t,"pvalue"),colnumb(t,"year")]
scalar r = 1-r(level)/100
scalar q = (b>0) & (p<=r)
if q {
    display "success with `i' lags"
    display "b: " b " p: " p " r: " r " q: " q
    xtabond
    continue, break
    }
else {
    display "no luck with `i' lags"
    }
}

产生

no luck with 1 lags
success with 2 lags
b: .00759529 p: .18035747 r: .2 q: 1

Arellano-Bond dynamic panel-data estimation     Number of obs     =        611
Group variable: id                              Number of groups  =        140
Time variable: year
                                                Obs per group:
                                                              min =          4
                                                              avg =   4.364286
                                                              max =          6

Number of instruments =     31                  Wald chi2(6)      =    1819.55
                                                Prob > chi2       =     0.0000
One-step results
------------------------------------------------------------------------------
           n |      Coef.   Std. Err.      z    P>|z|     [80% Conf. Interval]
-------------+----------------------------------------------------------------
           n |
         L1. |   .3244849   .0774312     4.19   0.000     .1727225    .4762474
         L2. |  -.0266879   .0363611    -0.73   0.463    -.0979544    .0445785
             |
           w |  -.5464779   .0562155    -9.72   0.000    -.6566582   -.4362975
           k |    .360622   .0330634    10.91   0.000     .2958189    .4254252
          ys |   .5948084   .0818672     7.27   0.000     .4343516    .7552652
        year |   .0075953   .0056696     1.34   0.180    -.0035169    .0187075
------------------------------------------------------------------------------
Instruments for differenced equation
        GMM-type: L(2/.).n
        Standard: D.w D.k D.ys D.year

. 
end of do-file