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:
- I cannot install modules (I am writing code for other people and not sure what modules they have installed)
- Some of the regressors are factor variables.
- Each regression differ only by the dependent variable, so I would like to store that in the final dataset to keep track of what regression the coefficients/variances correspond to.
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)
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.
I use a manual constant because _cons is not a valid name for a variable when using 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:
For more complex namelists (reg1 - reg4), you can build the syntax beforehand, store in a
local
, and then use withmatname
.Edit
The same strategy, with some automatation. It uses macro extended functions for matrices. See
help extended_fcn
.See also
ssc describe matnames
.Just for completeness, using Roberto excellent solution, this is the final code: