简体   繁体   English

“对比”错误出错

[英]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. SOCrossValidated中有几个问题,从我解释这个错误,我的模型中的一个因素只有一个级别。

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.

相关问题 对比错误,监督分类 - Error in contrasts, supervised classification R中的lm:“对比”错误的解决方法 - lm in R: Workaround for 'contrasts' error 在 R 中定义线性 model 时出现对比错误 - Error in contrasts when defining a linear model in R 运行逻辑回归时的对比度错误 - Contrasts error when running logistic regression `对比<-`(`* tmp *`,value = contr.funs [1 + isOF [nn]])出错:对比只能应用于2级或更多级别的因子 - Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels `contrasts&lt;-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) 中的错误:对比只能应用于具有 2 个或更多级别的因素 - Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]): contrasts can be applied only to factors with 2 or more levels R:将线性模型与`lm`拟合时的对比误差 - R: Error in contrasts when fitting linear models with `lm` 对比中的错误:按因子分组,并且`formula()`中的减号运算符停止工作 - Error in contrasts: group by factor and the minus operator in `formula()` stops working 重复测量方差分析误差:对比仅适用于因子 - Repeated measures ANOVA Error: contrasts only apply to factors R - geeglm 错误:对比只能应用于具有 2 个或更多级别的因子 - R - geeglm Error: contrasts can be applied only to factors with 2 or more levels
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM