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

Get R: Recipes for Analysis, Visualization and Machine Learning 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.