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

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


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.