Consider a simple dataset, split into a training and testing set:
dat <- data.frame(x=1:5, y=c(\"a\", \"b\", \"c\", \"d\", \"e\"), z=c(0, 0, 1, 0, 1))
tra
You could try updating mod2$xlevels[["y"]] in the model object
mod2 <- glm(z~.-y, data=train, family="binomial")
mod2$xlevels[["y"]] <- union(mod2$xlevels[["y"]], levels(test$y))
predict(mod2, newdata=test, type="response")
# 5
#0.5546394
Another option would be to exclude (but not remove) "y" from the training data
mod2 <- glm(z~., data=train[,!colnames(train) %in% c("y")], family="binomial")
predict(mod2, newdata=test, type="response")
# 5
#0.5546394