为什么我在 R 中的 cox 模型的 ANOVA 测试中出错?

Why do I get an error in ANOVA test on cox models in R?

我想在 R 中的两个模型之间进行似然比检验。

尽管如此,当我执行 anova(mod2,mod3) 时,我收到此错误消息:

Error in anova.coxphlist(c(list(object), dotargs), test = test) :
models were not all fitted to the same size of dataset In addition: Warning message: In anova.coxphlist(c(list(object), dotargs), test = test) : Models with response c(" sep = \"\"))))", " sep = \"\"))))") removed because response differs from model 1

对不起,我无法提供我使用的数据样本。 我在下面给出了我的 2 个模型的摘要:

> mod2
Call:
coxph(formula = Surv(eval(parse(text = paste(j, "_followup", sep = ""))), 
    eval(parse(text = paste(j, sep = "")))) ~ strata(trt) + Var1 + 
    Var2 + Var3 + rcs(Var4, 4), 
    data = data.clean)

                      coef exp(coef)  se(coef)      z       p
Var1               0.030361  1.030827  0.012188  2.491 0.01274
Var2               0.022405  1.022658  0.007463  3.002 0.00268
Var3:1             0.233716  1.263286  0.433950  0.539 0.59018
rcs(Var4, 4)Var4   0.315988  1.371613  0.111433  2.836 0.00457
rcs(Var4, 4)Var4' -0.799272  0.449656  0.378427 -2.112 0.03468

Likelihood ratio test=37.7  on 5 df, p=4.329e-07
n= 375, number of events= 74 
> mod3
Call:
coxph(formula = Surv(eval(parse(text = paste(j, "_followup", sep = ""))), 
    eval(parse(text = paste(j, sep = "")))) ~ strata(trt) + Var1 + 
    Var2 +Var3 + rcs(Var4, 4) + 
    eval(parse(text = paste(i, sep = ""))), data = data.clean)

                        coef exp(coef)  se(coef)      z       p
Var1                0.024004  1.024295  0.012742  1.884 0.05959
Var2                0.018190  1.018356  0.007881  2.308 0.02099
Var3:1              0.348887  1.417489  0.437326  0.798 0.42500
rcs(Var4, 4)Var4    0.289575  1.335859  0.110936  2.610 0.00905
rcs(Var4, 4)Var4'  -0.705976  0.493627  0.377903 -1.868 0.06174
eval(parse(text = paste(i, sep = "")))  0.742461  2.101100  0.304491  2.438 0.01475

Likelihood ratio test=43.69  on 6 df, p=8.499e-08
n= 375, number of events= 74 



>  anova(mod2,mod3)
    Error in anova.coxphlist(c(list(object), dotargs), test = test) : 
      models were not all fitted to the same size of dataset
    In addition: Warning message:
    In anova.coxphlist(c(list(object), dotargs), test = test) :
      Models with response c("    sep = \"\"))))", "    sep = \"\"))))") removed because response differs from model 1

也许是因为存在分层术语?或者 Var4 ?

的花键拟合版本的存在

感谢您的建议!

问题是anova函数使用了coxph函数的formula。 如果公式中包含evalpaste等,anova函数可能不起作用

为了解决这个问题,我们有两个解决方案:

1) 按照@StupidWolf 在上面评论中的建议,在对两个模型中的每一个应用 coxph 之后指定 f = as.formula(...)

2 ) 计算无特定函数的anova的不同元素如下:

Df = sum(anova(mod2)$Df, na.rm = T) - sum(anova(mod1)$Df, na.rm = T)
Chisq = abs(as.numeric(logLik(mod2) - logLik(mod1)) * 2)
pval = pchisq(Chisq, Df, lower.tail=F)