您可以使用被动插补 . 有关完整说明,请参见this article第25页的第3.4节 . 应用于常量变量时,此处的目标是将任何常量变量 x 的插补方法设置为 x 的常量值 . 如果 x 的常量值为 y ,则 x 的插补方法应为 "~I(y)" .
test = data.frame(
var1=c(2.3,2.3,2.3,2.3,2.3,NA,2.3),
var2=c(5.3,5.6,5.9,6.4,4.5,5.1,NA),
var3=c(NA,1:6))
cVars = which(sapply(test,sd,na.rm=T)==0) #determine which vars are constant (props to SimonG)
allMeans = colMeans(test,na.rm=T) #get the column means
miceImp.ini = mice(test,maxit=0,print=F) #initial mids object with no imputations
meth = miceImp.ini$method #extract the imputation method vector
meth[cVars] = paste0("~I(",allMeans[cVars],")") #set the imputation method to be a constant (the current column mean)
miceImp = mice(test,method=meth) #run the imputation with the user defined imputation methods
testImp = complete(miceImp) #extract an imputedly complete dataset
View(testImp) #take a look at it
1 回答
您可以使用被动插补 . 有关完整说明,请参见this article第25页的第3.4节 . 应用于常量变量时,此处的目标是将任何常量变量
x
的插补方法设置为x
的常量值 . 如果x
的常量值为y
,则x
的插补方法应为"~I(y)"
.总而言之,常量值在统计中往往不是很有用,因此在插补之前删除任何常量变量可能更有效(因为插补是一个非常昂贵的过程) .