我正在执行一个预测建模应用程序,我必须在其中预测索赔。如果我使用经典 GLM,我会使用泊松 glm,使用对数曝光作为偏移量,因此假设
假设索赔与暴露成正比,因此允许协变量依赖性。我想使用 ctree 或 rpart 或其他基于树的方法。是否可以以某种方式处理此类模型中的先前偏移?
我正在执行一个预测建模应用程序,我必须在其中预测索赔。如果我使用经典 GLM,我会使用泊松 glm,使用对数曝光作为偏移量,因此假设
一种方法是采用正式的基于模型的树。包中的glmtree()
函数partykit
实现了用于基于模型的递归分区的通用 MOB 算法(Zeileis等人2008, Journal of Computational and Graphical Statistics , 17 (2), 492-514)。这支持泊松响应,还允许包含偏移量。此外,可以在每个终端节点中包含额外的回归器。
考虑以下简单的人工示例:
set.seed(1)
d <- data.frame(
x1 = runif(500),
x2 = runif(500),
exposure = runif(500, 1, 10)
)
d$claims <- rpois(500, lambda = exp(d$x1 > 0.5 & d$x2 > 0.5) * d$exposure)
这使用两个简单的分区变量 (x1
和x2
) 和一个exposure
变量。响应是具有偏移量log(exposure)
和均值的泊松分布,但均值1 = exp(0)
为均值的情况除外x1 > 0.5 & x2 > 0.5
exp(1)
然后可以为with和 partitioning variablesglmtree()
拟合基于 Poisson GLM 的树。claims
offset(log(exposure))
x1 + x2
m <- glmtree(claims ~ offset(log(exposure)) | x1 + x2,
data = d, family = poisson)
plot(as.constparty(m))
这捕获了真正的树结构(在这里很容易找到)并正确估计截距(使用默认的日志链接):
coef(m)
## 2 4 5
## -0.047934373 -0.005690107 1.050569309
您还可以获得有关树节点中每个拟合 GLM 的更多详细信息,例如,对于最后一个节点:
summary(m, node = 5)
## Call:
## glm(formula = claims ~ offset(log(exposure)))
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -3.13527 -0.66977 -0.04251 0.56984 2.13581
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) 1.05057 0.02375 44.24 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for poisson family taken to be 1)
##
## Null deviance: 630.95 on 120 degrees of freedom
## Residual deviance: 113.36 on 120 degrees of freedom
## AIC: 635.89
##
## Number of Fisher Scoring iterations: 4
中提供了更多详细信息和参考资料vignette("mob", package = "partykit")
。