Random Forests With R

"Random forests as the name suggests grows large number of decision trees from randomized subsets of variables and observations from the input data. This overcomes influence bias and over-fitting effects, often yielding better accuracy compared to decision trees, but costs intensive computations."

cforest() is party’s implementation of random forest. The example below shows how cforest() can be implemented to predict the outcome. Besides prediction, random forest can also be separately used for variables selection if you choose to pursue other methods.

Prepare Training and Test Data

library (party)
data ("GlaucomaM", package = "ipred")
inputData <- GlaucomaM  # plugin your data here, with 'Class' as dependent variable
set.seed (100)
train <- sample(1:nrow(inputData), 0.7*nrow(inputData))  # random sample
trainData <- inputData[train,]  # training data
testData <- inputData[-train,]  # test data

Predict with cTree

cTreeMod <- ctree (Class ~ ., data = trainData)  # fit cTree with 'Class' as dependent
actuals <- testData$Class # actuals
predicted <- predict(cTreeMod, newdata = testData) # predicted
table(true = actuals, pred = predicted) # confusion matrix
mean (testData$Class != predicted) # Misclassification Error %

# Results Output: cTree()
             pred
true     glaucoma normal
glaucoma       15      7
normal          2     22
Misclassification Rate: 19.6%

cTree Example

Predict with cForest

cForestMod <- cforest(Class ~ ., data = trainData)  # random Forest model
actuals <- testData$Class # actuals
predicted <- predict(cTreeMod, newdata = testData) # predicted
table (true = actuals, pred = predict(cForestMod, newdata = testData))
mean (testData$Class != predicted) # Misclassification Error %

# Results Output: cForest()
             pred
true     glaucoma normal
glaucoma       19      3
normal          3     21
Misclassification Rate: 13%

cForest() improves the prediction accuracy compared to cTree(), however, it does not have the visualization capabilities that cTree() offers.

If you like us, please tell your friends.Share on LinkedInShare on Google+Share on RedditTweet about this on TwitterShare on Facebook