O'Reilly logo

R Data Analysis Cookbook by Shanthi Viswanathan, Viswa Viswanathan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required