O'Reilly logo

Practical Predictive Analytics by Ralph Winters

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

The random forest model

Now, let's run a random forest model. We will grow 2,000 trees and just for illustration include Age, Pclass (Passenger Class), and Fare as the independent variables. Random forest randomizes both the observations selected as well as a sample number of observations selected, so you are never certain which specific trees you will get. You might even get one of the trees just generated in the previous example!

library(randomForest) set.seed(123) fit <- randomForest(as.factor(Survived) ~ Age + Pclass + Fare,                     data=titanic,                      importance=TRUE,                      ntree=2000) 

Random forest also has a predict function, with a similar syntax to the predict function we used in Chapter 1, Getting Started with Predictive Analytics. We will use ...

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