5

这篇文章遵循这个问题:https ://stackoverflow.com/questions/31234329/rpart-user-defined-implementation

我对可以使用自定义标准处理树生长的工具非常感兴趣,这样我就可以测试不同的模型。

我尝试使用 partykit R 包来生长一棵树,其拆分规则由 Cox 模型的负对数似然给出(在 Cox 模型的情况下为对数准似然)并且拟合了 Cox 模型在每一片叶子上。

据我了解阅读有关 MOB 函数的小插图,有两种方法可以实现我自己的拆分标准,即让 fit 函数返回列表或模型对象。

出于我的目的,我尝试了这两种解决方案,但未能成功。

解决方案 1:返回一个列表对象:

我以“mob”小插曲中的“乳腺癌数据集”为例。

我试过这个:

cox1 = function(y,x, start = NULL, weights = NULL, offset = NULL, ...,
           estfun = FALSE, object = TRUE){
  res_cox = coxph(formula = y ~ x )
list(
  coefficients = res_cox$coefficients,
  objfun = - res_cox$loglik[2],
  object = res_cox)
}


mob(formula = Surv(time, cens) ~ horTh + pnodes - 1 | age + tsize + tgrade + progrec +
  estrec + menostat , 
    data = GBSG2 ,
    fit = cox1,
    control = mob_control(alpha = 0.0001) )

有一个关于 X 矩阵奇点的警告,并且 mob 函数是一个具有单个节点的树(即使 alpha 值较小)。

请注意,在运行 coxph 函数时,X 矩阵没有奇点问题:

res_cox = coxph( formula = Surv(time, cens) ~ horTh + pnodes  ,
             data = GBSG2 )

解决方案 2:返回一个 coxph.object :

我试过这个:

cox2 = function(y,x, start = NULL, weights = NULL, offset = NULL, ... ){
  res_cox = coxph(formula = y ~ x )
}

logLik.cox2 <- function(object, ...)
  structure( - object$loglik[2], class = "logLik")

mob(formula = Surv(time, cens) ~ horTh + pnodes - 1 | age + tsize + tgrade + progrec +
  estrec + menostat , 
    data = GBSG2 ,
    fit = cox2,
    control = mob_control(alpha = 0.0001 ) )

所以这次我沿着“progrec”变量进行了拆分:

Model-based recursive partitioning (cox2)

Model formula:
Surv(time, cens) ~ horTh + pnodes - 1 | age + tsize + tgrade + 
progrec + estrec + menostat

Fitted party:
[1] root
|   [2] progrec <= 21: n = 281
|         xhorThno  xhorThyes    xpnodes 
|       0.19306661         NA 0.07832756 
|   [3] progrec > 21: n = 405
|         xhorThno  xhorThyes    xpnodes 
|       0.64810352         NA 0.04482348 

Number of inner nodes:    1
Number of terminal nodes: 2
Number of parameters per node: 3
Objective function: 1531.132
Warning message:
In coxph(formula = y ~ x) : X matrix deemed to be singular; variable 2

我想知道我的解决方案 1 有什么问题。

我还为回归问题尝试了类似的方法并得到了相同的结果,以单叶结尾:

data("BostonHousing", package = "mlbench")

BostonHousing <- transform(BostonHousing,
                       chas = factor(chas, levels = 0:1, labels = c("no", "yes")),
                       rad = factor(rad, ordered = TRUE))


linear_reg = function(y,x, start = NULL, weights = NULL, offset = NULL, ...,
                  estfun = FALSE, object = TRUE){
  res_lm = glm(formula = y ~ x , family = "gaussian")
  list(
    coefficients = res_lm$coefficients,
    objfun = res_lm$deviance,
    object = res_lm )
}

mob( formula = medv ~ log(lstat) + I(rm^2) | zn + indus + chas + nox +
   + age + dis + rad + tax + crim + b + ptratio, 
     data = BostonHousing ,
     fit = linear_reg)

另外我想知道使用“将模型拟合到节点中”和“进行拆分”的变量是否没有问题。

先感谢您。

我可能还有其他关于partykit 功能的问题。

4

1 回答 1

5

cox1()您设置的和函数的问题linear_reg()是您没有提供估计函数,即分数贡献。由于这些是选择拆分变量的推理的基础,因此如果不提供这些,算法根本不会拆分。有关此问题的一些讨论,请参阅此最近的答案

但是对于coxph()对象(与fitdistr()上面讨论中的示例不同),很容易获得这些估计函数或分数,因为有estfun()一种可用的方法。所以你的cox2()方法是更容易到达这里的途径。

后者不能正常工作的原因是由于在coxph(). 在内部,这总是强制截距进入模型,然后从设计矩阵中省略第一列。当通过这个接口时,mob()你需要小心不要把它弄乱,因为mob()设置了它自己的模型矩阵。并且由于您排除了截距,mob()因此认为它可以估计horTh. 但事实并非如此,因为在 Cox-PH 模型中没有识别截距。

在这种情况下(IMO)的最佳解决方案如下:您让mob()设置一个截距,然后在将模型矩阵传递给时再次排除它coxph()。因为生成的对象有coef()logLik()estfun()方法,所以可以使用cox2()函数的简单设置。

包和数据:

library("partykit")
library("survival")
data("GBSG2", package = "TH.data")

拟合功能:

cox <- function(y, x, start = NULL, weights = NULL, offset = NULL, ... ) {
  x <- x[, -1]
  coxph(formula = y ~ 0 + x)
}

将 MOB 树拟合到GBSG2数据:

mb <- mob(formula = Surv(time, cens) ~ horTh + pnodes | age + tsize + tgrade + progrec + estrec + menostat, 
  data = GBSG2, fit = cox)
mb
## Model-based recursive partitioning (cox)
## 
## Model formula:
## Surv(time, cens) ~ horTh + pnodes | age + tsize + tgrade + progrec + 
##     estrec + menostat
## 
## Fitted party:
## [1] root: n = 686
##       xhorThyes     xpnodes 
##     -0.35701115  0.05768026  
## 
## Number of inner nodes:    0
## Number of terminal nodes: 1
## Number of parameters per node: 2
## Objective function: 1758.86
于 2016-03-15T14:12:17.253 回答