【发布时间】:2017-07-09 03:41:19
【问题描述】:
我正在尝试使用包 e1071 中经过训练的 SVM 进行预测,但我的数据包含一些缺失值 (NA)。
当该实例有任何缺失值时,我希望返回的预测为 NA。我尝试使用 na.action = na.pass 如下,但它给了我一个错误“名称错误(ret2)
如果我使用 na.omit,那么我可以在没有数据缺失实例的情况下获得预测。 如何获得包括 NA 在内的预测?
library(e1071)
model <- svm(Species ~ ., data = iris)
print(length(predict(model, iris)))
tmp <- iris
tmp[1, "Sepal.Length"] <- NA
print(length(predict(model, tmp, na.action = na.pass)))
【问题讨论】:
-
您可以将所有有效案例分配回
tmp集合中的预测变量 -tmp[complete.cases(tmp), "predict"] <- predict(model, newdata=tmp[complete.cases(tmp),])或等效项。 -
谢谢,太好了。鉴于我必须使用 SVM 并且想要 NA 而不是插补,这是要走的路。