I have a global environment with many dataframes which should be imputed with mice library(mice)
. In order to first calculate the prediction matrices I did (with the help of some other people) the following:
library(mice)
PredMatr= list()
Myls<-ls(sorted=F)[eapply(.GlobalEnv, class)=="data.frame"]
try (for (i in 1:length(Myls)) {
PredMatr[[i]]=quickpred(get(Myls[i]), mincor=.1)
})
This creates a very nice list with all the prediction matrices. The next step however is even a bit more complicated. I would like to do something similar to do the imputation. I want to loop through the dataframes like before. However now I would like the operation to be an imputation, referring to the predictor matrix from the previous list. I tried:
dfimpls= list()
try (for (i in 1:length(Myls)) {
dfimpls[[i]]=mice(get(Myls[i]), m=5, maxit = 5, method='cart',predictorMatrix=PredMatr[i])
})
The syntax is however incorrect, giving me the following error:
Error : predictorMatrix not a matrix
With:
dfimpls= list()
try (for (i in 1:length(Myls)) {
dfimpls[[i]]=mice(get(Myls[i]), m=5, maxit = 5, method='cart',predictorMatrix=as.matrix(PredMatr[i]))
})
I get:
Error : Missing row/column names in predictorMatrix