# Dataframe with missings in X
# Create Imp and Lm object
dat <- mice::nhanes
# add indicator training and test
# first 20 for training, last 5 for testing
dat$set <- c(rep("train", 20), rep("test", 5))
# Make prediction matrix and ensure that set is not used as a predictor
predmat <- mice::make.predictorMatrix(dat)
predmat[,"set"] <- 0
# Impute missing values based on the train set
imp <- mice(dat, m = 5, maxit = 5 , seed = 1, predictorMatrix = predmat,
ignore = ifelse(dat$set == "test", TRUE, FALSE), print = FALSE)
impdats <- complete(imp, "all")
# extract the training and test data sets
traindats <- lapply(impdats, function(dat) subset(dat, set == "train", select = -set))
testdats <- lapply(impdats, function(dat) subset(dat, set == "test", select = -c(set)))
# Fit the prediction models, based on the imputed training data
fits <- lapply(traindats, function(dat) lm(age ~ bmi + hyp + chl, data = dat))
# pool the predictions with function
pool_preds <- mice::predict_mi(object = fits, newdata = testdats,
pool = TRUE, interval = "prediction", level = 0.95)
Run the code above in your browser using DataLab