survfit2 | R Documentation |
Simple wrapper for survival::survfit()
except the environment is also
included in the returned object.
Use this function with all other functions in this package to ensure all elements are calculable.
survfit2(formula, ...)
formula |
a formula object, which must have a
|
... |
Arguments passed on to
|
survfit2 object
survfit2()
vs survfit()
Both functions have identical inputs, so why do we need survfit2()
?
The only difference between survfit2()
and survival::survfit()
is that the
former tracks the environment from which the call to the function was made.
The definition of survfit2()
is unremarkably simple:
survfit2 <- function(formula, ...) { # construct survfit object survfit <- survival::survfit(formula, ...) # add the environment survfit$.Environment = <calling environment> # add class and return class(survfit) <- c("survfit2", "survfit") survfit }
The environment is needed to ensure the survfit call can be accurately
reconstructed or parsed at any point post estimation.
The call is parsed when p-values are reported and when labels are created.
For example, the raw variable names appear in the output of a stratified
survfit()
result, e.g. "sex=Female"
. When using survfit2()
, the
originating data frame and formula may be parsed and the raw variable
names removed.
Most functions in the package work with both survfit2()
and survfit()
;
however, the output will be styled in a preferable format with survfit2()
.
survival::survfit.formula()
# With `survfit()`
fit <- survfit(Surv(time, status) ~ sex, data = df_lung)
fit
# With `survfit2()`
fit2 <- survfit2(Surv(time, status) ~ sex, data = df_lung)
fit2
# Consistent behavior with other functions
summary(fit, times = c(10, 20))
summary(fit2, times = c(10, 20))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.