Error with predict and glm.predict in R

一曲冷凌霜 提交于 2019-12-02 02:13:45

Thanks to joran's comment, I was able to find the solution to my problem. joran pointed out that formulas should not include subsetting operations.

As it turns out, this subsetting was allowing model fitting to proceed as normal, but it caused model predicting to balk with the error and warning described above. By removing subsetting from my formula definition, both model fitting and predicting ran without problem.

Alaa Hesham

This solution is for this error but not for the main question cause it is difficult to follow .

The solution is making a variable let's say x; x=as.data.frame(testset) and pass it to predict as

classifier = glm(formula = Survived ~ .,
                 family = binomial,
                 data = training_set)
x = as.data.frame(test_set)
prob_pred = predict(classifier, type = 'response', newdata =x)
y_pred = ifelse(prob_pred > 0.5, 1, 0)

Predicting the Test set results

In glm or rpart (library for classification based on decision tree), the test set should be data frame and sometimes the pre-processing steps change your data type (the data you want to predict) should be data frame, if it is not, the error

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels): 'data' must be a data.frame, not a matrix or an array

will appear.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!