I know this is a fairly specific R
question, but I may be thinking about proportion variance explained, $R^2$, incorrectly. Here goes.
I'm trying to use the R
package randomForest
. I have some training data and testing data. When I fit a random forest model, the randomForest
function allows you to input new testing data to test. It then tells you the percentage of variance explained in this new data. When I look at this, I get one number.
When I use the predict()
function to predict the outcome value of the testing data based on the model fit from the training data, and I take the squared correlation coefficient between these values and the actual outcome values for the testing data, I get a different number. These values don't match up.
Here's some R
code to demonstrate the problem.
# use the built in iris data
data(iris)
#load the randomForest library
library(randomForest)
# split the data into training and testing sets
index <- 1:nrow(iris)
trainindex <- sample(index, trunc(length(index)/2))
trainset <- iris[trainindex, ]
testset <- iris[-trainindex, ]
# fit a model to the training set (column 1, Sepal.Length, will be the outcome)
set.seed(42)
model <- randomForest(x=trainset[ ,-1],y=trainset[ ,1])
# predict values for the testing set (the first column is the outcome, leave it out)
predicted <- predict(model, testset[ ,-1])
# what's the squared correlation coefficient between predicted and actual values?
cor(predicted, testset[, 1])^2
# now, refit the model using built-in x.test and y.test
set.seed(42)
randomForest(x=trainset[ ,-1], y=trainset[ ,1], xtest=testset[ ,-1], ytest=testset[ ,1])