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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.