Multiple imputation

Start with the expanded example for each command. Open the variants for options and alternative R approaches. Read the qualifications before adapting the code.

mi

Stack imputations including original data

Stata

mi convert flong

R Package:mice

mice::complete(imp, action = "long", include = TRUE)

Fixed workflow; adapt the named data columns explicitly. R mids metadata and storage structures differ from Stata MI data styles.

19 more examples and alternatives

Describe multiple-imputation metadata

Stata

mi describe

R Package:mice

list(imputations = imp$m, missing_by_variable = imp$nmis, methods = imp$method)

Fixed workflow; adapt the named data columns explicitly. R mids metadata and storage structures differ from Stata MI data styles.

Pooled estimates with ninety-percent intervals

Stata

mi estimate, level(90): regress y x z

R Package:mice

summary(mice::pool(with(imp, stats::lm(y ~ x + z))), conf.int = TRUE, conf.level = .90)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Uses mice’s pooled small-sample degrees-of-freedom rules.

Pool logistic-regression coefficients

Stata

mi estimate: logit binary x z

R Package:mice

mice::pool(with(imp, stats::glm(binary ~ x + z, family = stats::binomial())))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Pool on the log-odds scale before exponentiating.

Rubin pooling of an imputed continuous mean

Stata

mi estimate: mean x

R Package:mice

{ completed <- mice::complete(imp, "all"); Q <- vapply(completed, function(d) mean(d$x), numeric(1)); U <- vapply(completed, function(d) stats::var(d$x) / nrow(d), numeric(1)); mice::pool.scalar(Q, U, n = nrow(data), k = 1) }

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Within-imputation mean variances and complete-data sample sizes are explicit.

Pool Poisson-regression coefficients

Stata

mi estimate: poisson count x z

R Package:mice

mice::pool(with(imp, stats::glm(count ~ x + z, family = stats::poisson())))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Pool log-rate coefficients; assess overdispersion separately.

Rubin pooling of linear-regression coefficients

Stata

mi estimate: regress y x z

R Package:mice

mice::pool(with(imp, stats::lm(y ~ x + z)))

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Pools within- and between-imputation variance; complete-data degrees of freedom must be appropriate.

Pool coefficient vectors and covariance matrices directly

Stata

mi estimate: regress y x z

R Package:mitools

{ fits <- lapply(mice::complete(imp, "all"), function(d) stats::lm(y ~ x + z, data = d)); mitools::MIcombine(lapply(fits, stats::coef), lapply(fits, stats::vcov)) }

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Alternative low-level Rubin pooling; degrees-of-freedom assumptions should be specified for small samples.

Extract first completed dataset

Stata

mi extract 1

R Package:mice

mice::complete(imp, 1)

Fixed workflow; adapt the named data columns explicitly. R mids metadata and storage structures differ from Stata MI data styles.

Binary logistic chained-equation imputation

Stata

mi impute chained (logit) category (pmm, knn(5)) x z = y, add(3)

R Package:mice

mice::mice(data, m = 3, maxit = 3, method = methods, seed = 131, donors = 5L, printFlag = FALSE)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Set categorical types before imputation and match the model family; random draws are not cross-software identical.

Unordered-category chained-equation imputation

Stata

mi impute chained (mlogit) category (pmm, knn(5)) x z = y, add(3)

R Package:mice

mice::mice(data, m = 3, maxit = 3, method = methods, seed = 131, donors = 5L, printFlag = FALSE)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Set categorical types before imputation and match the model family; random draws are not cross-software identical.

Ordered-category chained-equation imputation

Stata

mi impute chained (ologit) category (pmm, knn(5)) x z = y, add(3)

R Package:mice

mice::mice(data, m = 3, maxit = 3, method = methods, seed = 131, donors = 5L, printFlag = FALSE)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Set categorical types before imputation and match the model family; random draws are not cross-software identical.

Customize the imputation predictor matrix

Stata

mi impute chained (pmm, knn(5)) x z = y, add(3) // restrict predictors

R Package:mice

{ predictors <- mice::make.predictorMatrix(data); predictors["x", "z"] <- 0; mice::mice(data, m = 3, maxit = 3, predictorMatrix = predictors, seed = 132, donors = 5L, printFlag = FALSE) }

Fixed workflow; adapt the named data columns explicitly. A workflow for per-variable models; the R predictor matrix is not inferred automatically from Stata syntax.

Chained-equation imputation using pmm

Stata

mi impute chained (pmm, knn(5)) x z = y, add(3) rseed(108)

R Package:mice

mice::mice(data, m = 3, maxit = 3, method = "pmm", seed = 108, donors = 5L, printFlag = FALSE)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Imputation algorithms and random draws differ; specify predictor matrix and model types deliberately.

Chained-equation imputation using norm

Stata

mi impute chained (regress) x z = y, add(3) rseed(108)

R Package:mice

mice::mice(data, m = 3, maxit = 3, method = "norm", seed = 108, printFlag = FALSE)

Differs from Stata Fixed workflow; adapt the named data columns explicitly. Imputation algorithms and random draws differ; specify predictor matrix and model types deliberately.

Derive a nonlinear variable consistently in each completed dataset

Stata

mi passive: generate square = x^2

R Package:mice

lapply(mice::complete(imp, action = "all"), function(d) transform(d, square = x^2))

Fixed workflow; adapt the named data columns explicitly. Post-imputation derivation is appropriate when the transform is not needed as an imputation predictor; otherwise use passive imputation within the chain.

Combine repeated complete-data Wald tests

Stata

mi test x

R Package:mice

{ full <- with(imp, stats::lm(y ~ x + z)); reduced <- with(imp, stats::lm(y ~ z)); mice::D1(full, reduced) }

D1 pools coefficient estimates and covariance matrices for a multivariate MI Wald test, as Stata mi test does. Use the same coefficient ordering and complete-data degrees of freedom; D2 pools test statistics and is a different rule.

Pooled multivariate Wald comparison

Stata

mi test x z

R Package:mice

{ full <- with(imp, stats::lm(y ~ x + z)); reduced <- with(imp, stats::lm(y ~ 1)); mice::D1(full, reduced) }

Differs from Stata Fixed workflow; adapt the named data columns explicitly. D1 combines nested-model Wald information; coefficients must have consistent ordering.

Identify variables varying across imputations

Stata

mi varying

R Package:mice

vapply(imp$imp, function(z) if (nrow(z)) any(apply(z, 1, function(v) length(unique(v)) > 1)) else FALSE, logical(1))

Fixed workflow; adapt the named data columns explicitly. R mids metadata and storage structures differ from Stata MI data styles.

Inspect imputed-variable distributions in each dataset

Stata

mi xeq: summarize x

R Package:mice

lapply(mice::complete(imp, action = "all"), function(d) summary(d$x))

Fixed workflow; adapt the named data columns explicitly. R mids metadata and storage structures differ from Stata MI data styles.