# NOT RUN {
# Fit a Random Forest on the Boston housing data set
library("randomForest")
data("Boston", package = "MASS")
mod = randomForest(medv ~ ., data = Boston, ntree = 50)
# Fit a decision tree as a surrogate for the whole random forest
dt = tree.surrogate(mod, Boston[-which(names(Boston) == 'medv')], 200)
# Plot the resulting leaf nodes
plot(dt)
# Use the tree to predict new data
predict(dt, Boston[1:10,])
# Extract the results
dat = dt$data()
head(dat)
# It also works for classification
mod = randomForest(Species ~ ., data = iris, ntree = 50)
# Fit a decision tree as a surrogate for the whole random forest
X = iris[-which(names(iris) == 'Species')]
dt = tree.surrogate(mod, X, 200, predict.args = list(type = 'prob'), maxdepth=2, class=3)
# Plot the resulting leaf nodes
plot(dt)
# If you want to visualise the tree directly:
plot(dt$tree)
# Use the tree to predict new data
set.seed(42)
iris.sample = X[sample(1:nrow(X), 10),]
predict(dt, iris.sample)
predict(dt, iris.sample, type = 'class')
# Extract the dataset
dat = dt$data()
head(dat)
# }
Run the code above in your browser using DataLab