[Stata] Margins After Multiple Imputation: mimrgns
margins is how most applied papers turn a logit model into predicted probabilities and average marginal effects. After mi estimate, though, margins cannot simply be run: the pooled results are not stored in the form margins needs, and predicted probabilities have to be computed in each imputed dataset and then combined with Rubin’s rules (Rubin, 1987). Daniel Klein’s mimrgns does this. It runs the model and margins in every imputed dataset and pools the results the same way mi estimate pools coefficients.
Installation
ssc install mimrgnsThe current version is 4.0.7 (July 2022), and it runs on Stata 11.2 or newer.
Data
The examples use the 2024 California Health Interview Survey (CHIS) adult public use file, which has 24,810 adult respondents. The CHIS public use files are free and available through a brief application on the UCLA Center for Health Policy Research site. Every post in this series builds its variables with the same short do-file, 00_chis_prep.do:
use "chis_adult_2024.dta", clear
* outcomes
gen k6 = distress if distress >= 0 // K6 score, past month (0-24)
gen nousual = usual == 2 // no usual source of care
gen delay = ah22 == 1 // delayed or did not get medical care, past 12 months
gen erv = er == 1 // any ER visit, past 12 months
gen uninsured = ins == 2
gen needhelp = af81 == 1 if af81 > 0 // needed help for emotional/mental or substance problem
gen fairpoor = ab1 >= 4 // fair or poor self-rated health
gen spd = dstrs12 == 1 // likely serious psychological distress, past year
* groups
gen lep = spk_eng == 3 // speaks English not well or not at all
gen foreign = ah33new == 2
gen female = srsex == 2
* covariates
gen age = srage_p1
recode ahedc_p1 (1 2 = 1 "Less than high school") (3 = 2 "High school") ///
(4 5 6 = 3 "Some college") (7 8 9 = 4 "Bachelor's or more"), gen(educ)
gen pov = povll
label define povllf 4 "300%+ FPL", modify
label values pov povllf
recode ombsrr_p1 (1 = 1 "Latino") (2 = 2 "White") (3 = 3 "Black") (5 = 4 "Asian") ///
(4 6 = 5 "Other or multiracial"), gen(race)
* CHIS design: final weight plus 80 jackknife replicate weights
svyset [pw=rakedw0], jkrweight(rakedw1-rakedw80, multiplier(1)) vce(jackknife) mseLimited English proficiency (lep) means the respondent reported speaking English not well or not at all; 1,520 respondents (6.1 percent) meet that definition. Commands that accept the svy prefix use the jackknife replicate weights; commands that do not accept it use the final weight rakedw0 as a probability weight, which gives the right point estimates and robust standard errors that ignore the replicate design.
Missing data for the example
The CHIS public use file has no missing values on the variables used here, so for illustration about 16 percent of family income is set to missing, more often for adults under 40. Because the missingness depends only on age, which is observed, the data are missing at random, the setting in which multiple imputation gives valid results.
set seed 20260924
gen pov_m = pov
replace pov_m = . if runiform() < cond(age < 40, .30, .12)
label values pov_m povllf
misstable summarize pov_m | | Unique
Variable | Obs=. Obs>. Obs<. | values Min Max
-------------+--------------------------------+------------------------------
pov_m | 3,888 20,922 | 4 1 4Imputation and the pooled model
Income has four ordered categories, so it is imputed with an ordered logit that includes the outcome and the other model variables, which the imputation model should always do.
keep nousual lep age female educ pov pov_m rakedw0
mi set flong
mi register imputed pov_m
mi register regular nousual lep age female educ
mi impute ologit pov_m nousual lep age female i.educ [pw=rakedw0], add(20) rseed(20260924)
mi estimate, or: logit nousual i.lep age female i.educ i.pov_m [pw=rakedw0]Multiple-imputation estimates Imputations = 20
Logistic regression Number of obs = 24,810
Average RVI = 0.1087
Largest FMI = 0.3431
-------------------------------------------------------------------------------------
nousual | Odds ratio Std. err. t P>|t| [95% conf. interval]
--------------------+----------------------------------------------------------------
lep |
Yes | 1.645089 .210704 3.89 0.000 1.279872 2.114522
age | .9639378 .001937 -18.28 0.000 .9601489 .9677417
female | .6807781 .0467761 -5.60 0.000 .5950034 .7789179
|
educ |
High school | .7840977 .1096336 -1.74 0.082 .5961474 1.031304
Some college | .762327 .1065988 -1.94 0.052 .5795806 1.002695
Bachelor's or more | .5678498 .0797297 -4.03 0.000 .4312267 .7477584
|
pov_m |
100-199% FPL | .9039037 .1224367 -0.75 0.456 .6924422 1.179942
200-299% FPL | .7814104 .1214502 -1.59 0.114 .5750251 1.061871
300%+ FPL | .61849 .0812624 -3.66 0.000 .4771862 .8016365
-------------------------------------------------------------------------------------The largest fraction of missing information (FMI) is 0.34, for one of the income categories, which is in line with the share of income that was deleted.
Predicted probabilities and marginal effects
mimrgns takes the same syntax as margins. Add predict(pr) so the margins are probabilities; without it, mimrgns uses the linear prediction, because predictions that are not linear in the coefficients have to be requested explicitly.
mimrgns lep, predict(pr)
mimrgns, dydx(lep) predict(pr)Predictive margins Number of obs = 24,810
Expression : Pr(nousual), predict(pr)
------------------------------------------------------------------------------
| Margin Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
lep |
No | .1457249 .0044294 32.90 0.000 .1370435 .1544064
Yes | .2123031 .0175675 12.09 0.000 .1778713 .2467349
------------------------------------------------------------------------------
Average marginal effects Number of obs = 24,810
dy/dx w.r.t. : 1.lep
------------------------------------------------------------------------------
| dy/dx Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
lep |
Yes | .0665781 .0189479 3.51 0.000 .0294406 .1037156
------------------------------------------------------------------------------Holding the other covariates at their observed values, the predicted probability of having no usual source of care is 14.6 percent for adults without limited English proficiency and 21.2 percent for adults with it, an average marginal effect of 6.7 percentage points (95% CI 2.9 to 10.4).
Plotting with marginsplot
marginsplot needs results that look like margins output. The cmdmargins option tells mimrgns to store them that way.
mimrgns pov_m, predict(pr) cmdmargins
marginsplot, recast(scatter) ytitle("Pr(no usual source of care)") ///
xtitle("Family income (% FPL)") xscale(range(0.7 4.3)) title("") scale(1.5)
Comparison with the complete data
Because the example deleted values that were really there, the result can be checked against the model fit to the original income variable.
logit nousual i.lep age female i.educ i.pov [pw=rakedw0] if _mi_m == 0
margins pov, predict(pr)-------------------------------------------------------------------------------
| Delta-method
| Margin std. err. z P>|z| [95% conf. interval]
--------------+----------------------------------------------------------------
pov |
0-99% FPL | .2049724 .0124439 16.47 0.000 .1805829 .2293619
100-199% FPL | .1783265 .0113884 15.66 0.000 .1560056 .2006474
200-299% FPL | .1609822 .0119958 13.42 0.000 .1374708 .1844935
300%+ FPL | .1266999 .0058025 21.84 0.000 .1153272 .1380727
-------------------------------------------------------------------------------The imputed estimates (0.192, 0.178, 0.159, 0.132) are close to the complete-data ones (0.205, 0.178, 0.161, 0.127), and the odds ratio for limited English proficiency is 1.65 after imputation and 1.62 with the complete data (exp(0.481)). The imputed standard errors for income are somewhat larger, which is how the uncertainty from the missing values shows up. The if _mi_m == 0 matters: after mi set flong the data hold the original data plus all 20 imputed copies, and a model without it would run on 21 stacked copies of the sample.
Notes
mimrgns reports t statistics with degrees of freedom from the imputation, like mi estimate. Pooling predictions this way assumes they are approximately normal across imputations, which works well for probabilities that are not near 0 or 1. mimrgns does not support joint tests or contrast-style output; its help file lists what else it does not save.
References
Klein, D. (2014). MIMRGNS: Stata module to run margins after mi estimate. Statistical Software Components S457795, Boston College Department of Economics, revised 25 Jul 2022. https://ideas.repec.org/c/boc/bocode/s457795.html
Rubin, D. B. (1987). Multiple imputation for nonresponse in surveys. Wiley. https://doi.org/10.1002/9780470316696
