predict()错误:如果训练数据中存在一个变量,但预测数据中不存在该怎么办?
我有一个包含以下变量的训练数据集predict()错误:如果训练数据中存在一个变量,但预测数据中不存在该怎么办?,r,regression,glm,lm,predict,R,Regression,Glm,Lm,Predict,我有一个包含以下变量的训练数据集 str(PairsTrain) 'data.frame': 1495698 obs. of 4 variables: $ itemID_1 : int 1 4 8 12 15 19 20 20 22 26 ... $ itemID_2 : int 4112648 1223296 2161930 5637025 113701
str(PairsTrain)
'data.frame': 1495698 obs. of 4 variables:
$ itemID_1 : int 1 4 8 12 15 19 20 20 22 26 ...
$ itemID_2 : int 4112648 1223296 2161930 5637025 113701
$ isDuplicate : int 1 0 1 0 0 0 0 0 1 0 ...
$ generationMethod: int 1 1 1 1 1 1 1 1 1 1 ...
我使用logistic回归glm()
函数从该数据集学习
mod1 <- glm(isDuplicate ~., data = PairsTrain, family = binomial)
我试图对我的测试数据集做出如下预测
PredTest <- predict(mod1, newdata = Test, type = "response")
PredTest好的,这就是您所需要的:
test$generationMethod <- 0
嗨,Zheyuan,我在测试数据集中没有generationMethod。“id”、“itemID_1”、“itemID_2”是测试数据集中唯一的功能。我尝试将0分配给genrationMethod,并在测试数据集中执行isDuplicate,然后运行predict函数,它没有抛出任何错误,我还尝试使用您提到的mod2模型进行预测,它也起到了作用。谢谢你的帮助。
test$generationMethod <- 0
mod2 <- glm(isDuplicate ~ itemID_1 + itemID_2, data = PairsTrain,
family = binomial)