从 Stata 中的 e(b) 和 e(V) 中删除省略变量的参数

Drop parameters of omitted variables from e(b) and e(V) in Stata

我想从 e(b)e(V) 矩阵中删除省略变量的参数(等于 0),但是以编程方式进行,因为这个小例程在更大的程序中。您可以在 Statalist here.

的交叉发布问题中看到 为什么 我想这样做的完整解释

例如,想象一下下面的情况。

clear all
sysuse auto , replace
version 12
tempvar constant  
gen `constant' = 1
reg price mp mp `constant' , nocons
matrix V = e(V)
mat li V

symmetric V[3,3]
                               o.            
                 mpg         mpg    __000000
     mpg   2817.1347
   o.mpg           0           0
__000000  -59997.356           0   1370802.5

您将在下面看到我想要的输出。无论变量名称或省略变量的数量如何,该解决方案都应该有效。

symmetric V_non_omitted[2,2]
                 mpg    __000000
     mpg   2817.1347
__000000  -59997.356   1370802.5

谢谢

好吧...令人惊讶 乏味。无论如何,这是一个解决方案。-

开始于:

clear all
sysuse auto , replace
version 12
tempvar constant  
gen `constant' = 1
reg price mp mp mp `constant' , nocons

首先:e(b).

的解决方案
*First for e(b)
mat b = e(b)
mat li e(b)

*Extracting the full_name of the columns
local colfullnames_b: colnames b
di "`colfullnames_b'"

* Keeping only coefficients of non-omitted variables
foreach i in `colfullnames_b' {
    local first_two = substr("`i'",1,2)
    if "`first_two'" != "o."{
        local non_omitted  `non_omitted'  `i'
    }       
}

di "`non_omitted'"

*Getting the row name of the e(b) object
local rownames_b: rownames b
di "`rownames_b'"
* Getting the number of non-omitted variables 
local n_non_omitted : word count `non_omitted'
di "`n_non_omitted'"
* Getting the column equations 
local coleq: coleq b
di "`coleq'"

* setting counter for indexing purposes
local counter = 1
*generating a matrix to store the parameters
matrix b_non_omitted = J(1,`n_non_omitted',.)

foreach i in `non_omitted' {
    
    local col_var_i = colnumb(b,"`i'")
    di "`col_var_i'"
    
    matrix b_non_omitted[1, `counter']  = b[1,`col_var_i']
    local counter=`counter'+1
}


matrix colnames b_non_omitted = `non_omitted'
matrix rownames b_non_omitted = `rownames_b' 
mat li b_non_omitted

非遗漏系数的结果。

mat li b
#b[1,4]
#                         o.          o.            
#           mpg         mpg         mpg    __000000
#y1  -238.89435           0           0   11253.061

# b_non_omitted[1,2]
#            mpg    __000000
# y1  -238.89435   11253.061

第二:e(V).

的解决方案
*Second for e(V)
mat V = e(V)
mat li V
matrix V_non_omitted  = J(`n_non_omitted',`n_non_omitted',.)
* Here I am recoverin all the var-covar of the non-omitted variables
loc counter_i = 1
foreach i in `non_omitted' {
    loc counter_j = 1
    foreach j in `non_omitted' {
        local col_var_j = colnumb(V,"`j'")
        local row_var_i = rownumb(V,"`i'")
        di "`col_var_j'"
        matrix V_non_omitted[`counter_i', `counter_j']  = V[`row_var_i',`col_var_j']
        local counter_j = `counter_j' + 1
    }
local counter_i=`counter_i' + 1
}
*Column and row names are the same as e(b) 
matrix colnames V_non_omitted = `non_omitted'
matrix rownames V_non_omitted = `non_omitted'
mat li V

非省略结果variances/covariances.

#symmetric V[4,4]
#                               o.          o.            
#                 mpg         mpg         mpg    __000000
#     mpg   2817.1347
#   o.mpg           0           0
#   o.mpg           0           0           0
#__000000  -59997.356           0           0   1370802.5


mat li V_non_omitted
#symmetric V_non_omitted[2,2]
#                 mpg    __000000
#     mpg   2817.1347
#__000000  -59997.356   1370802.5

Ps:仍然愿意接受更好(更短)的答案。也许是这样的风格: matrix omitted = e(b)[1,!"o.:"] 左右...