knitr::opts_knit$set(
    stop_on_error = 2L
)
knitr::opts_chunk$set(
    fig.height = 11,
    fig.width = 7
)

options(cite = FALSE)

First, using the Amelia package, we multiply impute a dataset with missing values:

library(purrr)
library(Amelia)

data(africa)
a.out <- amelia(x = africa, cs = "country", ts = "year", logs = "gdp_pc")

Then we can use the output object from the Amelia package to fit a list of models:

models <- map(a.out$imputations, ~ lm(gdp_pc ~ trade + civlib, data = .))

Amelia includes a mi.meld function that can be used to combine the results:

mi.meld(reduce(map(models, coef), rbind),
        reduce(map(models, ~ coef(summary(.))[, "Std. Error"]), rbind))

Nicer output is available from several of the available multiple-imputation packages in R, including the mice and mitools.

library(mice)
(foo <- summary(pool(as.mira(models))))

Quantities of interest for MI

Using simulations to estimate uncertainty makes it pretty easy to incorporate additional uncertainty from imputed values.

library(smargins)

models.sm <- map(models,
                 ~smargins(., trade = quantile(trade,
                                               c(.25, .50, .75),
                                               na.rm = TRUE)))

summary(reduce(models.sm, rbind))


izahn/smargins documentation built on Sept. 11, 2019, 2:08 p.m.