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 :)
The easiest way to plot a decision tree in R is to use the prp() function from the rpart. plot package.
The caret package (short for Classification And REgression Training) contains functions to streamline the model training process for complex regression and classification problems.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With