Error when I try to predict class probabilities in R - caret

耗尽温柔 提交于 2019-11-28 08:06:49
topepo

The answer is in bold at the top of your post =]

What are you modeling? Is it alchemy_category? The code only says formula and we can't see it.

When you ask for class probabilities, model predictions are a data frame with separate columns for each class/level. If alchemy_category doesn't have levels that are valid column names, data.frame converts then to valid names. That creates a problem because the code is looking for a specific name but the data frame as a different (but valid) name.

For example, if I had

> test <- factor(c("level1", "level 2")) 
> levels(test)
[1] "level 2" "level1" 
> make.names(levels(test))
[1] "level.2" "level1"

the code would be looking for "level 2" but there is only "level.2".

As stated above the class values must be factors and must be valid names. Another way to insure this is,

levels(all.dat$target) <- make.names(levels(factor(all.dat$target)))

I have read through the answers above while facing a similar problem. A formal solution is to do this on the train and test datasets. Make sure you include the response variable in the feature.names too.

feature.names=names(train)

for (f in feature.names) {
  if (class(train[[f]])=="factor") {
    levels <- unique(c(train[[f]]))
    train[[f]] <- factor(train[[f]],
                   labels=make.names(levels))
  }
}

This creates syntactically correct labels for all factors.

As per the above example, usually refactoring the outcome variable will fix the problem. It's better to change in the original dataset before partitioning into training and test datasets

levels <- unique(data$outcome) data$outcome <- factor(data$outcome, labels=make.names(levels))

As others pointed out earlier, this problem only occurs when classProbs=TRUE which causes the train function to generate additional statistics related to the outcome class

As @Sam Firke already pointed out in comments (but I overlooked it) levels TRUE/FALSE also don't work. So I converted them to yes/no.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!