R 在手动更改GLM';s系数

R 在手动更改GLM';s系数,r,modeling,glm,coefficients,R,Modeling,Glm,Coefficients,我正在创建一个包含许多变量的GLM模型。获得输出后,我将使用GLM预测新值 我已经注意到,在手动更改一个分类变量级别的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), Judicia

我正在创建一个包含许多变量的GLM模型。获得输出后,我将使用GLM预测新值

我已经注意到,在手动更改一个分类变量级别的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)
数据帧
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
ModelingVars df$PredictedLossPerUnit
> 
>##手动编辑我的分类变量级别之一的系数
>模型$系数[“司法方向性”中性]
>##再次预测损失进行比较
>df$PredictedLossPerUnit2
>df
账户风险司法定向频率损失预测损失预测损失预测损失2
1 A 1空档0.008 100000 100000.00000 1549.56677 40213.38196
2 B 50空档0.500 100 2.00000919.41825 23860.16405
3 C 67原告0.050 2500 37.31343 169.99221 169.99221
4 D 85防御0.340 100000 1176.47059 565.49150 565.49150
5 E 250原告0.700 25000 100.00000 85.29641 85.29641
6 F 25空档0.000 0.000001562.77490 40556.15105
7 G 22原告0.040 7500 340.90909 171.80535 171.80535
8小时89防御0.120 5200 58.42697 714.15870 714.15870
我通常会尝试在屏幕上保留基本内容,但在这里,您需要滚动查看两列中的“中性”项目是否不同


编辑:我将公式的创建放在外面,因为这是可能的最小更改,但是更好的策略是只使用您的公式,而不使用“as.formula”包装器,它不应该是需要的,并且将有一个不同的环境用于以后的评估。第一次运行:Model我找到了答案。在我的其他数据集中,我正在做:

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

df$PredictedLossPerUnit
tweedie()
函数从何而来?我无法运行代码。一个潜在的问题是,如果您希望以后能够使用
predicted()
安装.packages(“tweedie”),就不应该在公式中使用“$”谢谢你的回答。我的测试脚本代码确实得到了相同的结果,而没有对我的原始脚本进行更改。但是,当我在我真正的主数据集上执行此操作时(这只是我为了提问而创建的一个伪数据集).即使在操纵“中性”的系数后,我也会得到相同的预测数。我想这很难说明,因为我无法共享我的整个其他数据集,但我不明白为什么predict函数在手动更改系数后会给出相同的预测值,就像我在本测试中所做的那样。请参见答案正文。故事的一个寓意是:按照运行代码的方式呈现代码。我可能会这样做我已经看到了这个错误。另外,你不应该用“函数”的名称来调用像
newdata
这样的实体。它们是“形式参数”。这一点很好。newdata是我在其中传递“df”参数的参数的名称。再次感谢你的帮助。