0

I want to generate descriptive statistics for different subgroups after multiple imputation with MICE.

I have a dataset that include a medicine and age, which I imputed.

id <- c(1,2,3,4,5,6,7,8,9,10)  
age <- c(60, 80, 70, NA, 49, 30, NA, 59, 79, NA)
 medication <- c("yes", "no") 
 dat <- data.frame(id, age, medication)
 imp_1 <- mice(dat, maxit = 0)
 meth <- imp_1$method
 pred <- imp_1$predictorMatrix
 imp <- mice(dat, method = meth, predictorMatrix = pred, maxit = 10, m = 5, seed = 2018)

So I imputed the NA in age, using information about id and medication use (of course, it is not about the quality of the imputation, but it is just to create an example).

I want to calculate how many users of thiazide diuretics I have in the group of people aged above 75 and below 75. I tried two codes:

 subgroup <- with(imp, expr= table(age>75), subset=(medication=="yes"))
 withPool_MI(subgroup)

 subgroup1 <- with(imp, expr= table(age>75, medication==1))
 withPool_MI(subgroup1)

However, both codes do not work unfortunately. So what I want is the following:

          0    1
age>75
age<75

I want this table filled, so the number of users and non-users of the medication in both age categories. Can someone help me?

(This is the first time I created a reproducible example, so if it is not correct or does not work, please let me know!)

slamballais
  • 3,161
  • 3
  • 18
  • 29
  • Welcome on SO. The example is fine, but there is a bit of information missing. First, `library(mice)` is required, which is an easy one because it is a rather common library. However, `withPool_MI`, what package is it from? It is always best to paste your final example into a freshly started R session, or use a `reprex` (easy to google). – Dieter Menne Oct 20 '18 at 18:26
  • Thank you for the tips. I am going to do this next time! (And withPool_MI is from miceadds) –  Oct 22 '18 at 08:18

2 Answers2

1

you need to use the complete() function of mice. for descriptive statistics I use dplyr and tidyr (both within the tidyverse package).

library(tidyverse)
library(mice)

complete(imp, 1) %>% 
  # generate age groups
  mutate(Age_Group = case_when(age >= 75 ~ "age > 75",
                               TRUE ~ "age <= 75")) %>% 
  # aggregate groups
  count(medication, Age_Group) %>% 
  # make the clean table
  spread(medication, n)

output is:

# A tibble: 2 x 3
  Age_Group    no   yes
  <chr>     <int> <int>
1 age <= 75     3     3
2 age > 75      2     2
Stephan
  • 2,056
  • 1
  • 9
  • 20
  • Thank you, this worked! However, if I have five datasets, I get 5 different tables right? Because in one dataset, age can be imputed as 76 and in the other as 74. Can I then just take the mean and combine tables ? –  Oct 22 '18 at 08:21
  • For this you need to define `set.seed()`. look at it's documentation or at https://stackoverflow.com/a/27292162/8598377 . Hope that answers your question? – Stephan Oct 22 '18 at 09:31
-1

I am dealing with the same issue, but if you use the complete function, then you will not generate the descriptives for all the datasets right? Because if I impute 5 times, all 5 are different. Of is there a way to combine those results?

  • Welcome to stackoverflow. We are not a discussion forum, but question and answer. If you are posting an answer (as you have done here) please ensure you are answering the question. – Simon.S.A. Oct 20 '18 at 21:45