To build random forest models for regression, perform the following steps:
- Load the randomForest and caret packages:
> install.packages(c("caret","randomForest"))> library(randomForest) > library(caret)
- Read the data:
> bn <- read.csv("BostonHousing.csv")
- Partition the data:
> set.seed(1000) > t.idx <- createDataPartition(bh$MEDV, p=0.7, list=FALSE)
- Build the random forest model. Since this command builds many regression trees, it can take significant processing time on even moderate datasets:
> mod <- randomForest(x = bh[t.idx,1:13], y=bh[t.idx,14],ntree=1000, xtest = bh[-t.idx,1:13], ytest = bh[-t.idx,14], importance=TRUE, keep.forest=TRUE)
- Examine the results (your results will most likely differ slightly because ...