# Create Data
B1=.5
B2=.5
X<-rnorm(1000)
XX<-X^2
e<-rnorm(1000, 0, 1)
Y <- B1*X+B2*XX+e
dat <- data.frame(x=X, xx=XX, y=Y)
# Impose 25 percent MCAR Missingness
dat[0 == rbinom(1000, 1, 1-.25), 1:2] <- NA
# Prepare data for imputation
ini <- mice(dat, maxit=0)
meth <- c("quadratic", "~I(x^2)", "")
pred <- ini$pred
pred[,"xx"] <- 0
# Impute data
imp <- mice(dat, meth=meth, pred=pred)
# Pool results
pool(with.mids(imp, lm(y~x+xx)))
# Plot results
stripplot(imp)
plot(dat$x, dat$xx, col="blue", xlab="x", ylab="xx")
points(complete(imp,1)$x[is.na(dat$x)], complete(imp,1)$xx[is.na(dat$x)], col="red")
Run the code above in your browser using DataLab