r语言 - 如何修复 coxph 函数中的"ran out of iterations and did not converge or more coefficientsmay be infinit



我想编写代码来计算数据集的coxph危险率。此数据有 5 个变量,其中 2 个用于 Surv((,其中两个用作协变量。现在我可以编写一个函数,它可以简单地计算输入 dataname 后两个协变量的危险率。但是,当我想使用相同的函数计算 3 个协变量的风险比时,程序说"迭代用完并且没有收敛或更多系数可能是无限的",并且结果包含所有五个变量作为协变量(应该是三个(。这是我的代码,任何人都可以纠正它吗?谢谢!

library(KMsurv)
library(survival)
data(larynx)
larynx2 = larynx[,c(2,5,1,3,4)]
larynx2$stage = as.factor(larynx2$stage)
mod = function(dataname){
    fit = coxph(Surv(dataname[,1],dataname[,2]) ~ ., data = dataname, ties = "breslow")
    return(list(result = summary(fit)))
}
mod(larynx2)

这个怎么样?由于公式中的列名有效,因此我们使用列名动态构建公式:

mod = function(dataname) {
    form = as.formula(sprintf("Surv(%s, %s) ~ .", names(dataname)[1], names(dataname)[2]))
    fit = coxph(form, data = dataname, ties = "breslow")
    return(list(result = summary(fit)))
}
mod(larynx2)
# $result
# Call:
# coxph(formula = form, data = dataname, ties = "breslow")
# 
#   n= 90, number of events= 50 
# 
#            coef exp(coef) se(coef)      z Pr(>|z|)    
# stage2  0.15078   1.16275  0.46459  0.325   0.7455    
# stage3  0.64090   1.89820  0.35616  1.799   0.0719 .  
# stage4  1.72100   5.59012  0.43660  3.942 8.09e-05 ***
# age     0.01855   1.01872  0.01432  1.295   0.1954    
# diagyr -0.01923   0.98096  0.07655 -0.251   0.8017    
# ---
# Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
# 
#        exp(coef) exp(-coef) lower .95 upper .95
# stage2     1.163     0.8600    0.4678     2.890
# stage3     1.898     0.5268    0.9444     3.815
# stage4     5.590     0.1789    2.3757    13.154
# age        1.019     0.9816    0.9905     1.048
# diagyr     0.981     1.0194    0.8443     1.140
# 
# Concordance= 0.676  (se = 0.039 )
# Rsquare= 0.182   (max possible= 0.988 )
# Likelihood ratio test= 18.13  on 5 df,   p=0.003
# Wald test            = 20.87  on 5 df,   p=9e-04
# Score (logrank) test = 24.4  on 5 df,   p=2e-04

相关内容

最新更新