I have a dataset like this:
d <- data.frame(X1 = c(1, 1, NA, NA, 0, NA, NA, 1, 0),
X2 = c(NA, 0, NA, NA, 0, NA, NA, 1, 0))
X1 X2
1 1 NA
2 1 0
3 NA NA
4 NA NA
5 0 0
6 NA NA
7 NA NA
8 1 1
9 0 0
I want to perform mice
imputation with the following condition based on the future imputed values of X1:
How can I write this condition? The base code for my imputation is:
library(mice)
imp <- mice(d, seed=123, m=5, maxit=10)
I am having troubles in understand. Thank you
We can use a passive imputation approach as described by Heymans and Eekhout (2019) by specifying the imputation method. If X1
is 0, X2
will always be 0, but if X1
is 1
, then the usual pmm
method (or whatever method you specify) applies.
library(mice)
set.seed(123)
d <- data.frame(X1 = c(1, 1, NA, NA, 0, NA, NA, 1, 0),
X2 = c(NA, 0, NA, NA, 0, NA, NA, 1, 0))
fakeimp <- mice(d, seed = 123, maxit = 0, printFlag = FALSE)
meth <- fakeimp$method
meth["X2"] <- "~I((ifelse(X1 == 0, 0, X2)))"
imp <- mice(d, seed = 123, m = 5, maxit = 10, method = meth, printFlag = FALSE)
full <- complete(imp, action = "long", include = FALSE)
head(full)
#> .imp .id X1 X2
#> 1 1 1 1 1
#> 2 1 2 1 0
#> 3 1 3 1 0
#> 4 1 4 0 0
#> 5 1 5 0 0
#> 6 1 6 0 0