如何从 glm 泊松模型中提取最终方程?

机器算法验证 r 回归 回归系数 爪哇
2022-04-15 04:32:14

我有一个表现良好的泊松模型。现在我们需要将它放入 Java 代码中并发布给全世界。我将泊松系数插入的方程是什么?

类似于这个问题: Find the equation from generalized linear model output

这是来自 R 的一些示例代码,显示了我在做什么:

d = data.frame(y=(10:100)^-1*100, x=10:100)
m = glm(y~x, data=d, family=poisson(link="log"))
plot(d$x, predict.glm(m, type="response"))
points(d$x, d$y, col="blue")
summary(m)

输出:

Call:
glm(formula = y ~ x, family = poisson(link = "log"), data = d)

Deviance Residuals: 
     Min        1Q    Median        3Q       Max  
-0.34781  -0.25933  -0.05075   0.21286   1.20265  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)    
(Intercept)  2.155676   0.126778  17.004   <2e-16 ***
x           -0.025912   0.002819  -9.191   <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: 104.5820  on 90  degrees of freedom
Residual deviance:   8.5623  on 89  degrees of freedom
AIC: Inf

Number of Fisher Scoring iterations: 4

在此处输入图像描述

> cor(d$y, predict.glm(m, type="response"))
[1] 0.9461024

这是相当不错的!:)

2个回答

方程是

log(μi)=β0+β1xi

在哪里μi是条件期望yi,E(y|x),β0是标记的系数Interceptβ1标记的系数xlogbit 是您指定的链接函数。因此,要获得有关响应数据规模的实际预测y,您需要将链接函数的逆函数(反对数)应用于等式的两边:

μi=exp(β0+β1xi)

μi然后是给定的值的预测平均计数x

如果需要,打印出系数:

coef(m)

它们可能比summary()输出中的更精确,因此您的 Java 代码将更接近predict().

尽管线性相关性很高,但该模型看起来并不好。在整个范围内都存在偏差x在对数据一无所知的情况下,您是否考虑过一个模型xx2?

这太棒了,我已经用它来验证一个绘图的一部分,在该绘图中我正在模拟在某些高度计算的树木数量。但是,我在 pscl 中通过 zeroinfl() 使用零膨胀泊松模型,并且 zeroinfl() 的预测值在海拔约 1000m 后开始偏离 Gavin 提供的方程。我假设这是因为零计数的概率在大约 1000m 的高度后增加,因此 zeroinfl() 模型开始考虑这种增加的零计数概率。太好了,它很好地反映了实际观察结果,但我还需要知道这条线的方程式。这个模型的零膨胀泊松版本是什么?涉及概率的东西?下面是一个图,其中的点是实际观察值,红色是基于 zeroinfl() 的预测值,蓝色是基于 Gavin 提供的方程和 zeroinfl() 模型的系数的预测值。我正在使用 y 轴的对数表示来澄清偏差。包括代码的一般表示。

LogMega[,1] <- yjPower(LogMega[,1],0) #This is a log transform of the average density
m1 <- zeroinfl(Avg_dens~Z, data = LogMega) #the zero-inflated model
m2 <- 2.718^(coef(m1)[1]+(coef(m1)[2]*Z), data=LogMega) #the Poisson equation model
newdata1 <- expand.grid(LogMega[,n+3]) #create a table of the zeroinfl() predicted values
colnames(newdata1)<-"pred"
newdata1$resp <- predict(m1,newdata1)
p <- ggplot(LogMega,aes_string(x=names(LogMega)[n+3],y=LogMega[1]))
theme(
  geom_line(data=newdata1, aes(x=pred,y=resp), size=1, color="red")+
  geom_line(data=LogMega, aes(x=LogMega[,n+3], y=m2), size=1, color="blue"))

在此处输入图像描述