```
library(data.table)
library(magrittr)
library(ggplot2)
<- function(data, argset){
sim_function <- data.table(id = 1:argset$n)
data
:= rbinom(.N, 1, 0.5)]
data[, is_male := rbinom(.N, 1, 0.5)]
data[, is_smoker := -1 + 0 * is_male + 0 * is_smoker + 5 * is_male * is_smoker + rnorm(.N, sd = 2)]
data[, lung_function
<- copy(data)
data_missing ==1 & runif(.N)<0.7, lung_function := NA]
data_missing[is_male==1 & runif(.N)<0.7, lung_function := NA]
data_missing[is_smoker
# complete case analysis
<- coef(summary(lm(lung_function ~ is_male*is_smoker, data = data_missing)))[,1]
coef_complete_case
# we impute the data blindly
<- mice::mice(data_missing, print=F, m = 20)
data_imputed <- with(data_imputed, lm(lung_function ~ is_male*is_smoker))
fit_imputed <- mice::pool(fit_imputed)$pooled$estimate
coef_imputed_blindly
# we impute the data separately for males and females
<- mice::mice(data_missing[is_male==1], print=F, m = 20)
data_imputed_male <- mice::mice(data_missing[is_male==0], print=F, m = 20)
data_imputed_female <- mice:::rbind.mids(data_imputed_male, data_imputed_female)
data_imputed_sex_specific <- with(data_imputed_sex_specific, lm(lung_function ~ is_male*is_smoker))
fit_imputed <- mice::pool(fit_imputed)$pooled$estimate
coef_imputed_separately
<- data.table(
retval var = names(coef_complete_case),
coef_complete_case,
coef_imputed_blindly,
coef_imputed_separately,n = argset$n
)return(retval)
}
<- plnr::Plan$new()
p for(n in c(100, 500, 1000, 2000, 4000)) for(i in 1:100){
$add_argset(
pname = uuid::UUIDgenerate(),
n=n,
i=i
)
}$apply_action_fn_to_all_argsets(sim_function)
pset.seed(1234)
<- p$run_all()
results <- rbindlist(results)
results
== "is_male:is_smoker", .(
results[var coef_complete_case = round(mean(coef_complete_case), 2),
coef_imputed_blindly = round(mean(coef_imputed_blindly), 2),
coef_imputed_separately = round(mean(coef_imputed_separately), 2)
=.(n)] %>%
), keybyprint()
```

```
n coef_complete_case coef_imputed_blindly coef_imputed_separately
1: 100 5.06 1.33 2.60
2: 500 4.97 3.56 4.89
3: 1000 5.10 4.49 5.12
4: 2000 5.03 4.97 5.09
5: 4000 5.05 5.09 5.09
```