[R] Regression Tree Questions

José María Mateos chema at rinzewind.org
Sat Feb 24 20:21:36 CET 2018


On Sat, Feb 24, 2018 at 01:16:27PM -0600, Gary Black wrote:
> Hi All,
> 
> I'm a newbie and have two questions.  Please pardon me if they are very basic.
> 
> 
> 1.  I'm using a regression tree to predict the selling prices of 10 new records (homes).  The following code is resulting in an error message:  pred <- predict(model, newdata = outOfSample[, -6]) 
> 
> The error message is:
> 
> Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = attr(object,  : 
> factor Sq. Feet has new levels 1375, 1421, 1547, 1621, 1868, 2211, 2265, 2530, 2672, 3365
> 

Seems to me that variable 'Sq. Feet' is being encoded as a factor 
instead of having numerical values. When you train, the model sees a 
series of values that understands as categorical, and when you try to 
predict it is encountering some different categories and it doesn't know 
what to do with them.

As that variable is most probably numeric, it should be read as such. 
You can try converting it on both your train and test datasets.

Cheers,

JMM.

-- José María Mateos
https://rinzewind.org/blog-es || https://rinzewind.org/blog-en



More information about the R-help mailing list