在将训练数据拟合为线性模型之前,我已将测试数据中存在的新级别的因素列添加到我的训练数据因素列中。仍然逻辑回归模型在使用测试数据集进行预测时会引发以下错误:
model.frame.default(Terms,newdata,na.action = na.action,xlev = object $ xlevels)中的错误: 因子Parch具有新的9级
> classifier1 <- glm(formula=Survived ~ . , family = binomial,data = train_final[-1])
> predicted.values1 <- predict(classifier1, newdata = test_final[-1], type='response')# for logistic regression
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
factor Parch has new levels 9
> levels(train_final$Parch)
[1] "0" "1" "2" "3" "4" "5" "6" "9"
> levels(test_final$Parch)
[1] "0" "1" "2" "3" "4" "5" "6" "9"
> unique(train_final$Parch)
[1] 0 1 2 5 3 4 6
Levels: 0 1 2 3 4 5 6 9
> unique(test_final$Parch)
[1] 0 1 3 2 4 6 5 9
Levels: 0 1 2 3 4 5 6 9
>