require(mice)
require(nlme)
require(broom.mixed)
require(parallel)
# Load dataset
data(Obesity)
# Define imputation methods for each incomplete variables
meth <- find.defaultMethod(Obesity, ind.clust = 1)
# Modify some of the proposed imputation methods
# Deterministic imputation
meth["BMI"] <- "~ I(Weight / (Height)^2)"
meth["Age"] <- "2l.2stage.pmm"
# Set method, here Weight variable is assumed an MNAR variable
# Weight imputed with the Heckman method
meth["Weight"] <- "2l.2stage.heckman"
# Set type of predictor variable,
# All covariates are included in both outcome and selection equation
ini <- mice(Obesity, maxit = 0)
pred <- ini$pred
pred[,"Time"] <- 0
pred[,"Cluster"] <- -2
pred[pred == 1] <- 2
# Time was used as exclusion restriction variable
pred["Weight","Time"] <- -3
# Deterministic imputation, to avoid circular predictions
pred[c("Height", "Weight"), "BMI"] <- 0
# Imputation of continuous variables (time consumming)
# nnodes <- detectCores()
# imp <- mice.par(Obesity, meth = meth, pred = pred, m=10, seed = 123,
# nnodes = nnodes)
# summary(complete(imp,"long")$Weight)
# Imputation of continuous variables using the predictor mean matching method.
# Imputed values fall within the range of observable variables.
# imp_pmm <- mice.par(Obesity, meth = meth, pred = pred, m = 10,
# seed = 123, pmm=TRUE, nnodes = nnodes)
# summary(complete(imp_pmm,"long")$Weight)
# Fit the model
# model_MNAR <- with(imp,lme( BMI ~ Age + FamOb + Gender,random=~1+Age|Cluster))
# model_MNAR_pmm <- with(imp_pmm,lme( BMI ~ Age + FamOb + Gender,random=~1+Age|Cluster))
# summary(pool(model_MNAR))
# summary(pool(model_MNAR_pmm))
Run the code above in your browser using DataLab