我使用此代码对carData库中的Davis数据集进行了5次交叉验证。
install.packages("caret")
library(caret)
trainControl<-trainControl(method="cv",number=5)
lm<-train(weight~height+repht+repwt,Davis,method="lm",trControl=trainControl)
lm
运行此操作时,我得到了一个错误,即缺少权重值。这是错误消息:
na.fail.default中的错误(list(weight=c(77L,58L,53L,68L,59L,76L(:对象中缺少值
如果能就如何解决这个问题提出任何建议,我将不胜感激。提前感谢!
预测器中缺少错误,例如:
library(caret)
data = mtcars
data$mpg[c(3,6,9)]<-NA
trainControl<-trainControl(method="cv",number=5)
fit<-train(mpg~cyl+hp,data,method="lm",trControl=trainControl)
Error in na.fail.default(list(mpg = c(21, 21, NA, 21.4, 18.7, NA, 14.3, :
missing values in object
使用complete.cases获取包含完整观察的数据
complete.obs = complete.cases(data[,c("mpg","cyl","hp")])
data = data[complete.obs,]
fit<-train(mpg~cyl+hp,data,method="lm",trControl=trainControl)
在您的情况下,它应该是:
complete.obs = Davis[,c("weight","height","repht","repwt")]