1
$\begingroup$

I'm training a randomForest with 202 observations of 1026 variables. I want to test the prediction accuracy of this forest on a different set of 50 observations. But predict() returns a number predictions equal to the size of my training set. Is there something I'm missing here in the below usage? I thought this would use the already-trained forest to predict the newData. (Can post more code if necessary but this seems like the only relevant part)

dim(trainData)   
#[1] 202 1026   
length(response)  
#[1] 202  
forest<-randomForest(response~., data=trainData)  
dim(testData)  
#[1] 50 1026  
length(predict(forest, newData=testData))  
#[1] 202 

... Why is this not 50? (the size of testData)

$\endgroup$
3
$\begingroup$

Check the capitalization of your parameters:

length(predict(forest, newData=testData))

Should be

length(predict(forest, newdata=testData))

It's unfortunate that it doesn't warn you that there was a spelling mistake. Without the "newdata" parameter, it gives you the prediction of the train set.

$\endgroup$
  • $\begingroup$ Wow. Thanks! After being a developer for years R is still like greek to me sometimes. $\endgroup$ – Brian Faires Mar 31 '17 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.