【问题标题】:Saving coefficients and standard errors as variables将系数和标准误差保存为变量
【发布时间】:2014-05-30 13:36:29
【问题描述】:

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

我可以看到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 

任何帮助将不胜感激!

【问题讨论】:

  • 为什么要将常量保存在变量中?试试scalars 或locals。标准误差只是方差的平方根。 e(V) 持有方差-协方差矩阵。

标签: stata scalar


【解决方案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'])"

【讨论】:

    【解决方案2】:

    Imperial 的 Roger Newson 写了一个很好的 package,叫做 parmest,它会为你做这件事。我花了一些时间来制定选项,但效果非常好。

    【讨论】:

      猜你喜欢
      • 2015-10-04
      • 2021-12-09
      • 2014-02-27
      • 1970-01-01
      • 2023-03-06
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多