使用老鼠进行输入后剩余的NAs

时间:2014-08-24 14:17:35

标签: r r-mice

以下是怎么回事?

#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)]))

如果小鼠已成功替换所有NA值,则结果应为0。但事实并非如此。我做错了什么?

1 个答案:

答案 0 :(得分:1)

complete中的第二个参数旨在表示零以外的其他参数(返回原始的不完整数据),例如1和生成的插补数之间的标量。它还接受一些字符输入(有关详细信息,请参阅文档)。

试试这个:

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

比较

> 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

干杯!