Stata:在存在因子变量时在.dta文件中保存回归系数和标准误差
Stata: saving regressions coefficients and standard errors in .dta file when there are factor variables
我想 运行 几个回归并将它们的结果存储在 DTA 文件中,我可以稍后用于分析。我的约束是:
- 我无法安装模块(我正在为其他人编写代码而不是
确定他们安装了哪些模块)
- 一些回归变量是因子变量。
- 每个回归仅因因变量不同,所以我想将其存储在最终数据集中以跟踪 coefficients/variances 对应的回归。
我在这里严重失去理智。我觉得这可能很简单,因为 Stata 是统计软件,但 svmat
真的不合作。目前我正在做的是:
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 表示系数,_v 表示方差。
我使用手动常量,因为在使用 svmat
时 _cons 不是变量的有效名称。
当然我的 "solution" 不起作用,因为因子水平生成奇怪的矩阵列名,然后在调用 svmat
时这些列名是无效的变量名。 (错误是一个简洁的 invalid syntax
。)鉴于我的限制,我很乐意使用任何解决方案来解决这个问题。它不必使用 svmat,系数和方差可以在同一行上,如果这样更容易等。
重命名矩阵列是一种选择:
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)
对于更复杂的 namelists (reg1 - reg4),您可以预先构建语法,存储在 local
中,然后与 matname
一起使用.
编辑
同样的策略,有一些自动化。它对矩阵使用宏扩展函数。参见 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 describe matnames
。
为了完整起见,使用 Roberto 优秀的解决方案,这是最终代码:
sysuse census, clear
generate constant = 1
capture matrix drop regsresults // erase previously existing matrix
replace region = region +15
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
local rownames : rownames regsresults // collects row names
local colnames : colfullnames regsresults // collects column names
local newnames // clean column names
foreach name of local colnames {
local newnames `newnames' `=strtoname("`name'")'
}
matrix colnames regsresults = `newnames' // attribute the cleaned column names
clear
svmat regsresults, names(col)
// add the row names as its own variable rown
gen str rown = ""
order rown,
local i = 1
foreach rowname in `rownames' {
replace rown = "`rowname'" if _n == `i'
local i = `i' + 1
}
br
我想 运行 几个回归并将它们的结果存储在 DTA 文件中,我可以稍后用于分析。我的约束是:
- 我无法安装模块(我正在为其他人编写代码而不是 确定他们安装了哪些模块)
- 一些回归变量是因子变量。
- 每个回归仅因因变量不同,所以我想将其存储在最终数据集中以跟踪 coefficients/variances 对应的回归。
我在这里严重失去理智。我觉得这可能很简单,因为 Stata 是统计软件,但 svmat
真的不合作。目前我正在做的是:
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 表示系数,_v 表示方差。
我使用手动常量,因为在使用 svmat
时 _cons 不是变量的有效名称。
当然我的 "solution" 不起作用,因为因子水平生成奇怪的矩阵列名,然后在调用 svmat
时这些列名是无效的变量名。 (错误是一个简洁的 invalid syntax
。)鉴于我的限制,我很乐意使用任何解决方案来解决这个问题。它不必使用 svmat,系数和方差可以在同一行上,如果这样更容易等。
重命名矩阵列是一种选择:
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)
对于更复杂的 namelists (reg1 - reg4),您可以预先构建语法,存储在 local
中,然后与 matname
一起使用.
编辑
同样的策略,有一些自动化。它对矩阵使用宏扩展函数。参见 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 describe matnames
。
为了完整起见,使用 Roberto 优秀的解决方案,这是最终代码:
sysuse census, clear
generate constant = 1
capture matrix drop regsresults // erase previously existing matrix
replace region = region +15
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
local rownames : rownames regsresults // collects row names
local colnames : colfullnames regsresults // collects column names
local newnames // clean column names
foreach name of local colnames {
local newnames `newnames' `=strtoname("`name'")'
}
matrix colnames regsresults = `newnames' // attribute the cleaned column names
clear
svmat regsresults, names(col)
// add the row names as its own variable rown
gen str rown = ""
order rown,
local i = 1
foreach rowname in `rownames' {
replace rown = "`rowname'" if _n == `i'
local i = `i' + 1
}
br