“Factor has new levels” error for variable I'm not using

前端 未结 2 1354
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 01:03

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         


        
2条回答
  •  心在旅途
    2020-12-14 01:06

    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 
    

提交回复
热议问题