In some applications, e.g. if you want to save coefficient estimates from a regression with many dummies (e.g. fixed effects), you might want to store coefficients as estimates. In this example, we are interested in storing the estimates of the GROUPVAR
dummies, but not the dummies of OTHERVAR
. While this is usually straightforward by writing
xi: reg OUTCOME i.GROUPVAR i.OTHERVAR, nocons noomit
gen NEWVAR=_b[_IGROUPVAR_123]
where GROUPVAR
is the original variable name for which the dummies where created in the regression (using xi:
). Occasionally, it happens that some of the coefficients are not estimated due to multicollinearity (be sure to figure out why!), i.e. Stata reports
i.GROUPVAR _IGROUPVAR_111-999 (naturally coded; _IGROUPVAR_111 omitted)
If you are now interested to store all estimates of all dummies generated by “i.GROUPVAR
“, we need to separate these variable names stored in the loop (starting with _IGROUPVAR_
) from all other variable names stored in the loop.
While it is easy to check whether variables or scalars exist (using capture confirm variable VARNAME
or capture confirm scalar SCALARNAME
), it seems more difficult to do for large number of estimates after a regression, especially if some of the dummies are omitted. There are other ways to do it (e.g. using the _rmcoll
command), but the following worked well for me. It first saves all stored estimates from the matrix of estimation results (stored in e(b)
), and then does something with the estimates with certain names (in my case: those starting with _IGROUPVAR
). In my case, this means that we are saving the estimates. The syntax goes as following:
* Run the regression (without constants)
xi: reg OUTCOME i.GROUPVAR i.OTHERVAR, nocons noomit
* Create local with all variable names used in the estimation (i.e. this does not include omitted variables)
local regvars : colnames e(b)
* Create variable to store estimates
gen VAR_FE=.
* Run loop over all variables stored in local regvarsforeach locvar of local regvars {
* Execute the command only for dummies generated from variable GROUPVAR
if substr("`locvar'",1,10)=="_IGROUPVAR" {
* Remove the prefix from the loop-local `locvar' (e.g. keep "keep 123" instead of "_IGROUPVAR_123"):
local GROUP_ID=subinstr("`locvar'" ,"_IGROUPVAR" ,"",1)
di _b["`locvar'"]
di "`GROUP_ID'"
replace VAR_FE=_b[`locvar'] if GROUPVAR==`GROUP_ID'
}
}
Now each observation (e.g. firm 123 stored in GROUPVAR
) now has their respective fixed effect stored in variable VAR_FE
.
Leave a Reply
You must be logged in to post a comment.