3
votes

I'm trying to predict a continuous variable (count) in R with random forest. The values of the predicted variable are min=1 and max=1000.

I tried getting the prediction accuracy with "confusionMatrix", but naturally I get the error of different number of levels between the prediction and the predicted.

What is the best method of getting prediction accuracy in these circumstances?

2
to predict a continuous variable <-- regression || values of the predicted variable are min=1 and max=1000 <-- It can be depending on the data set, the range of continuous variable is (-infinity, infinity) || prediction accuracy with "confusionMatrix" <-- confusion matrix is not used for regression but root mean squared error, which is the root of squared sum of difference between actual and fitted values divided by the number of records (en.wikipedia.org/wiki/Root-mean-square_deviation) - Jaehyeon Kim
So, should I treat Rsquared as my prediction accuracy indicator? - mishakob

2 Answers

4
votes

@ mishakob

Roughly speaking, the root mean squared error can be understood as normalized deviance between actual and fitted values. it can be obtained as following.

library(randomForest)
set.seed(1237)
iris.rg <- randomForest(Sepal.Length ~ ., data=iris, importance=TRUE,
                        proximity=TRUE)

sqrt(sum((iris.rg$predicted - iris$Sepal.Length)^2) / nrow(iris))
[1] 0.3706187
1
votes

randomForest should only show confusion matrices for categorical outcomes, so try ensuring your outcome is numeric. This will then show mean squared residuals instead. e.g.:

library(randomForest)
# This is probably what you're seeing
randomForest(as.factor(Sepal.Length) ~ Sepal.Width, data=iris)
# This is what you want to see
randomForest(Sepal.Length ~ Sepal.Width, data=iris)