I am trying to impute a matrix, of which a subset of the variables are a binary matrix that I would like to summarise into one by means of MCA. This should be achieved by writing my own post-processing function and call it within the MICE algorithm.
I imagine the code to be something like the one below.
a1 <- NA
a2 <- c(1,NA, 1, 1, 1)
a3 <- c(0,0, 1, 1, NA)
a4 <- c(0,0, NA, 0, 1)
a_PC <- NA # to be summarised by MCA
b1 <- c(0,0, 0, 0, 1)
b2 <- c(1,0, 1, 1, 1)
b3 <- c(0,1, 1, 1, 1)
b4 <- c(1,1, 1, 1, 1)
b_PC <- NA
c <- c(0, 4, NA, 3, 7) # further variable(s) to be imputed using mice()
matrix <- data.frame(a1,a2,a3,a4,a_PC,b1,b2,b3,b4,b_PC,c)
imp0 <- mice(matrix, maxit = 0, print = FALSE)
pred <- imp0$predictorMatrix
meth <- imp0$meth
# defining the MCA function that is supposed to be my post processing algorithm
post_MCA <- function(data) {
res.mca <- MCA(data, graph=FALSE)
data$MCA_1st_Coord <- res.mca$ind$coord[,1]
return(data)
}
# the code for passive imputation should be something like
meth["a_PC"] <- "~MCA(matrix[c('a1','a2','a3','a4'),], graph=FALSE)$ind$coord[,1]"
meth["b_PC"] <- "~MCA(matrix[c('b1','b2','b3','b4'),], graph=FALSE)$ind$coord[,1]"
pred[c('a1','a2','a3','a4') "a_PC"] <- 0
post<- imp0$post
post["a_PC"] <- "imp[[j]][i] <- post_MCA(p$data[c('a1','a2','a3','a4'),]"
post["b_PC"] <- "imp[[j]][i] <- post_MCA(p$data[c('b1','b2','b3','b4'),]"
# then impute:
imp1 <- mice(matrix, m = 5, meth = meth, pred = pred, post = post, maxit = 5, print = FALSE)