I am trying to impose a predictor matrix of my own to the mice command of the mice package in R, to control which variables are imputed and based on which variables they are imputed. However, the command does not work. I have 33 variables and I build the matrix this way:
Pred_Matrix<-(1-diag(1,33))
Pred_Matrix[1, ] <- 0 #so that this variable is not imputed
Pred_Matrix[, 1] <- 0 #so that this variable does not impute
Pred_Matrix[6, ] <- 0 # so that this variable is not imputed
Pred_Matrix[, 6] <- 0 # so that this variable does not impute
...
And I do the same to other 5 variables (setting both row and column to 0 so that they are not imputed and also not used to impute the other ones). This happens, therefore, in 7 variables in total.
I then start the imputation with
imp_dataset<-mice(MyDataset, m = 10, maxit = 10, pred=Pred_Matrix)
The error message I get after the imputation starts is:
iter imp variable
1 1 Ed_level Occ_levelError in model.frame.default(formula = formula(xy), data = xy[ry, ], weights = w[ry]) :
variable lengths differ (found for '(weights)')
Where Ed_level and Occ_level are two variables - and Occ_level is one of the seven whose row and column had been set to all 0s (maybe not chance?).
I cannot make the example more reproducible because the dataset is huge. But all my experiments on smaller datasets work; it's because of the matrix, because if I don't use my own matrix everything works. I could not figure out what could possibly be wrong. Is there any other requirement a predictor matrix must comply to apart from having both dimensions equal to the number of variables, only including 0s and 1s, and having a diagonal=0? I cannot find any in the documentation of mice. Thank you in advance!