我想运行几个回归并将结果存储在我以后可用于分析的DTA文件中。我的约束是:
我在这里严重失去理智。鉴于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代表coeffs,_v代表差异。
我使用手动常量,因为_cons在使用svmat
时不是变量的有效名称。
当然,我的“解决方案”不起作用,因为因子级别会生成奇怪的矩阵列名称,这些名称在调用svmat
时是无效的变量名称。 (错误是一个简洁的invalid syntax
。)鉴于我的限制,我很乐意解决这个问题的任何解决方案。它不必使用svmat,系数和方差可以在同一条线上,如果它更容易,等等。
答案 0 :(得分:2)
重命名矩阵列是一个选项:
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),您可以预先构建语法,存储在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
。
答案 1 :(得分:0)
为了完整起见,使用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