R SVM 为缺失数据的预测返回 NA [英] R SVM return NA for predictions with missing data

查看:34
本文介绍了R SVM 为缺失数据的预测返回 NA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 e1071 包中经过训练的 SVM 进行预测,但我的数据包含一些缺失值 (NA).

I am attempting to make predictions using a trained SVM from package e1071 but my data contains some missing values (NA).

当该实例有任何缺失值时,我希望返回的预测为 NA.我尝试使用 na.action = na.pass 如下,但它给了我一个错误名称错误(ret2)<- rowns:'names'属性 [150] 必须与向量 [149] 的长度相同".

I would like the returned predictions to be NA when that instance has any missing values. I tried to use na.action = na.pass as below but it gives me an error "Error in names(ret2) <- rowns : 'names' attribute [150] must be the same length as the vector [149]".

如果我使用 na.omit 那么我可以得到没有数据缺失实例的预测.如何获得包括 NA 在内的预测?

If I use na.omit then I can get predictions without instances with missing data. How can I get predictions including NAs?

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

推荐答案

如果您熟悉 caret 包,在 按标签相似性聚类的模型" 在那里您可以找到一个 csv,其中包含它们用于对算法进行分组的数据.

if you are familiar with the caret package, where you can use 233 different types of models to fit (Including SVM from package e1071), in the section called "models clustered by tag similarity" there you can find a csv with the data they used to group the algorithms.

那里有一列叫做处理缺失的预测数据,它告诉你哪些算法可以做你想做的事.不幸的是,SVM 不包括在那里,但这些算法是:

There is a column there called Handle Missing Predictor Data, which tells you which algorithms can do what you want. Unfortunately SVM is not included there, but these algorithms are:

  • 提升分类树 (ada)
  • 袋装 AdaBoost (AdaBag)
  • AdaBoost.M1 (AdaBoost.M1)
  • C5.0 (C5.0)
  • 对成本敏感的 C5.0 (C5.0Cost)
  • 单个 C5.0 规则集 (C5.0Rules)
  • 单个 C5.0 树 (C5.0Tree)
  • 购物车(rpart)
  • 购物车 (rpart1SE)
  • 购物车 (rpart2)
  • 对成本敏感的购物车 (rpartCost)
  • CART 或有序响应 (rpartScore)

如果你仍然坚持使用 SVM,你可以使用 preProccess 函数来自同一个包,它应该允许您预测所有观察结果.

If you still insist on using SVM, you could use the knnImpute option in the preProccess function from the same package, that should allow you to predict for all your observations.

这篇关于R SVM 为缺失数据的预测返回 NA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆