Stata:当存在因子变量时,将回归系数和标准误差保存在.dta文件中 [英] Stata: saving regressions coefficients and standard errors in .dta file when there are factor variables

查看:792
本文介绍了Stata:当存在因子变量时,将回归系数和标准误差保存在.dta文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行多个回归并将其结果存储在DTA文件中,以便以后用于分析。我的约束是:

I would like to run several regressions and store their results in a DTA file that I could later use for analysis. My constraints are:


  1. 我无法安装模块(我是在为其他人编写代码,而不是
    确认他们已经安装了什么模块)

  2. 某些回归变量是因子变量。

  3. 每个回归仅因因变量不同,因此我想将其存储在最终变量中跟踪系数/方差对应的回归值的数据集。

我在这里严重丧失了理智。考虑到Stata是统计软件,我觉得这很简单,但是 svmat 确实不合作。目前我正在做的是这样:

I am seriously losing sanity here. I feel it's probably simple given that Stata is statistics software but svmat is really not cooperative. Currently what I am doing is this:

sysuse census, clear
generate constant = 1
capture matrix drop regsresults // erase previously existing matrix
foreach depvar in marriage divorce {

    reg `depvar' popurban i.region constant, robust noconstant  // regressions
    matrix result_matrix = e(b)\vecdiag(e(V))                   // grab coeffs and their variances in a 2xK matrix
    matrix rownames result_matrix = `depvar'_b `depvar'_v       // add rownames to the two extra rows
    matrix regsresults = nullmat(regsresults)\result_matrix     // add those results matrix to the existing ones

}
matrix list regsresults
clear 
svmat regsresults, names(col)

这将为每次回归创建:一行存储系数,一行存储系数使用 vecdiag(e(V))进行方差分析。这两个行的行名称是因变量名称,后跟_b代表coeffs,_v代表方差。

This creates for each regression: one row that stores the coefficients, one row that stores their variance using vecdiag(e(V)). The row names for those two rows are the dependent variable name, followed by _b for coeffs and _v for variances.

我使用手动常量,因为_cons不是有效名称对于使用 svmat 时的变量。

I use a manual constant because _cons is not a valid name for a variable when using svmat.

当然,我的解决方案不起作用,因为因子水平生成奇怪的矩阵列名称,这些列名称在调用 svmat 时是无效的变量名称。 (错误是简洁的无效语法。)鉴于我的限制,我很乐意使用任何解决该问题的解决方案。不必使用svmat,如果更容易,系数和方差可以在同一行上,等等。

Of course my "solution" does not work because factor levels generate strange matrix column names which are then invalid variable names when calling svmat. (The error is a terse invalid syntax.) I'd be happy with ANY solution to overcome that problem, given my constraints. It doesn't have to use svmat, coefficients and variances can be on same line if it makes it easier, etc.

推荐答案

重命名矩阵列是一种选择:

Renaming matrix columns is one option:

sysuse census, clear
generate constant = 1
capture matrix drop regsresults // erase previously existing matrix
foreach depvar in marriage divorce {

    reg `depvar' popurban i.region constant, robust noconstant  // regressions
    matrix result_matrix = e(b)\vecdiag(e(V))                   // grab coeffs and their variances in a 2xK matrix
    matrix rownames result_matrix = `depvar'_b `depvar'_v       // add rownames to the two extra rows
    matrix regsresults = nullmat(regsresults)\result_matrix     // add those results matrix to the existing ones

}
matrix list regsresults
matname regsresults reg1 reg2 reg3 reg4, columns(2..5) explicit

clear 
svmat regsresults, names(col)

对于更复杂的名称列表(reg1-reg4),您可以预先构建语法,并将其存储在本地的 ,然后与 matname 一起使用。

For more complex namelists (reg1 - reg4), you can build the syntax beforehand, store in a local, and then use with matname.

相同的策略,但有所自动化。它对矩阵使用宏扩展函数。请参阅帮助extended_fcn

The same strategy, with some automatation. It uses macro extended functions for matrices. See help extended_fcn.

sysuse census, clear
generate constant = 1
capture matrix drop regsresults // erase previously existing matrix
foreach depvar in marriage divorce {

    reg `depvar' popurban i.region constant, robust noconstant  // regressions
    matrix result_matrix = e(b)\vecdiag(e(V))                   // grab coeffs and their variances in a 2xK matrix
    matrix rownames result_matrix = `depvar'_b `depvar'_v       // add rownames to the two extra rows
    matrix regsresults = nullmat(regsresults)\result_matrix     // add those results matrix to the existing ones

}

// list the matrix
matrix list regsresults

// get original column names of matrix
local names : colfullnames regsresults

// get original row names of matrix (and row count)
local rownames : rowfullnames regsresults
local c : word count `rownames'

// make original names legal variable names
local newnames
foreach name of local names {
    local newnames `newnames' `=strtoname("`name'")'
}

// rename columns of matrix
matrix colnames regsresults = `newnames'

// convert matrix to dataset
clear 
svmat regsresults, names(col)

// add matrix row names to dataset
gen rownames = ""
forvalues i = 1/`c' {
    replace rownames = "`:word `i' of `rownames''" in `i'
}

// list
order rownames
list, noobs

另请参见 ssc描述子名

这篇关于Stata:当存在因子变量时,将回归系数和标准误差保存在.dta文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆