对于Poisson Log Link模型,Predict函数如何处理R中值为0的连续值?

时间:2018-11-27 21:57:35

标签: r offset glm predict poisson

我正在对一些虚拟数据使用Poisson GLM,以基于频率和司法方向这两个变量来预测ClaimCounts。

虚拟数据框:

data5 <-data.frame(Year=c("2006","2006","2006","2007","2007","2007","2008","2009","2010","2010","2009","2009"), 
           JudicialOrientation=c("Defense","Plaintiff","Plaintiff","Neutral","Defense","Plaintiff","Defense","Plaintiff","Neutral","Neutral","Plaintiff","Defense"),
           Frequency=c(0.0,0.06,.07,.04,.03,.02,0,.1,.09,.08,.11,0),
           ClaimCount=c(0,5,10,3,4,0,7,8,15,16,17,12),
           Loss = c(100000,100,2500,100000,25000,0,7500,5200, 900,100,0,50),
           Exposure=c(10,20,30,1,2,4,3,2,1,54,12,13)
           )

模型GLM:

ClaimModel <- glm(ClaimCount~JudicialOrientation+Frequency     
                           ,family = poisson(link="log"), offset=log(Exposure), data = data5, na.action=na.pass)

Call:
glm(formula = ClaimCount ~ JudicialOrientation + Frequency, family = poisson(link = "log"), 
    data = data5, na.action = na.pass, offset = log(Exposure))

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-3.7555  -0.7277  -0.1196   2.6895   7.4768  

Coefficients:
                             Estimate Std. Error z value Pr(>|z|)    
(Intercept)                   -0.3493     0.2125  -1.644      0.1    
JudicialOrientationNeutral    -3.3343     0.5664  -5.887 3.94e-09 ***
JudicialOrientationPlaintiff  -3.4512     0.6337  -5.446 5.15e-08 ***
Frequency                     39.8765     6.7255   5.929 3.04e-09 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 149.72  on 11  degrees of freedom
Residual deviance: 111.59  on  8  degrees of freedom
AIC: 159.43

Number of Fisher Scoring iterations: 6

我也在使用“曝光”偏移。

然后我想使用此GLM来预测相同观察结果的索赔数:

data5$ExpClaimCount <- predict(ClaimModel, newdata=data5, type="response")

如果我理解正确,则泊松glm方程应为:

  

ClaimCount = exp(-。3493 + -3.3343 * JudicialOrientationNeutral +   -3.4512 * JudicialOrientationPlaintiff + 39.8765 *频率+ log(Exposure))

但是我手动尝试(In excel =EXP(-0.3493+0+0+LOG(10)) for observation 1 for example)并进行了一些观察,但未获得正确答案。

我对GLM方程的理解不正确吗?

1 个答案:

答案 0 :(得分:2)

关于Poisson GLM predict()的工作原理的假设是正确的。可以在R中验证:

co <- coef(ClaimModel)
p1 <- with(data5,
           exp(log(Exposure) +                            # offset
               co[1] +                                    # intercept
               ifelse(as.numeric(JudicialOrientation)>1,  # factor term
                      co[as.numeric(JudicialOrientation)], 0) +
               Frequency * co[4]))                        # linear term

all.equal(p1, predict(ClaimModel, type="response"), check.names=FALSE)
[1] TRUE

如注释中所述,由于对数的底数不同(Excel中为10,R中为欧拉数),您在Excel中可能会得到错误的结果。