Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 803
Jot eN Avatar asked Sep 10 '25 03:09

Jot eN


2 Answers

nicer looking treeplot:

library(rattle)
fancyRpartPlot(t$finalModel)

enter image description here

like image 82
Jot eN Avatar answered Sep 12 '25 18:09

Jot eN


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

like image 26
Andrie Avatar answered Sep 12 '25 17:09

Andrie