Building regression trees

This recipe covers the use of tree models for regression. The rpart package provides the necessary functions to build regression trees.

Getting ready

Install the rpart, caret, and rpart.plot packages if you do not already have them installed. If you have not already downloaded the data files for this chapter, do so now and ensure that the BostonHousing.csv and education.csv files are in the R working directory.

How to do it...

To build regression trees, follow the steps below:

  1. Load the rpart, rpart.plot, and caret packages:
    > library(rpart)
    > library(rpart.plot)
    > library(caret)
  2. Read the data:
    > bh <- read.csv("BostonHousing.csv")
  3. Partition the data:
    > set.seed(1000) > t.idx <- createDataPartition(bh$MEDV, p=0.7, list = FALSE) ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with O’Reilly online learning.

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