The code and error in the following MWE represent my issue.
a = LETTERS[1:4] b = LETTERS[1:5] n1 = 70 n2 = 30 s1 = sample(a, n1, replace = T) s2 = sample(b, n2, replace = T) s3 = rbinom(n1, 1, 0.5) s4 = rbinom(n2, 1, 0.5) train <- data.frame( x1 = s1, y1 = s3 ) test <- data.frame( x1 = s2, y1 = s4 ) m <- glm(y1 ~ x1, data=train, family = "binomial") predict(m, test, type="response")
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : factor x1 has new levels E
This has resulted from having test and training data that contain different values for a variable. The testset contains a value that's not in the trainset just by chance with how the sets have been created.
To create the sets (in the code on my system) the following process has been used;
# get a sample of indices t <- round(0.7 * nrow(main_data)) train <- sample(nrow(main_data), t, replace=FALSE) # create test / training data trainset <- main_data[train,] testset <- main_data[-train,]