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

Inaccurate predictions with Poisson Regression in R

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

输入包含 6 列:

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

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

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

所以我创建了一个名为 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%实际值。而且这是在train数据集上,应该能提供乐观的看法。

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

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 中的默认设置是在 link 尺度上进行预测。如果您想要可以直接与原始数据进行比较的预测,则需要使用参数 type = "response"

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

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

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

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