# NOT RUN {
#Multiple imputation with missingness on a continuous variable.
#Randomly generated data with missingness in x2. The probability of x2
# being missing increases with with value of x1.
n <- 20
pmissing <- 0.10
x1 <- runif(n, min = -3, max = 3)
x2 <- x1^2 + rnorm(n, mean = 0, sd = 1)
error <- rnorm(n, mean = 0, sd = 1)
y <- x1 + x2 + error
f <- ecdf(x1)
x2 <- ifelse(runif(x2) < (f(x1) * 2 * pmissing), NA, x2)
dat <- data.frame(y, x1, x2)
#Create vector of SuperLearner method names
# Note: see SuperLearner::listWrappers() for a full list of methods
# available.
SL.lib <- c("SL.mean", "SL.glm")
#Run mice().
# Note 1: m >= 30 and maxit >= 10 are recommended outside of this
# toy example
# Note 2: a denser bandwidth grid is recommended, see default for bw
# argument for example.
imp.SL <- mice::mice(dat, m = 2, maxit = 2,
method = "SuperLearner",
print = TRUE, SL.library = SL.lib,
kernel = "gaussian",
bw = c(0.25, 1, 5))
# }
Run the code above in your browser using DataLab