R中预测和glm.predict的错误 [英] Error with predict and glm.predict in R

查看:320
本文介绍了R中预测和glm.predict的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我在R中训练了线性回归,以根据city(数据框data中的变量)预测this.target.此训练针对由train.index指定的数据子集完成.

I trained a linear regression in R to predict this.target from city, variables in data frame data. This trainig is done on a subset of the data, which is specified by train.index.

model = glm('data[, this.target] ~ data$city', data = data, subset = train.index)

我正在尝试对由test.index指定的保留数据进行测试.

I am trying to test this model on the held out data, which is specified by test.index.

predictions = predict(model, data[test.index, ])

无论出于何种原因,第二步都会产生错误和警告.

For whatever reason, this second step creates an error and a warning.

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev =
object$xlevels) : invalid type (NULL) for variable 'data$city' In addition:
Warning message: 'newdata' had 22313 rows but variables found have 0 rows

我的分析

data$city是4级的因数,但是即使该变量中的任何观察值都不为NULL,似乎R仍将其读取为无效类型(NULL)".

data$city is a factor of 4 levels, but it seems like R reads it as an "invalid type (NULL)" even though none of the observations in this variable are NULL.

此外,似乎R正确读取了训练集的行而不是列. dim(data[test.index, ])产生一个具有22313和12的向量.

Moreover, it seems like R reads the rows but not the columns of the training set correctly. dim(data[test.index, ]) yields a vector with 22313 and 12.

推荐答案

感谢joran的评论,我能够找到解决问题的方法. joran指出公式不应包含子集运算.

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.

这篇关于R中预测和glm.predict的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆