I am using mice in R to impute missing data for an intervention study. The data I want to impute are continuous. I have two observation groups; 1= the intervention group, 2= the control group.
There are some variables that I only want mice to impute for the intervention group. For the control group, the NA's should be remaining.
Here is a toy version of my data set:
toy2 <- data.frame(group= c(1, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 1, 2),
PSS_t1=c(16, 15, 17, 22, 30, 12, 18, 19, 20, 14, 21, 23, 27, 20, 14),
CSQ_t2= c(8, 9, NA, 12, NA, 7, NA, 9, NA, NA, 10, 11, NA, 6, NA),
PSS_t3=c(15, 14, 15, 19, 22, 7, 19, 21, 14, 18, 12, 10, 22, 17, 14),
PSS_t4=c(1, 1, NA, 1, NA, 1, NA, 1, NA, NA, 1, 1, NA, 1, NA))
The scales CSQ_t2 and PSS_t4 have only been presented to the intervention group, so NA's for the control group are natural.
I thought about using post from mice, unfortunately it does not work:
posttoy2 <- mice(toy2, m = 1, maxit = 0)$post
posttoy2["CSQ_t2"] <- "ifdo(group==1)"
posttoy2["PSS_t4"] <- "ifdo(group==1)"
imptoy2 <- mice(toy2, m = 1, post = posttoy2, seed = 555,
print = TRUE)
Here is the output I get:
> toy2 <- data.frame(group= c(1, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 1, 2),
+ PSS_t1=c(16, 15, 17, 22, 30, 12, 18, 19, 20, 14, 21, 23, 27, 20, 14),
+ CSQ_t2= c(8, 9, NA, 12, NA, 7, NA, 9, NA, NA, 10, 11, NA, 6, NA),
+ PSS_t3=c(15, 14, 15, 19, 22, 7, 19, 21, 14, 18, 12, 10, 22, 17, 14),
+ PSS_t4=c(1, 1, NA, 1, NA, 1, NA, 1, NA, NA, 1, 1, NA, 1, NA))
> posttoy2 <- mice(toy2, m = 1, maxit = 0)$post
Warning message:
Number of logged events: 1
> posttoy2["CSQ_t2"] <- "ifdo(group_unguided==1)"
> posttoy2["CSQ_t2"] <- "ifdo(group==1)"
> posttoy2["PSS_t4"] <- "ifdo(group==1)"
> posttoy2
group PSS_t1 CSQ_t2 PSS_t3 PSS_t4
"" "" "ifdo(group==1)" "" "ifdo(group==1)"
> imptoy2 <- mice(toy2, m = 1, post = posttoy2, seed = 555,
+ print = TRUE)
iter imp variable
1 1 CSQ_t2Function ifdo() not yet implemented.
2 1 CSQ_t2Function ifdo() not yet implemented.
3 1 CSQ_t2Function ifdo() not yet implemented.
4 1 CSQ_t2Function ifdo() not yet implemented.
5 1 CSQ_t2Function ifdo() not yet implemented.
Warning message:
Number of logged events: 6
Has someone any idea how I can realize this conditional imputation?
Many thanks in advance for your ideas and help! :-)