代码之家  ›  专栏  ›  技术社区  ›  Coldchain9

对于泊松对数链接模型,预测函数如何处理R为0的连续值?

  •  1
  • Coldchain9  · 技术社区  · 7 年前

    我在一些虚拟数据上使用泊松GLM来预测基于两个变量的索赔数量,频率和司法方向。

    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(-0.3493+-3.3343*司法导向中性+ -3.4512*司法指导原告+39.8765*频率+日志(曝光))

    但是我手动尝试了这个 (In excel =EXP(-0.3493+0+0+LOG(10)) for observation 1 for example) 而对于一些观察,却没有得到正确的答案。

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

    1 回复  |  直到 7 年前
        1
  •  2
  •   Nairolf    7 年前

    你关于如何做的假设是对的 predict() 对于泊松分布,GLM有效。这可以在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
    

    推荐文章