如何从R中的Coxph模型摘要中提取公式? [英] how to extract formula from coxph model summary in R?

查看:178
本文介绍了如何从R中的Coxph模型摘要中提取公式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够按照本教程中的概述复制coxph模型和提取组件: http://www.sthda.com/english/wiki/cox-proportional-hazards-model .但是,我正在努力从模型中提取公式对象.

I am able to replicate the coxph model and the extraction of components as outlined in the tutorial here:http://www.sthda.com/english/wiki/cox-proportional-hazards-model. However, I am struggling to extract the formula object from the model.

library("survival")
library("survminer")
data("lung")
head(lung)

covariates <- c("age", "sex",  "ph.karno", "ph.ecog", "wt.loss")
univ_formulas <- sapply(covariates,
                        function(x) as.formula(paste('Surv(time, status)~', x)))

univ_models <- lapply( univ_formulas, function(x){coxph(x, data = lung)})
# Extract data 
univ_results <- lapply(univ_models,
                       function(x){ 
                         x <- summary(x)
                         p.value<-signif(x$wald["pvalue"], digits=2)
                         wald.test<-signif(x$wald["test"], digits=2)
                         beta<-signif(x$coef[1], digits=2);#coeficient beta
                         HR <-signif(x$coef[2], digits=2);#exp(beta)
                         HR.confint.lower <- signif(x$conf.int[,"lower .95"], 2)
                         HR.confint.upper <- signif(x$conf.int[,"upper .95"],2)
                         HR <- paste0(HR, " (", 
                                      HR.confint.lower, "-", HR.confint.upper, ")")
                         res<-c(beta, HR, wald.test, p.value)
                         names(res)<-c("beta", "HR (95% CI for HR)", "wald.test", 
                                       "p.value")
                         return(res)
                         #return(exp(cbind(coef(x),confint(x))))
                       })
res <- t(as.data.frame(univ_results, check.names = FALSE))
as.data.frame(res)

我尝试将以下语句添加到函数(x)中,但没有成功:

I have tried adding the following statement to the function(x) without success:

formula_extract <- paste( c(x$formula[[2]],
                                                x$formula[[1]],
                                                x$formula[[3]]), collapse='')

添加语句看起来像这样,结果数据帧"res"包含该列,但全为空(甚至不适用).

adding the statement looks like this and the resulting data frame "res" contains the column but it is all empty (not even NA).

# Extract data 
univ_results <- lapply(univ_models,
                       function(x){ 
                         x <- summary(x)
                         p.value<-signif(x$wald["pvalue"], digits=2)
                         wald.test<-signif(x$wald["test"], digits=2)
                         beta<-signif(x$coef[1], digits=2);#coeficient beta
                         HR <-signif(x$coef[2], digits=2);#exp(beta)
                         HR.confint.lower <- signif(x$conf.int[,"lower .95"], 2)
                         HR.confint.upper <- signif(x$conf.int[,"upper .95"],2)
                         HR <- paste0(HR, " (", 
                                      HR.confint.lower, "-", HR.confint.upper, ")")
formula_extract <- paste( c(x$formula[[2]],
                                                    x$formula[[1]],
                                                    x$formula[[3]]), collapse='')

                         res<-c(beta, HR, wald.test, p.value, formula_extract)
                         names(res)<-c("beta", "HR (95% CI for HR)", "wald.test", 
                                       "p.value", "formula_extract")
                         return(res)
                         #return(exp(cbind(coef(x),confint(x))))
                       })
res <- t(as.data.frame(univ_results, check.names = FALSE))
as.data.frame(res)

但是,它本身可以工作:

However, by itself this works:

paste( c(univ_models$age$formula[[2]],
                 univ_models$age$formula[[1]],
                 univ_models$age$formula[[3]]), collapse='')

任何帮助将不胜感激!

推荐答案

所有您需要做的就是使用其他变量:ie

all you need to do is to use a different variable: ie

univ_results <- lapply(univ_models,
                       function(y){ 
                         x <- summary(y)
                             :
                             :
                        formula_extract <- deparse(y$formula)
                             :
                        }


as.data.frame(res)
           beta HR (95% CI for HR) wald.test p.value               formula_extract
age       0.019            1 (1-1)       4.1   0.042      Surv(time, status) ~ age
sex       -0.53   0.59 (0.42-0.82)        10  0.0015      Surv(time, status) ~ sex
ph.karno -0.016      0.98 (0.97-1)       7.9   0.005 Surv(time, status) ~ ph.karno
ph.ecog    0.48        1.6 (1.3-2)        18 2.7e-05  Surv(time, status) ~ ph.ecog
wt.loss  0.0013         1 (0.99-1)      0.05    0.83  Surv(time, status) ~ wt.loss

这篇关于如何从R中的Coxph模型摘要中提取公式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆