Random Forest Model in R

Premium

Now, we will tune RandomForest model. Like SVM, we tune parameter based on 5% downsampling data. The procedure is exactly the same as for SVM model. Below we have reproduced the code for Random Forest model.

1set.seed(300)
2#down sampling again so than we get more info when stacking
3samp = downSample(data_train[-getIndexsOfColumns(data_train, c( "loan_status") )],data_train$loan_status,yname="loan_status")
4#choose small data for tuning 
5train_index_tuning = createDataPartition(samp$loan_status,p = 0.05,list=FALSE,times=1)
6#choose small data for re-train
7train_index_training = createDataPartition(samp$loan_status,p = 0.1,list=FALSE,times=1)
8rfGrid = expand.grid(
9                .mtry = as.integer(seq(2,ncol(samp), (ncol(samp) - 2)/4))
10                )
11#Install random forest package
12library(randomForest)
13rfTuned = train(
14    samp[train_index_tuning,-getIndexsOfColumns(samp,"loan_status")],
15    y = samp[train_index_tuning,"loan_status"],
16    method = "rf",
17    tuneGrid = rfGrid,
18    metric = "ROC",
19    trControl = ctrl,
20    preProcess = NULL,
21    ntree = 100
22    )
23plot(rfTuned)
24

Unlock Premium Content

Upgrade your account to access the full article, downloads, and exercises.

You'll get access to:

  • Access complete tutorials and examples
  • Download source code and resources
  • Follow along with practical exercises
  • Get in-depth explanations