R/meusv_mice.R
meusv_mice.Rd
meusv_mice: estimate marginal effects at user-specified values using data objects created by mice::mice() (.mids)
meusv_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
user-specified values
a dataframe of the pooled marginal effect estimates (pooled using Rubin's rules)
## generate random data for example
## 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 marginal effects at user-specified values
memice::initialize_memice()
pooled_mfx <- memice::meusv_mice(model = lm_mod, mice_object = imputation, cyl = 4, wt = 3.5)
pooled_mfx
#> term contrast estimate std.error statistic p.value user_spec_vars
#> 1 wt dY/dX -6.19618278 1.73872465 -3.5636366 0.0003665855 cyl, wt
#> 2 hp dY/dX -0.02462797 0.01141123 -2.1582217 0.0309166107 cyl, wt
#> 3 cyl 6 - 4 1.64523805 2.59862192 0.6331194 0.5266556757 cyl, wt
#> 4 cyl 8 - 4 1.98701101 3.14458727 0.6318829 0.5277236573 cyl, wt
#> 5 disp dY/dX -0.00672253 0.01372794 -0.4896969 0.6246266311 cyl, wt
#> user_spec_vals
#> 1 4, 3.5
#> 2 4, 3.5
#> 3 4, 3.5
#> 4 4, 3.5
#> 5 4, 3.5