# Can I use the 'mice' library in R to impute missing data separately for each of two groups in the same dataset?

I would like to use the 'mice' library in R to impute data from a clinical trial, in which I have two groups (i.e. var="group" [0=control; 1=intervention]). I want to impute the missing data separately for each group, using the same imputation model. So, I do not want to use group, as a categorical predictor (0;1) but actually perform separate imputations, ending up with m datasets and each of those containing data from both groups.

I am more familar with stata, where after defining the imputation model I would simply add "by (group)".

Is there an efficient way to do so with 'mice'? Thank you.

As far as I could find out, it is not possible to impute separately for two groups within one data set in 'mice'. One way to do this would be to create two data sets (one per group), impute separately, and then merge the imputed data sets for the two groups.

# create two groups
df_int <- df %>%
filter(group == 1) # intervention group

df_cont <- df %>%
filter(group == 0) # control group

# .... perform the imputation for both groups with 'mice'....

# merge imputed dfs
df_imp <- rbind(df_imp_int, df_imp_con)


You could achieve this by specifying an imputation model in which treatment group interacts with all other predictors, but Spyros's solution is more efficient.