I have been testing conditional trees and random forests with caret, and I've noticed it does something weird with factors.

So, for example, a ctree using the base dataset chickwts:

# Via 'party'
ctreeNC = ctree(weight ~ feed, data=chickwts)
plot(ctreeNC, type="simple")

# Via 'caret'
ctreeCARET <- train(weight ~ feed, data = chickwts, method = "ctree")
plot(ctreeCARET$finalModel, type = "simple")

And with random forest:

# Via 'randomForest'
rfNOCARET <- randomForest(weight ~ feed, data=chickwts)

feed        242589

# Via 'caret'
rfCARET <- train(weight ~ feed, method = "rf", data = chickwts)

feedhorsebean     120992.97
feedlinseed        50673.75
feedmeatmeal       15688.51
feedsoybean        32652.89
feedsunflower      23308.30

I read that train only accepted numerical values (in 2008), but I'm not sure if this has changed by now. Any insights?


The difference is how the formula method for each of these functions handles factors. The individual tree methods you mention do not convert factors to dummy variables. This is not the traditional way formulas work in R but it makes a lot of sense for these mods (and a few others).

train is deigned to be more general and train.formula will convert them.

You can use the non-formula interface to train with these models and keep the factors intact.

  • 9
    $\begingroup$ I just realised who you are. Keep being awesome! $\endgroup$ – foto51 Jan 30 '15 at 18:51
  • $\begingroup$ Apart from the tree methods mentioned in the question, which other caret methods does it make sense to not convert factors to dummy variables for? $\endgroup$ – makeyourownmaker Apr 22 '19 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.