3) Recalling a rpart() Decision Tree.

This Blog entry is from the Probability and Trees section in Learn R.

As with regression and most of the predictive analytics tools presented in this document, the predict() function can take the RegressionTree object in conjunction with a data frame,  then return the predictions.  To create predictions using the RegressionTree model and the FDX dataset:

RegressionTreePredictions <- predict(RegressionTree,FDX)
1.png

Run the line of script to console:

2.png
library(dplyr)
FDX <- mutate(FDX, RegressionTreePredictions)
3.png

Run the block of script to console:

4.png

2) Visualise a rpart Decision Tree

This Blog entry is from the Probability and Trees section in Learn R.

Once familiar with the output of a regression tree, it becomes an informative means to create business rules. Quite often however, for the purposes of communication, it is more satisfying to create a visualisation.  A package called rpart.plot is available for the purposes of translating regression trees to a visualisation.  Start by installing the rpart.plot package:

1.png

Click install to download and install the package:

2.png

Reference the library:

library(rpart.plot)
3.png

Run the line of script to console:

4.png

To transpose the Regression Tree to a plot, simply pass it as an argument to the rpart.plot() function:

rpart.plot(RegressionTree)
5.png

Run the line of script to console:

6.png

It can be seen that a complex visualisation has been created in the plots window of R Studio:

7.png

The visualisation is exceptionally hard to interpret for a large regression tree; hence it will likely need to be exported to a PDF or Image file to use a zoom function:

8.png