library(knitr) options(knitr.kable.NA = "") options(digits = 2) knitr::opts_chunk$set(comment = "#>") if (!requireNamespace("mice", quietly = TRUE) || !requireNamespace("GLMMadaptive", quietly = TRUE) || !requireNamespace("glmmTMB", quietly = TRUE) || !requireNamespace("lme4", quietly = TRUE)) { knitr::opts_chunk$set(eval = FALSE) } set.seed(333)
mira
objectsmodel_parameters()
can be used in combination with the mice package to deal
with missing data, in particular to summaries regression models used with
multiple imputed datasets. It computes pooled summaries of multiple imputed
repeated regression analyses, i.e. of objects of class mira
. Thus,
model_parameters()
for mira
-objects is comparable to the pool()
-function
from mice, but only focuses on the final summary of parameters and does not
include the diagnostic statistic per estimate.
library(mice) library(parameters) data("nhanes2") imp <- mice(nhanes2, printFlag = FALSE) fit <- with(data = imp, exp = lm(bmi ~ age + hyp + chl)) model_parameters(fit)
Not all packages work with with.mids()
from package mice. Thus, for some
modeling packages, it's not possible to perform multiply imputed repeated
analyses, i.e. you cannot work with imputed data for such models. We give an
example for the GLMMadaptive package here.
First, we generate a dataset with missing values. We take the data cbpp
from
lme4 and randomly assign some missing values into one of the predictors. Then
we impute the data, using mice()
from package mice.
library(lme4) library(GLMMadaptive) data(cbpp) cbpp$period[sample(seq_len(nrow(cbpp)), size = 10)] <- NA imputed_data <- mice(cbpp, printFlag = FALSE)
Using with
to compute multiple regression analyses for each imputed dataset
fails.
fit <- with(data = imputed_data, expr = GLMMadaptive::mixed_model( cbind(incidence, size - incidence) ~ period, random = ~ 1 | herd, family = binomial )) # > Error in as.data.frame(data) : # > argument "data" is missing, with no default
However, we can use a workaround by using pool_parameters()
, which works on a
list of model objects. So whenever a model-object is not yet supported by
mice::with()
, you can instead fit multiple models to the imputed datasets and
pool all parameters with pool_parameters()
:
The steps would be:
Calculate the regression models for each imputed dataset manually (either by
using complete()
from package mice to get the imputed datasets, or by
accessing the datasets directly from the mids
object)
Save all model objects in a list.
Pass the list to pool_parameters()
.
models <- lapply(1:imputed_data$m, function(i) { mixed_model( cbind(incidence, size - incidence) ~ period, random = ~ 1 | herd, data = complete(imputed_data, action = i), family = binomial ) }) pool_parameters(models)
For comparison and to show that the results from mice:pool()
and
pool_parameters()
are identical, we take an example that also works with the
mice package:
library(mice) library(parameters) data("nhanes2") imp <- mice(nhanes2, printFlag = FALSE) # approach when model is supported by "mice" fit <- with(data = imp, exp = lm(bmi ~ age + hyp + chl)) summary(pool(fit)) # approach when model is *not* supported by "mice" models <- lapply(1:5, function(i) { lm(bmi ~ age + hyp + chl, data = complete(imp, action = i)) }) pool_parameters(models)
mipo
objectsIt is also possible to compute summaries of pooled objects of class mipo
.
data("nhanes2") imp <- mice(nhanes2, printFlag = FALSE) fit <- with(data = imp, exp = lm(bmi ~ age + hyp + chl)) pooled <- pool(fit) model_parameters(pooled)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.