June 2017
Beginner to intermediate
576 pages
15h 22m
English
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 ...