From e6ed2f9244da5d5c6721ad33cdb9fd77c3376795 Mon Sep 17 00:00:00 2001 From: Michael McCarthy <51542091+mccarthy-m-g@users.noreply.github.com> Date: Sat, 3 Jun 2023 16:39:00 -0700 Subject: [PATCH 1/3] return standard errors for the survival probability, not the cumulative hazard --- R/survival-survfit-tidiers.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/survival-survfit-tidiers.R b/R/survival-survfit-tidiers.R index 1bbf8c547..f685d78b2 100644 --- a/R/survival-survfit-tidiers.R +++ b/R/survival-survfit-tidiers.R @@ -65,7 +65,7 @@ tidy.survfit <- function(x, ...) { n.event = c(x$n.event), n.censor = c(x$n.censor), estimate = c(x$pstate), - std.error = c(x$std.err), + std.error = c(summary(x)$std.err), conf.high = c(x$upper), conf.low = c(x$lower), state = rep(x$states, each = nrow(x$pstate)) @@ -79,7 +79,7 @@ tidy.survfit <- function(x, ...) { n.event = x$n.event, n.censor = x$n.censor, estimate = x$surv, - std.error = x$std.err, + std.error = summary(x)$std.err, conf.high = x$upper, conf.low = x$lower ) From 03a4662da40ae34d07597a319d46d0514cba3fa8 Mon Sep 17 00:00:00 2001 From: Michael McCarthy <51542091+mccarthy-m-g@users.noreply.github.com> Date: Sun, 4 Jun 2023 16:47:04 -0700 Subject: [PATCH 2/3] compute std.error manually instead of using `summary()` for compatibility with objects reformulated by `survfit0()` --- R/survival-survfit-tidiers.R | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/R/survival-survfit-tidiers.R b/R/survival-survfit-tidiers.R index f685d78b2..235357474 100644 --- a/R/survival-survfit-tidiers.R +++ b/R/survival-survfit-tidiers.R @@ -65,7 +65,9 @@ tidy.survfit <- function(x, ...) { n.event = c(x$n.event), n.censor = c(x$n.censor), estimate = c(x$pstate), - std.error = c(summary(x)$std.err), + # A survfit object may contain std(log S) or std(S), tidy/summary should + # always be std(S). + std.error = c(x$std.err * x$surv), conf.high = c(x$upper), conf.low = c(x$lower), state = rep(x$states, each = nrow(x$pstate)) @@ -79,7 +81,9 @@ tidy.survfit <- function(x, ...) { n.event = x$n.event, n.censor = x$n.censor, estimate = x$surv, - std.error = summary(x)$std.err, + # A survfit object may contain std(log S) or std(S), tidy/summary should + # always be std(S). + std.error = x$std.err * x$surv, conf.high = x$upper, conf.low = x$lower ) From 73638a76130eab585da0b1835926d753cb302007 Mon Sep 17 00:00:00 2001 From: Michael McCarthy <51542091+mccarthy-m-g@users.noreply.github.com> Date: Sun, 4 Jun 2023 16:47:04 -0700 Subject: [PATCH 3/3] compute std.error manually instead of using `summary()` for compatibility with objects reformulated by `survfit0()` --- R/survival-survfit-tidiers.R | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/R/survival-survfit-tidiers.R b/R/survival-survfit-tidiers.R index f685d78b2..208a8e712 100644 --- a/R/survival-survfit-tidiers.R +++ b/R/survival-survfit-tidiers.R @@ -58,6 +58,10 @@ #' tidy.survfit <- function(x, ...) { if (inherits(x, "survfitms")) { + # A survfit object may contain std(log S) or std(S), tidy/summary should + # always be std(S). + if (!is.null(x$std.err) && x$logse) x$std.err <- x$std.err * x$pstate + # c() coerces to vector ret <- data.frame( time = x$time, @@ -65,7 +69,7 @@ tidy.survfit <- function(x, ...) { n.event = c(x$n.event), n.censor = c(x$n.censor), estimate = c(x$pstate), - std.error = c(summary(x)$std.err), + std.error = c(x$std.err), conf.high = c(x$upper), conf.low = c(x$lower), state = rep(x$states, each = nrow(x$pstate)) @@ -73,13 +77,17 @@ tidy.survfit <- function(x, ...) { ret <- ret[ret$state != "", ] } else { + # A survfit object may contain std(log S) or std(S), tidy/summary should + # always be std(S). + if (!is.null(x$std.err) && x$logse) x$std.err <- x$std.err * x$surv + ret <- data.frame( time = x$time, n.risk = x$n.risk, n.event = x$n.event, n.censor = x$n.censor, estimate = x$surv, - std.error = summary(x)$std.err, + std.error = x$std.err, conf.high = x$upper, conf.low = x$lower )