I'm trying to use the randomForest
algorithm in R. When I apply the algorithm to my database, I got the following result:
library(randomForest)
x = data[,1:8]
y = data[,9]
model <- randomForest(y ~ ., x)
model
Call:
randomForest(formula = y ~ ., data = x)
Type of random forest: classification
Number of trees: 500
No. of variables tried at each split: 2
OOB estimate of error rate: 23.7%
Confusion matrix:
0 1 class.error
0 430 70 0.1400000
1 112 156 0.4179104
The answer is according to the prediction:
pred <- predict(model, type = "class")
table(pred, y)
1-mean(pred==y)
y
pred 0 1
0 430 112
1 70 156
[1] 0.2369792
Here, above, we have the same error rate. However, when I try a new prediction with the explicit initial x values, the result is different:
pred2 <- predict(model, newdata = x, type = "class")
table(pred2, y)
1-mean(pred2==y)
y
pred2 0 1
0 500 0
1 0 268
[1] 0
I've already tried the same procedure with the glm
function, and in both cases, the result were the same.
What is the difference between the two predictions above?
randomForest(y ~ ., x)
y is a vector and all the predictors are in x. The formula argument expects all variables in the same data frame when you provide a formula. UserandomForest(y=y,x=x)
. $\endgroup$