我注意到,在手动更改分类变量级别之一的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”。一个愚蠢的错误,但是一个很好的教训。感谢所有的帮助。