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

人工修改GLM系数后如何在R中使用预测函数

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

    我注意到,在手动更改分类变量级别之一的GLM系数后,我仍然得到相同的预测值,尽管我知道我的一些数据具有此级别。一些代码可能有助于解释我的过程:

    ##data frame
    df <-data.frame(Account =c("A","B","C","D","E","F","G","H"), 
           Exposure = c(1,50,67,85,250,25,22,89),
           JudicialOrientation=c("Neutral","Neutral","Plaintiff","Defense","Plaintiff","Neutral","Plaintiff","Defense"),
           Freq= c(.008,.5,.05,.34,.7,0,.04,.12),
           Losses = c(100000,100,2500,100000,25000,0,7500,5200),
           LossPerUnit = c(100000,100,2500,100000,25000,0,7500,5200)/c(1,50,67,85,250,25,22,89))
    
    
    ##Variables for modeling
    ModelingVars <- as.formula(df$LossPerUnit~df$JudicialOrientation+df$Freq)
    
    ##Tweedie GLM
    Model <- glm(ModelingVars, family=tweedie(var.power=1.5, link.power = 0),
                 weight = Exposure, data = df)
    summary(Model)
    
    ##Predict Losses with Model coefficients
    df$PredictedLossPerUnit <- predict(Model,df, type="response")
    
    
    ##Manually edit a coefficient for one of my categorical variable's levels
    Model$coefficients["df$JudicialOrientationNeutral"] <-log(50)
    
    ##Predict Losses again to compare
    df$PredictedLossPerUnit2 <- predict(Model, df, type ="response")
    
    
    sum(df$PredictedLossPerUnit)
    sum(df$PredictedLossPerUnit2)
    View(head(df))
    summary(Model)
    

    有什么奇怪的事情会导致我的predict函数继续给出与原来相同的结果吗?即使我在GLM中手动更改了一个系数?

    编辑:我找到了答案。在我的另一个数据集中,我在做:

    数据实际上不是predict函数的参数,它应该是“newdata”。一个愚蠢的错误,但是一个很好的教训。感谢所有的帮助。

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

    你使用这个公式的方式是将意义从df对象中分离出来,或者混淆了df对象的逻辑 predict.lm 某物 . 如果改为按预期的方式运行公式创建(不引用数据对象的名称(因此仅使用列名),则可以获得所需的效果:

     ModelingVars <- as.formula(LossPerUnit~JudicialOrientation+Freq)
    
    #----------
    
    > df$PredictedLossPerUnit <- predict(Model,df, type="response")
    > 
    > 
    > ##Manually edit a coefficient for one of my categorical variable's levels
    > Model$coefficients["JudicialOrientationNeutral"] <-log(50)
    > 
    > ##Predict Losses again to compare
    > df$PredictedLossPerUnit2 <- predict(Model, df, type ="response")
    > 
    > df
      Account Exposure JudicialOrientation  Freq Losses  LossPerUnit PredictedLossPerUnit PredictedLossPerUnit2
    1       A        1             Neutral 0.008 100000 100000.00000           1549.56677           40213.38196
    2       B       50             Neutral 0.500    100      2.00000            919.41825           23860.16405
    3       C       67           Plaintiff 0.050   2500     37.31343            169.99221             169.99221
    4       D       85             Defense 0.340 100000   1176.47059            565.49150             565.49150
    5       E      250           Plaintiff 0.700  25000    100.00000             85.29641              85.29641
    6       F       25             Neutral 0.000      0      0.00000           1562.77490           40556.15105
    7       G       22           Plaintiff 0.040   7500    340.90909            171.80535             171.80535
    8       H       89             Defense 0.120   5200     58.42697            714.15870             714.15870
    

    我通常会尽量保持屏幕上的基本资料,但在这里你将需要滚动,看看“中立”的两列中的项目是不同的。

        2
  •  0
  •   Coldchain9    7 年前

    df$PredictedLossPerUnit <- predict(Model,data=df, type="response")
    

    “data”实际上不是predict函数的函数参数,它应该是“newdata”。一个愚蠢的错误,但是一个很好的教训。感谢所有的帮助。

    推荐文章