Search code examples
rplotrpartr-caret

How to make a tree plot in caret package?


I'm using caret package to model the data using rpart package.

library('caret')
data(iris)
formula <- as.formula(Species ~.)
t <- train(formula,iris,method = "rpart",cp=0.002,maxdepth=8)
plot(t)

As a result I get object 't' and I'm trying to plot this object to get tree plot. But the result look like that: enter image description here

Are there any way to make a tree plot from caret train object?


Solution

  • The object returned from caret::train() is a list. The element finalModel contains your model.

    Try this:

    plot(t$finalModel)
    text(t$finalModel)
    

    enter image description here