将系数和标准误差保存为变量

时间:2014-05-30 13:36:29

标签: stata scalar

我运行了回归,我想将系数和标准误差保存为变量。

我可以看到ereturn liste(b)的系数,但我无法获得标准错误。另外,我现在还不知道如何将它们变成变量。在Stata手册中,他们引用[eqno] b[varname][eqno] se[varname],但没有示例,我无法在网上找出/找到如何使用它们。

b1是因变量1的回归系数的示例,se1是依赖var1的回归标准误差:

name    year    output  var1    var2    b1  b2  se1 se2  
a   1   0.72    0.74    0.87    0.64    0.15    0.48    0.62  
a   2   0.61    0.75    0.33    0.64    0.15    0.48    0.62  
a   3   0.08    0.61    0.85    0.64    0.15    0.48    0.62  
b   1   0.02    0.22    0.26    0.64    0.15    0.48    0.62  
b   2   0.8 0.32    0.51    0.64    0.15    0.48    0.62  
b   3   0.47    0.68    0.79    0.64    0.15    0.48    0.62  
c   1   0.56    0.12    0.63    0.64    0.15    0.48    0.62  
c   2   0.35    0.49    0.53    0.64    0.15    0.48    0.62  
c   3   0.93    0.65    0.97    0.64    0.15    0.48    0.62 

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

手动执行的示例

sysuse auto, replace
reg price mpg foreign trunk weight length
mata: b=st_matrix("e(b)")' // extracts e(b) into mata matrix b
mata: se=sqrt(diagonal(st_matrix("e(V)"))) // converts e(V) into se and placed in mata matrix se
getmata b se, force // force mata matrices into variables
list b se if b<.

或者为了避免使用mata,你可以使用,

sysuse auto, replace
reg price mpg foreign trunk weight length
ereturn list
mat b=e(b)' // transpose e(b) into matrix b
svmat double b, n(beta) // convert matrix b into variable beta1 (see help svmat)

mat V=e(V) // place e(V) in V
loca nv=`e(rank)' // count number of right hand variables
mat se=J(`nv',1,-9999) // create empty matrix for standard errors
forval i=1/`nv' {
    mat se[`i',1]=sqrt(V[`i',`i']) // convert the variances into the se one at a time
}   
svmat double se, n(se) // convert matrix se into variable se1
list beta1 se1 if beta1<.

不太确定您希望如何安排值。如果你喜欢它们的列而不是行(比如你喜欢手动预测yhat),只需在运行svmat之前转置矩阵。只需要修改这三行。

mat b=e(b) // instead of "mat b=e(b)'"
mat se=J(1,`nv',-9999) // instead of "mat se=J(`nv',1,-9999)"
mat se[1,`i']=sqrt(V[`i',`i']) // instead of "mat se[1,`i']=sqrt(V[`i',`i'])"

答案 1 :(得分:0)

帝国的罗杰·纽森写了一个名为parmest的好package来为你做这件事。我花了一些时间来研究这些选项,但效果非常好。