How it works...

Steps 1 and 2 load the required packages and read the data.

Step 3 partitions the data. Refer to the Creating random data partitions recipe in Chapter 2, What's In There? - Exploratory Data Analysis for more details. We set the random seed to enable you to match your results with those that we have displayed.

Step 4 uses the rpart function to build the tree model. It passes the formula as MEDV ~ . to indicate that MEDV is the outcome and that all the remaining variables will be predictors. It specifies data = bh[t.idx, ] to indicate that only the rows in the training partition should be used to build the model. It then prints the model details in textual form. The output shows information for the root node and subsequently ...

Get R Data Analysis Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.