Skip to content

Instantly share code, notes, and snippets.

@batpigandme
Last active August 1, 2017 13:32
Show Gist options
  • Select an option

  • Save batpigandme/3407597ada793031ab1663fdc8fa2ba3 to your computer and use it in GitHub Desktop.

Select an option

Save batpigandme/3407597ada793031ab1663fdc8fa2ba3 to your computer and use it in GitHub Desktop.
Code from Random Forests in R by Anish Singh Walia on DataScience+ https://datascienceplus.com/random-forests-in-r/
---
title: "Random Forests in R"
output: html_notebook
---
From [**Random Forests in R**](https://datascienceplus.com/random-forests-in-r/) by [Anish Singh Walia](https://datascienceplus.com/author/anish-singh-walia/) (@[anish_walia](https://twitter.com/anish_walia)), via [**DataScience+**](https://datascienceplus.com).
# Implementation in R
```{r message=FALSE}
require(randomForest)
require(MASS)#Package which contains the Boston housing dataset
attach(Boston)
set.seed(101)
```
## Saperating Training and Test Sets
```{r}
#training Sample with 300 observations
train=sample(1:nrow(Boston),300)
?Boston #to search on the dataset
```
## Fitting the Random Forest
```{r}
Boston.rf=randomForest(medv ~ . , data = Boston , subset = train)
Boston.rf
```
### Plotting the Error vs Number of Trees Graph.
```{r}
plot(Boston.rf)
```
## Compare the Out of Bag Sample Errors & Error on Test Set
```{r}
oob.err=double(13)
test.err=double(13)
#mtry is no of Variables randomly chosen at each split
for(mtry in 1:13)
{
rf=randomForest(medv ~ . , data = Boston , subset = train,mtry=mtry,ntree=400)
oob.err[mtry] = rf$mse[400] #Error of all Trees fitted
pred<-predict(rf,Boston[-train,]) #Predictions on Test Set for each Tree
test.err[mtry]= with(Boston[-train,], mean( (medv - pred)^2)) #Mean Squared Test Error
cat(mtry," ") #printing the output to the console
}
```
### Test Error
```{r}
test.err
```
### Out of Bag Error Estimation
```{r}
oob.err
```
### Plotting Test Error & Out of Bag Error
```{r}
matplot(1:mtry , cbind(oob.err,test.err), pch=19 , col=c("red","blue"),type="b",ylab="Mean Squared Error",xlab="Number of Predictors Considered at each Split")
legend("topright",legend=c("Out of Bag Error","Test Error"),pch=19, col=c("red","blue"))
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment