I am getting an error from my datasets similar logic with the code I posted in below. I have tried increased the number of training data but didn't solve. I have already excluded all NA
values.
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : factor y has new levels L, X
set.seed(234)
d <- data.frame(w=abs(rnorm(50)*1000),
x=rnorm(50),
y=sample(LETTERS[1:26], 50, replace=TRUE))
train_idx <- sample(1:nrow(d), floor(0.8*nrow(d)))
train <- d[train_idx,]
test <- d[-train_idx,]
fit <- lm(w ~x + y, data=train)
predict(fit, test)