Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Combine imputed data by group in r using mice

$
0
0

my question is a follow-up to this question on imputation by group using "mice": multiple imputation and multigroup SEM in R

The code in the answer works fine as far as the imputation part goes. But afterwards I am left with a list of actually complete data but more than one set. The sample looks as follows:

'Set up data frame'
df.g1<-data.frame(ID=rep("A",5),x1=floor(runif(5,0,2)),x2=floor(runif(5,10,20)),x3=floor(runif(5,100,150)))
df.g2<-data.frame(ID=rep("B",5),x1=floor(runif(5,0,2)),x2=floor(runif(5,25,50)),x3=floor(runif(5,200,250)))
df.g3<-data.frame(ID=rep("C",5),x1=floor(runif(5,4,5)),x2=floor(runif(5,75,99)),x3=floor(runif(5,500,550)))
df<-rbind(df.g1,df.g2,df.g3)

'Introduce NAs'

df$x1[rbinom(15,1,0.1)==1]<-NA
df$x2[rbinom(15,1,0.1)==1]<-NA
df$x3[rbinom(15,1,0.1)==1]<-NA
df

'Impute values by group:'

df.clean<-lapply(split(df,df$ID), function(x) mice::complete(mice(df,m=5)))
df.clean

As you can see, df.clean is a list of 3. One element per group. But each element containing a complete data set I am looking for.

The original answer suggests to rbind() the obtained data in df.clean which leaves me with a new data set with 45 (3x the original size) observations. Here is the original code for the last step:

imputed.both <- do.call(args = df.clean, what = rbind)

Which data is the "right" one? And why the last step?

Thanks a bunch!


Viewing all articles
Browse latest Browse all 201839

Trending Articles