0

I running into this error while doing linear regression. I have tried to drop the new level on testing data but when I run a MAPE on testing set the value is NA.

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  factor Certification has new levels DOW

This is my original code

set.seed(123)
train_index <- sample(2, 440, prob = c(0.7,0.3), replace = T)

train <- data[train_index == 1, ]
test <- data[train_index == 2, ]


linreg_model <- lm(formula = Price ~. , 
                    data = train)

#MAPE of training data
linreg_trainpreds <- linreg_model$fitted.values
err <- linreg_trainpreds - train$Price
abserr<- abs(err)
percabserr <- abserr / train$Price
mape <- mean(percabserr)
mape

#MAPE on testing data
linreg_testpreds <- predict(linreg_model, test)
err <- linreg_testpreds - test$Price
abserr<- abs(err)
percabserr <- abserr / test$Price
mape <- mean(percabserr)
mape

I expected to get value on MAPE testing set

  • I'm guessing that at least one of the variables in your data is categorical. That variable or those variables are where are your error is derived from. It's likely that your current seed has placed at least one of these factor levels entirely in the test data. It looks like you're new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. For a better, more detailed answered, include your data from the output of `dput()` or `reprex::reprex()`. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Nov 06 '22 at 15:24

0 Answers0