add_median_survival: Calculate median survival time

View source: R/add_median_survival.R

add_median_survivalR Documentation

Calculate median survival time

Description

Fitting a survival fit for each subgroup defined by var and extracting median survival times. Additionally the total survival time for the whole sample is calculated. This function fits a survival curve for each subgroup defined by var and extracts the median survival times. Additionally the median survival time for the whole sample is calculated.

Usage

add_median_survival(
  data,
  time,
  status,
  var,
  round = 1,
  statistics = TRUE,
  weights = NULL,
  conf.type = "log-log"
)

Arguments

data

data.frame or data.table containing survival data.

time

The time interval from start of observation until date of event (e.g. disease progression or death) or censoring.

status

Variable specifying if event occured or data has been censored. Default behaviour inherited from the surv_fit function in the survival package, with 0 indicating censored data and 1 indicating event.

var

Variable tested for Influence on outcome.

round

rounds the results to the specified number of decimal places (default 1)

statistics

Logical value. If TRUE pvalue is printed. Default is TRUE. Statistical test is log-rank test.

weights

character variable specifying the name of the weights column. Weights (numerical vector) have to be added to the original dataframe in order to be applied correctly.

conf.type

Method to calculate confidence intervals. Log-log method is the default in SAS and was therefore set as the default here too.

Examples

# example survival calculation with the lung dataset
df <- survival::lung
add_median_survival(data = df, time = "time", status = "status", var = "ph.ecog")
# calculate median survival with inverse propensity score weighting for sex
df$sex <- factor(df$sex)
df$ph.ecog <- ifelse(is.na(df$ph.ecog), "Missing", df$ph.ecog)
df$ph.karno <- ifelse(is.na(df$ph.karno), "Missing", df$ph.karno)
df$weights.ate <- ate_weights(data = df, vars = c("age", "ph.ecog", "ph.karno"), prop.var = "sex")
# the internal logrank test (logrank_IPSW_RISCA) needs a status variable of 0 and 1
df$status <- ifelse(df$status == 2, 1, 0)
add_median_survival(data = df, time = "time", status = "status",
var = "sex", weights = "weights.ate")

MBender1992/emR documentation built on Feb. 18, 2025, 9:21 a.m.