1) Create a Decision Tree using rpart.

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

Firstly it is necessary to install the rpart package:

1.png

Click the install button to download and install the package:

2.png

Load the lirary rpart:

library(rpart)
3.png

Run the line of script to console:

4.png

Unlike in regression Blog entries, where it is necessary to be selective about the independent variables being passed to the model,  regression trees do not necessarily require any variable selection as they are much better at producing models on very large feature sets, as such can be instructed to use all independent variables.  To train a regression tree, use the rpart() function, specify the dependent variable and the independent variables:

RegressionTree <- rpart(Dependent ~ ., data = FDX)
5.png

Run the line of script to console:

6.png

A regression tree has been created and saved to the RegressionTree object.  To retrieve information about the splits and tree simply:

RegressionTree
7.png

Run the line of script to console:

8.png

A regression tree has been written out that can be interpreted as a series of if, then, else statements.  In this example, the follow logic would predict a percentage price change, although there are many variations:

If Range_1>=1.095992 and Min_2>=0.1196234 and PointStep_12_PearsonCorrelation>=-0.3153543 then Forecast is -0.149363700
9.png

The rtree() function has though suggested a wide range of potential rules,  the endpoints being denoted by a *.  It is though important to understand the performance of each one these endpoints to propose implementation of these rules.  To establish the error rates of these endpoints, use the summary() function passing the RegressionTree object:

summary(RegressionTree)
10.png

Run the line of script to console:

11.png

Notice in the regression tree output, each node is labelled and in this example, node 18 was referenced.  By searching for this node in the summary output, the error rate can be determined:

12.png