线性回归-因子pe_mo_Float_Ind具有新水平

时间:2018-12-19 15:56:00

标签: r linear-regression prediction

我正在尝试将线性回归模型应用于测试集,但出现错误:

  

model.frame.default(Terms,newdata,na.action = na.action,xlev = object $ xlevels)中的错误:     pe_mo_Float_Ind因素具有新水平

这显然是不正确的,因为当我在训练和测试集中检查此变量时,它具有完全相同的因子水平:

> levels(as.factor(train$pe_mo_Float_Ind))
 [1] ""  "B" "M" "S" "W"
> levels(as.factor(test$pe_mo_Float_Ind))
[1] ""  "B" "M" "S" "W"

> prediction <- predict(model1, interval="prediction", test)
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = 
object$xlevels) : 
factor pe_mo_Float_Ind has new levels 

> str(train$pe_mo_Float_Ind)
  Factor w/ 5 levels "","B","M","S",..: 3 3 3 1 1 2 2 1 4 1 ...
> str(test$pe_mo_Float_Ind)
  Factor w/ 5 levels "","B","M","S",..: 5 2 1 1 3 3 2 3 3 3 ...

> summary(train$pe_mo_Float_Ind)
      B     M     S     W 
 60058 48905 29656  3722 10782 
> summary(test$pe_mo_Float_Ind)
       B     M     S     W 
 25916 20823 12734  1618  4534 

关于发生了什么的任何想法?对于未发布数据(过大),我深表歉意,并且示例不会重现该错误。非常感谢!

0 个答案:

没有答案