提取具有与data.frame竞争风险的survival :: survfit对象的摘要

时间:2017-12-15 11:03:40

标签: r

提取具有与data.frame竞争风险的survival :: survfit对象的摘要。

图片中的代码打印了survfit-object的摘要。 但是如何将其保存为data.frame?

http://xx.xx.xx.xx/staging/

broom :: tidy()可以从survfit-object中提取data.frame,但在survfit()中 - 具有竞争风险的对象获取n.risk(它始终为零),如下所述:{{ 3}}

enter image description here

# data
library(survival)
library(tidyverse)
data(lung)
lung$status_competing_risk <- sample(x = c("censor", "main event", "competing risk 1", "competing risk 2"), size = nrow(lung), replace = TRUE )
lung$status_competing_risk <- factor( lung$status_competing_risk) %>% relevel( ref = "censor" )
# model object
fit <- survfit( Surv( time, status_competing_risk ) ~ as.factor(sex), lung ) 

1 个答案:

答案 0 :(得分:1)

使用生存的修改版本::: print.summary.survfitms

print_summary_to_dataframe <- function(x, 
                                digits = max(options()$digits - 4, 3), ...) {
savedig <- options(digits=digits)
on.exit(options(savedig))

if (!is.null(cl<- x$call)) {



}
tsum <- function(x) {
    if (is.matrix(x)) rowSums(x) else x
}
omit <- x$na.action
if (length(omit)) 
    cat(naprint(omit), "\n")
if (x$type == 'mright' || is.null(x$n.enter)) {
    mat <- cbind(x$time, tsum(x$n.risk), tsum(x$n.event), x$pstate)
    cnames <- c("time", "n.risk", "n.event")
}  else if (x$type == 'mcounting') {
    mat <- cbind(x$time, tsum(x$n.risk), tsum(x$n.event), x$pstate,)
    cnames <- c("time", "n.risk", "n.event")
}

if (is.matrix(x$pstate)) ncurve <- ncol(x$pstate) else             ncurve <- 1
if (ncurve==1) {                 #only 1 curve
    cnames <- c(cnames, "P")
    if (!is.null(x$std.err)) {
        if (is.null(x$lower)) {
            mat <- cbind(mat, x$std.err)
            cnames <- c(cnames, "std.err")
        } else {
            mat <- cbind(mat, x$std.err, x$lower, x$upper)
            cnames <- c(cnames, 'std.err',
                        paste("lower ", x$conf.int*100, "% CI", sep=''),
                        paste("upper ", x$conf.int*100, "% CI", sep=''))
        }   
    }
} else cnames <- c(cnames, paste0("P(", x$states[1:ncurve], ")"))

if (!is.null(x$start.time)) {
    mat.keep <- mat[,1] >= x$start.time
    mat <- mat[mat.keep,,drop=FALSE]
    if (is.null(dim(mat)))
        stop(paste("No information available using start.time =", x$start.time, "."))
}
if (!is.matrix(mat)) mat <- matrix(mat, nrow=1)

if (is.null(mat)) {
    stop("There are no events to print.  Please use the option ",
         "censored=TRUE with the summary function to see the censored ",
         "observations.")
}

if (!is.null(mat)) {
    dimnames(mat) <- list(rep("", nrow(mat)), cnames)
    if (is.null(x$strata)) print(mat) else  { #print it out one strata at a time

        strata <- x$strata
        if (!is.null(x$start.time)) strata <- strata[mat.keep]

        data_frame <- data.frame(mat, check.names = FALSE) 
        head(data_frame)
        data_frame$strata <- strata

        }
}
data_frame
}

使用指定strata:

的列将输出打印为data.frame
print_summary_to_dataframe(fit_summary )

输出:

> print_summary_to_dataframe(fit_summary  ) %>% head()
  time n.risk n.event P(competing risk 1) P(competing risk 2) P(main event)       P()           strata
1   11    138       3         0.000000000          0.01449275   0.007246377 0.9782609 as.factor(sex)=1
2   12    135       1         0.007246377          0.01449275   0.007246377 0.9710145 as.factor(sex)=1
3   13    134       2         0.007246377          0.02173913   0.014492754 0.9565217 as.factor(sex)=1
4   15    132       1         0.007246377          0.02173913   0.021739130 0.9492754 as.factor(sex)=1
5   26    131       1         0.007246377          0.02173913   0.028985507 0.9420290 as.factor(sex)=1
6   30    130       1         0.007246377          0.02173913   0.036231884 0.9347826 as.factor(sex)=1