# NOT RUN {
#############################################################################
# EXAMPLE 1: Cluster-specific imputation for some variables
#############################################################################
data( data.ma01 )
dat <- data.ma01
# use sub-dataset
dat <- dat[ dat$idschool <= 1006 , ]
V <- ncol(dat)
# create initial predictor matrix and imputation methods
predictorMatrix <- matrix( 1 , nrow=V , ncol=V)
diag(predictorMatrix) <- 0
rownames(predictorMatrix) <- colnames(predictorMatrix) <- colnames(dat)
predictorMatrix[ , c("idstud", "studwgt","urban" ) ] <- 0
imputationMethod <- rep("norm" , V)
names(imputationMethod) <- colnames(dat)
#** groupwise imputation of variable books
imputationMethod["books"] <- "bygroup"
# specify name of the grouping variable ('idschool') and imputation method ('norm')
group <- list( "books" = "idschool" )
imputationFunction <- list("books" = "norm" )
#** conduct multiple imputation in mice
imp <- mice::mice( dat, imputationMethod=imputationMethod, predictorMatrix=predictorMatrix,
m=1 , maxit=1 , group = group , imputationFunction = imputationFunction )
# }
Run the code above in your browser using DataLab