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 random forest models for regression

This recipe looks at random forests—one of the most successful machine learning techniques.

Getting ready

If you have not already installed the randomForest and caret packages, install them now. Download the data files for this chapter from the book's website and place the BostonHousing.csv file is in your R working directory. We will build a random forest model to predict MEDV based on the other variables.

How to do it...

To build random forest models for regression, follow the steps below:

  1. Load the randomForest and caret packages:
    > library(randomForest)
    > library(caret)
  2. Read the data:
    > bn <- 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