Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Leftover NAs after imputing using mice

Tags:

r

r-mice

What is going on in the following?

#create some data
library(data.table)
library(mice)
myData = data.table(invisible.covariate=rnorm(10),
         visible.covariate=rnorm(10),
         category=factor(sample(1:3,10, replace=TRUE)),
         treatment=sample(0:1,10, replace=TRUE))
myData[,outcome:=invisible.covariate+visible.covariate+treatment*as.integer(category)]
myData[,invisible.covariate:=NULL]    
myData[treatment == 0,untreated.outcome:=outcome]
myData[treatment == 1,treated.outcome:=outcome]

#impute missing values
myPredictors = matrix(0,ncol(myData),ncol(myData))
myPredictors[5,] = c(1,1,0,0,0,0)
myPredictors[6,] = c(1,1,0,0,0,0)
myImp = mice(myData,predictorMatrix=myPredictors)

#Now look at the "complete" data
completeData = data.table(complete(myImp,0))
print(nrow(completeData[is.na(untreated.outcome)]))

The result should be 0, if mice had successfully replaced all the NA values. But it's not. What am I doing wrong?

like image 492
Jameson Quinn Avatar asked Aug 29 '26 11:08

Jameson Quinn


1 Answers

The second argument in complete is intended to something other than zero (which returns the original, incomplete data), e.g., a scalar between 1 and the number of imputations generated. It also accepts some character inputs (see the documentation for details).

Try this:

completeData = data.table(complete(myImp, 1))

Compare:

> completeData = data.table(complete(myImp,0))
> print(nrow(completeData[is.na(untreated.outcome)]))
[1] 5
> completeData = data.table(complete(myImp,1))
> print(nrow(completeData[is.na(untreated.outcome)]))
[1] 0

Cheers!

like image 92
SimonG Avatar answered Sep 01 '26 05:09

SimonG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!