I am trying to follow along in a textbook example from James et al.'s "An introduction to statistical Learning with Applications in R" and I am running into an error message I don't understand.
library(MASS)
library(randomForest)
set.seed(1)
bag.boston=randomForest(medv~.,data=Boston, subset=train,mtry=13, importance=TRUE)
yhat.bag = predict(bag.boston,newdata=Boston[-train,])
With this last line I get the error message
Error in eval(expr, envir, enclos) : object 'age' not found
Why am I getting this error message and how do I prevent it? I see that a similar question was asked here: Error in running randomForest : object not found . but in that case the OP was trying to input a matrix rather than a data frame as their original data set, and anyhow that is at the randomForest call, rather than the predict call.
This person randomForest in R object not found error also had a similar problem, but traced it to non ascii characters in their text file, which I am pretty sure is not characteristic of this data set.
Maybe I am supposed to subsstitute the word "data" for "newdata" in the predict function, but that seems to yield really different answers than I see in the text examples.
Any other thoughts?