## Not run:
# #############################################################################
# # EXAMPLE 1: Sequential hierarchical imputation for data.ma05 dataset
# #############################################################################
#
# data(data.ma05)
# dat <- data.ma05
#
# # empty imputation
# imp0 <- mice::mice( dat , m=0 , maxit=0 )
# summary(imp0)
#
# # define predictor matrix
# predM <- imp0$pred
# # exclude student IDs
# predM[ , "idstud"] <- 0
# # define idclass as the cluster variable (type=-2)
# predM[ , "idclass" ] <- -2
#
# # initialize with norm method
# impMethod <- rep( "norm" , length(imp0$method) )
# names(impMethod) <- names( imp0$method )
# impMethod[ c("idstud","idclass")] <- ""
#
# #*****
# # STUDENT LEVEL (Level 1)
#
# # Use a random slope model for Dscore and Mscore as the imputation method.
# # Here, variance homogeneity of residuals is assumed (contrary to
# # the 2l.norm imputation method in the mice package).
# impMethod[ c("Dscore" , "Mscore") ] <- "2l.pan"
# predM[ c("Dscore","Mscore") , "misei" ] <- 2 # random slopes on 'misei'
# predM[ , "idclass" ] <- -2
#
# # For imputing 'manote' and 'denote' use contextual effects (i.e. cluszer means)
# # of variables 'misei' and 'migrant'
# impMethod[ c("denote" , "manote") ] <- "2l.contextual.pmm"
# predM[ c("denote" , "manote") , c("misei","migrant")] <- 2
#
# # Use no cluster variable 'idclass' for imputation of 'misei'
# impMethod[ "misei"] <- "norm"
# predM[ "misei" , "idclass"] <- 0 # use no multilevel imputation model
#
# # Variable migrant: contextual effects of Dscore and misei
# impMethod[ "migrant"] <- "2l.contextual.pmm"
# predM[ "migrant" , c("Dscore" , "misei" ) ] <- 2
# predM[ "migrant" , "idclass" ] <- -2
#
# #****
# # CLASS LEVEL (Level 2)
# # impute 'sprengel' and 'groesse' at the level of classes
# impMethod[ "sprengel"] <- "2lonly.pmm"
# impMethod[ "groesse"] <- "2lonly.norm"
# predM[ c("sprengel","groesse") , "idclass" ] <- -2
#
# # do imputation
# imp <- mice::mice( dat , predictorMatrix = predM , m = 3 , maxit = 4 ,
# imputationMethod = impMethod , paniter=100)
# summary(imp)
# ## End(Not run)
Run the code above in your browser using DataLab