Random Forest in R

R

Loading a dataset first

I will be using the birthwt dataset which can be found in the MASS library:

library(MASS)
data(birthwt)

It is very important to make sure that any categorical variable is coded as factor first:

birthwt$race = as.factor(birthwt$race)
birthwt$smoke = as.factor(birthwt$smoke)
birthwt$ht = as.factor(birthwt$ht)
birthwt$ui = as.factor(birthwt$ui)

Creating a Random Forest algorithm:

# Load the randomForest package
library(randomForest)

# Implement the random forest algorithm and look at the result
rf = randomForest(low~., data=birthwt)
rf
Call:
 randomForest(formula = low ~ ., data = birthwt) 
               Type of random forest: regression
                     Number of trees: 500
No. of variables tried at each split: 3

          Mean of squared residuals: 0.008889174
                    % Var explained: 95.86

We can check variable importance as follows:

varImpPlot(rf)

png

Et voila!

Share this post: