即使列中的所有值都相同,是否可以使用包 MICE 进行插补?然后它将仅使用该数字进行估算。
例子:
test<-data.frame(var1=c(2.3,2.3,2.3,2.3,2.3,NA),var2=c(5.3,5.6,5.9,6.4,4.5,NA))
miceImp<-mice(test)
testImp<-complete(miceImp)
仅估算 var2。我希望它也用 2.3 替换 var1 中的 NA。
您可以为此使用被动插补。有关完整说明,请参阅本文第 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
话虽如此,常数值在统计中往往没有多大用处,因此在插补之前删除任何常数变量可能更有效(因为插补是一个成本很高的过程)。