Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot decision tree in R (Caret)

I have trained a dataset with rf method. For example:

ctrl <- trainControl(
                     method = "LGOCV", 
                     repeats = 3, 
                     savePred=TRUE,
                     verboseIter = TRUE,
                     preProcOptions = list(thresh = 0.95)
                    )

preProcessInTrain<-c("center", "scale")
metric_used<-"Accuracy"
model <- train(
               Output ~ ., data = training,
               method = "rf",
               trControl = ctrl,
               metric=metric_used,
               tuneLength = 10,
               preProc = preProcessInTrain
              )

After thath, I want to plot the decission tree, but when I wirte plot(model), I get this: plot(model).

If I write plot(model$finalModel), I get this : plot(model$finalModel)

I would like to plot the decission tree...

How can I do that? Thanks :)

like image 429
Alonso Albaladejo Rojo Avatar asked Sep 22 '16 10:09

Alonso Albaladejo Rojo


People also ask

How do you plot a decision tree in R?

The easiest way to plot a decision tree in R is to use the prp() function from the rpart. plot package.

What is the caret package in R?

The caret package (short for Classification And REgression Training) contains functions to streamline the model training process for complex regression and classification problems.

Does rpart use Gini?

In the rpart algorithm the computational metric is the Gini coefficient. At each node, rpart minimizes the Gini coefficient and thus splits the data into purer class subsets with the class leaf nodes at the bottom of the decision tree.

Does rpart do cross validation?

rpart() uses k-fold cross validation to validate the optimal cost complexity parameter cp and in tree(), it is not possible to specify the value of cp.


1 Answers

The model you are using is random forest, which is not a single decision tree, but an ensemble of a large number of trees. Plotting the final model will plot the error rates on the training and test datasets as # of trees are increased, something like the following.

enter image description here

If you want a single decision tree instead, you may like to train a CART model like the following:

model <- train(
  Species ~ ., data = training,
  method = "rpart",
  trControl = ctrl,
  metric=metric_used,
  tuneLength = 10,
  preProc = preProcessInTrain
)
library(rpart.plot)
rpart.plot(model$finalModel)

Now plotting the final model as above will plot the decision tree for you.

like image 181
Sandipan Dey Avatar answered Sep 20 '22 16:09

Sandipan Dey