# NOT RUN {
###################################
# simulate some data
# two-level regression model with fixed slope
# number of groups
G <- 250
# number of persons
n <- 20
# regression parameter
beta <- .3
# intraclass correlation
rho <- .30
# correlation with missing response
rho.miss <- .10
# missing proportion
missrate <- .50
y1 <- rep( rnorm( G , sd = sqrt( rho ) ) , each=n ) + rnorm(G*n , sd = sqrt( 1 - rho ))
x <- rnorm( G*n )
y <- y1 + beta * x
dfr0 <- dfr <- data.frame( "group" = rep(1:G , each=n ) , "x" = x , "y" = y )
dfr[ rho.miss * x + rnorm( G*n , sd = sqrt( 1 - rho.miss ) ) < qnorm( missrate ) , "y" ] <- NA
#.....
# empty imputation in mice
imp0 <- mice( as.matrix(dfr) , maxit=0 )
predM <- imp0$predictorMatrix
impM <- imp0$method
#...
# specify predictor matrix and imputationMethod
predM1 <- predM
predM1["y","group"] <- -2
predM1["y","x"] <- 1 # fixed x effects imputation
impM1 <- impM
impM1["y"] <- "2l.pan"
# multilevel imputation
imp1 <- mice( as.matrix( dfr ) , m = 1 , predictorMatrix = predM1 ,
imputationMethod = impM1 , maxit=1 )
# multilevel analysis
library(lme4)
mod <- lmer( y ~ ( 1 + x | group) + x , data = complete(imp1) )
summary(mod)
############################################
# Examples of predictorMatrix specification
# random x effects
# predM1["y","x"] <- 2
# fixed x effects and group mean of x
# predM1["y","x"] <- 3
# random x effects and group mean of x
# predM1["y","x"] <- 4
# }
Run the code above in your browser using DataLab