Ignore some categories for imputation #640
-
Dear MICE community, I would like to ask for help and advice. While trying to add constraints, I came across the discussion where Prof. Stef van Buuren recommended squeeze_vec() function here: #507 I tried the squeeze_vec() function and it didn't work with factor variables. ` mice.impute.School <- function(y, ry, x, ...) { The results are a bit disappointing: #One imputation with that function in the methods and there are NAs
1 2 3 4 #Using map function reduced the number in category 3 and 4 and there are NAs
Here is the code: #Loading the data Convert columns to factors using base Rcols<-c("gender","language","education","marital","exchProg","cctrain", "School") # Creates a vector of column names library(mice) imp<-mice(df,maxit = 0,print=F) # a dry run. predM<-imp$predictorMatrix # in the predictor matrix: columns are predictors, rows are predicted vars. predM[,c("exchProgDue", "sf23", "sf24", "sf25", "sf26", predM Setting a method for predicting each variable.meth<-imp$method meth[c("as1","as2","as3", Assign the custom method directly by column index # we will impute only 1, 2mice.impute.School <- function(y, ry, x, ...) { meth["School"] <- "School" imp_2 <- mice( imp_n1<-complete(imp_2,1) map(1:5, (x) complete(imp_2, x)) %>% |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I suggest you try the |
Beta Was this translation helpful? Give feedback.
I suggest you try the
exclude
argument frommice.impute.pmm()
. It specifies a vector of dependent values to exclude from the imputation model and the collection of donor values. Excluding the two impossible categories would then always impute one of the other two.