Home > OS >  Calculate averages of imputed data in mice
Calculate averages of imputed data in mice

Time:06-11

I would like to calculate the individual-level averages of the imputed datasets in Mice (for presentational purposes, not for analysis). For example, if I have the following dataset with missing.

Id V1 V2 V3
1 10 NA 9
2 12 8 NA
3 NA 7 11

And 2 imputed datasets like these (imputed values marked with *)

Id V1 V2 V3
1 10 10* 9
2 12 8 12*
3 11* 7 11
Id V1 V2 V3
1 10 9* 9
2 12 8 10*
3 14* 7 11

I would like the resulting dataset look like this, with * numbers being the averages of the imputed missing data.

Id V1 V2 V3
1 10 9.5* 9
2 12 8 11*
3 12.5* 7 11

I can't figure out how to do this in an effective way in R.

CodePudding user response:

You can dirrectly replace the NA values:

 df[is.na(df)] <- ((impute_df1   imput_df2)/2)[is.na(df)]

CodePudding user response:

I brute-forced it.

averages <- complete(imp, 1)
for(m in 2:imp$m){
  averages <- averages   complete(imp, m)
}
averages <- averages / imp$m
  • Related