# How does R's randomForest package calculate its constant predictions?

I tried to understand it, by applying an regression-tree on the Friedman#1 dataset:

library("mlbench")

dataset <- list()
random.forest <- list()
regression.tree <- list()
train.data.in.test.leaf <- list()
test.data.in.test.leaf <- list()

test.leaf.size <- 0
test.leaf.prediction <- 0
test.leaf.index <- 1
all.leaf.index <- matrix(1, nrow = 500, ncol = 1)

dataset <- data.frame(mlbench.friedman1(501, sd=1))


Then I fitted a regression tree on the 500 first data-points in order to predict the 501-st data-point:

library("randomForest")

random.forest <- randomForest(y ~ ., data=dataset[1:500,], ntree=1, nodesize=50, mtry=3)

regression.tree <- data.frame(getTree(random.forest))
status <- regression.tree$status; split.var <- regression.tree$split.var;
split.point <- regression.tree$split.point; while(status[test.leaf.index] < (-1)) { if(dataset[501,split.var[test.leaf.index]] <= split.point[test.leaf.index]) { test.leaf.index <- regression.tree$left.daughter[test.leaf.index]
} else {
test.leaf.index <- regression.tree$right.daughter[test.leaf.index] } } for(i in 1:500) while(status[all.leaf.index[i]] < (-1)) { if(dataset[i,split.var[all.leaf.index[i]]] <= split.point[all.leaf.index[i]]) { all.leaf.index[i] <- regression.tree$left.daughter[all.leaf.index[i]]
} else {
all.leaf.index[i] <- regression.tree$right.daughter[all.leaf.index[i]] } } end train.data.in.test.leaf <- dataset[all.leaf.index == test.leaf.index,]; test.data.in.test.leaf <- dataset[501,]; test.leaf.prediction <- regression.tree$prediction[test.leaf.index]