[英]Error in `contrasts' Error
I have trained a model and I am attempting to use the predict
function but it returns the following error. 我已经训练了一个模型,我试图使用predict
函数,但它返回以下错误。
Error in
contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) :contrasts<-
误差contrasts<-
(*tmp*
,value = contr.funs [1 + isOF [nn]]):
contrasts can be applied only to factors with 2 or more levels 对比度仅适用于具有2级或更多级别的因素
There are several questions in SO and CrossValidated about this, and from what I interpret this error to be, is one factor in my model has only one level. 在SO和CrossValidated中有几个问题,从我解释这个错误,我的模型中的一个因素只有一个级别。
This is a pretty simple model, with one continuous variable (driveTime) and one factor variable which has 3 levels 这是一个非常简单的模型,有一个连续变量(driveTime)和一个有3个级别的因子变量
driveTime Market.y transfer
Min. : 5.100 Dallas :10 Min. :-11.205
1st Qu.: 6.192 McAllen: 6 1st Qu.: 3.575
Median : 7.833 Tulsa : 3 Median : 7.843
Mean : 8.727 Mean : 8.883
3rd Qu.:10.725 3rd Qu.: 15.608
Max. :14.350 Max. : 30.643
When I use the predict function to determine an outcome on an unseen sample 当我使用预测函数来确定未见样本的结果时
newDriveTime <- data.frame(driveTime = 8,Market.y = as.factor("Dallas"))
predict(bestMod_Rescaled, newDriveTime)
I get the following error 我收到以下错误
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Here is more of my workflow 这是我的工作流程的更多内容
tc <- tune.control(cross = 10, fix = 8/10)
tuneResult_Rescaled <- tune(svm,data = finalSubset,
transfer~ driveTime + Market.y,
ranges = list(epsilon = seq(0.1,.5,0.1),
cost = seq(8,10,.1)), tunecontrol=tc)
summary(tuneResult_Rescaled)
bestMod_Rescaled <- tuneResult_Rescaled$best.model
I think you have to provide factor levels in the trainings data to the test set as well. 我认为你必须将训练数据中的因子水平提供给测试集。 Something like the following should work. 像下面这样的东西应该工作。
newDriveTime <- data.frame(driveTime = 8,
Market.y = factor("Dallas", levels(finalSubset$Market.y)))
predict(bestMod_Rescaled, newDriveTime)
In R, factor are saved as integers with names / labels. 在R中,因子被保存为带有名称/标签的整数。 If you have two factor vectors with different number of levels, just by looking at the labels, one can not be sure which labels are the corresponding levels in the two vectors. 如果您有两个具有不同级别的因子向量,只需查看标签,就无法确定哪些标签是两个向量中的相应级别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.