O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

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

How to do it...

To build regression trees, perform the following steps:

  1. Load the rpart, rpart.plot, and caret packages:
> library(rpart) 
> library(rpart.plot) 
> library(caret) 
  1. Read the data:
> bh <- read.csv("BostonHousing.csv") 
  1. Partition the data:
> set.seed(1000) 
> t.idx <- createDataPartition(bh$MEDV, p=0.7, list = FALSE) 
  1. Build and view the regression tree model:
> bfit <- rpart(MEDV ~ ., data = bh[t.idx,]) > bfit n= 356 node), split, n, deviance, yval * denotes terminal node 1) root 356 32071.8400 22.61461 2) LSTAT>=7.865 242 8547.6860 18.22603 4) LSTAT>=14.915 114 2451.4590 14.50351 8) CRIM>=5.76921 56 796.5136 11.63929 * 9) CRIM< 5.76921 58 751.9641 17.26897 * 5) LSTAT< 14.915 128 3109.5710 21.54141 10) DIS>=1.80105 121 1419.7510 ...

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