Jun 24

Save Estimation Results in Stata

Tag: stata, tricksadmin @ 6:40 pm

JohnZ posted a solution to saving your coefficient estimates in Stata to a simple dataset. Here is the code:

*!
*! est2data.ado --
*!
*! Usage: est2data nbase outfname
*!
*! Saves the 'b' and 'se' vectors of the most recent estimation results
*! to a dataset named 'outfname', using 'nbase' as the base name for the
*! dataset columns.
*!
*! Also saves variable labels. Each row of the resulting dataset
*! is a coefficient estimate.
*!
program define est2data
preserve
args nbase outfname
drop in 1/`c(N)'
matrix bvec = e(b)'
matrix vvec = vecdiag(e(V))'
svmat bvec, names(`nbase'_est)
svmat vvec, names(`nbase'_se)
quietly: replace `nbase'_se = sqrt(`nbase'_se)

local vnames: rownames bvec
local numv: list sizeof vnames
quietly: gen `nbase'_names = ""
quietly: gen `nbase'_lbl = ""
forvalues ivar = 1/`numv' {
local thisv: word `ivar' of `vnames'
quietly: replace `nbase'_names = "`thisv'" in `ivar'
local thislbl "NA"
capture local thislbl: variable label `thisv'
quietly: replace `nbase'_lbl = "`thislbl'" in `ivar'
}
keep `nbase'_*
notes: "e(N) `e(N)'"
notes: "e(depvar) `e(depvar)'"
notes: "e(ll) `e(ll)'"
notes: "e(cmd) `e(cmd)'"
save `outfname', replace
end

Thanks John!

Leave a Reply