'Impute based on condition in mice

I am using mice for imputation and want to impute data in a column based on the condition of another column. Consider the following data:

d <- data.frame(X1 = c(0, 1, 2, 3),
                X2 = c(NA, 5, NA, 7))

  X1 X2
1  0 NA
2  1  5
3  2 NA
4  3  7

I want to impute missing values in X2 where X1 > 0

  X1 X2
1  0 NA <-- do not impute
2  1  5
3  2 NA <-- impute
4  3  7


Solution 1:[1]

You can just use the where argument in the mice function. It should be a logical matrix of same dimensions as your data frame and by default it is TRUE for every value that is NA in the data frame. In your case, you want to make some changes to that matrix.

Given, that missings in any other variable that you did not mention should be imputed without conditions, you can use this:

d.where <- is.na(d)
d.where[d$X1 <= 0, names(d)=="X2"] <- FALSE

And then use this matrix in your call to mice::mice(...):

d.mids <- mice(data=d, where=d.where)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 benimwolfspelz