R/ame_mice.R
ame_mice.Rd
ame_mice: estimate average marginal effects using data objects created by mice::mice() (.mids)
ame_mice(model, mice_object, ...)
a model object, either lm() or glm(), created using the original data with missing values
an object of class .mids, created using the mice::mice() function, containing imputed data
additional arguments (not intended to be used)
a dataframe of the pooled marginal effect estimates (pooled using Rubin's rules)
## Create dataset with missingness
data_with_missings <- mtcars |>
missMethods::delete_MCAR(0.3, "cyl") |>
dplyr::mutate(cyl = as.factor(cyl))
## Create model object
lm_mod <- lm(formula = mpg ~ wt + hp + cyl + cyl*wt + disp,
data = data_with_missings)
## Impute data
imputation <- mice::mice(data = data_with_missings, m = 5, seed = 01701)
#>
#> iter imp variable
#> 1 1 cyl
#> 1 2 cyl
#> 1 3 cyl
#> 1 4 cyl
#> 1 5 cyl
#> 2 1 cyl
#> 2 2 cyl
#> 2 3 cyl
#> 2 4 cyl
#> 2 5 cyl
#> 3 1 cyl
#> 3 2 cyl
#> 3 3 cyl
#> 3 4 cyl
#> 3 5 cyl
#> 4 1 cyl
#> 4 2 cyl
#> 4 3 cyl
#> 4 4 cyl
#> 4 5 cyl
#> 5 1 cyl
#> 5 2 cyl
#> 5 3 cyl
#> 5 4 cyl
#> 5 5 cyl
## Estimate and pool average marginal effects
pooled_mfx <- memice::ame_mice(model = lm_mod, mice_object = imputation)
pooled_mfx
#> term contrast estimate std.error statistic p.value
#> 1 wt dY/dX -3.452168371 1.05859858 -3.2610741 0.00111112
#> 2 hp dY/dX -0.021935526 0.01177799 -1.8624165 0.06257376
#> 3 cyl 6 - 4 -1.123803760 1.67022463 -0.6728459 0.50105077
#> 4 cyl 8 - 4 -0.908763163 2.38620936 -0.3808397 0.70351528
#> 5 disp dY/dX -0.005478762 0.01271360 -0.4309371 0.66653809