r - R 中泊松回归的预测不准确

标签 r regression prediction predict poisson

我正在尝试根据收集的历史数据预测网站的访问者数量。我认为这是我可以使用泊松回归的场景。

输入由 6 列组成:

id(网站id)、日、月、年、星期几、访问量。

所以基本上作为输入,我们有一个 CSV 格式的列:“2”,“22”,“7”,“2015”,“6”,“751”。

我正在尝试根据之前的访问次数来预测访问次数。网站的大小可能会有所不同,因此我最终将它们分为 5 个类别

  • 几乎为零(平均 < 1)
  • 非常小(平均 < 100)
  • 小(平均 < 1000)
  • 中等(平均 < 50.000)
  • 大(平均 < 500.000)

所以我创建了一个名为 type 的第 7 列,它是一个 int,范围从 1 到 5。

我的代码如下:

train = read.csv("train.csv", header = TRUE)
model<-glm(visits ~ type + day + month + year + dayofweek, train, family=poisson)
summary(model)
P = predict(model, newdata = train)
imp = round(P)
imp

预测值甚至不接近,我教过我最终可能会得到实际值的 10-20%,但没有这样做,大部分预测值比实际值大 200-300% .这是在火车数据集上,应该提供乐观的看法。

我是 R 的新手,在解释 summary 命令返回的数据时遇到了一些问题。这是它返回的内容:

Call: glm(formula = visits ~ type + day + month + year + dayofweek, family = poisson, data = train)

Deviance Residuals: Min 1Q Median 3Q Max
-571.05 -44.04 -11.33 -5.14 734.43

Coefficients:

            Estimate Std. Error  z value Pr(>|z|)     

(Intercept) -9.998e+02  6.810e-01 -1468.19   <2e-16 *** 

type         2.368e+00  1.280e-04 18498.53   <2e-16 *** 

day         -2.473e-04  6.273e-06   -39.42   <2e-16 *** 

month        1.658e-02  3.474e-05   477.31   <2e-16 *** 

year         4.963e-01  3.378e-04  1469.31   <2e-16 *** 

dayofweek   -3.783e-02  2.621e-05 -1443.46   <2e-16 ***

--- Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

Null deviance: 1239161821 on 12370 degrees of freedom Residual deviance: 157095033 on 12365 degrees of freedom AIC: 157176273

Number of Fisher Scoring iterations: 5

谁能更详细地描述 summary 命令返回的值,以及它们在泊松回归中应该是什么样子才能输出更好的预测?对于基于待估计值随时间演变的数据,R 中是否有更好的方法?

乐。 link to train.csv file .

最佳答案

您的问题出在 predict 命令上。 predict.glm 中的默认设置是在链接尺度上进行预测。如果您想要可以直接与原始数据进行比较的预测,则需要使用参数 type = "response"

P <- predict(model, newdata = train, type = "response")

模型设置不理想。或许应该将月份作为分类变量 (as.factor) 包含在内,并且您需要更多地考虑日期(每月的第 31 天之后是下个月的第 1 天)。预测变量“类型”也是可疑的,因为类型直接来自响应。

您的模型也高度过度分散。这可能表示缺少预测变量或其他问题。

您还应该考虑使用混合效应模型。

关于r - R 中泊松回归的预测不准确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36529436/

相关文章:

r - R smooth.spline(): smoothing spline is not smooth but overfitting my data

r - 绘制 glm 交互 : "newdata=" structure in predict() function

R中的残差使用auto.arima和预测包

r - 仅搜索特定记录的矩阵

html - Shiny :侧边栏和 "sentence"之间的空格

python - xgboost 回归预测相同的值

machine-learning - 如何确定 RMSLE 的准确性?

machine-learning - StackOverflow 标签预测器…请推荐一种机器学习方法?

r - 在同一个 Shiny 的应用程序中使用 shinyjs 和 ggplot2::autoplot

r - 增加 y 轴刻度标签 ggplot2 上的间距