Skip to contents

[Stable]

Fits a Cox regression model and estimate hazard ratio to describe the effect size in a survival analysis.

Usage

s_coxreg(df, .var)

summarize_coxreg(
  lyt,
  conf_level,
  multivar = FALSE,
  vars = c("n", "hr", "ci", "pval")
)

Arguments

df

(data.frame)
data set containing all analysis variables.

.var

(string)
single variable name that is passed by rtables when requested by a statistics function.

lyt

(layout)
input layout where analyses will be added to.

conf_level

(proportion)
confidence level of the interval.

multivar

(flag)
if TRUE, the multi-variable Cox regression will run and no interaction will be considered between the studied treatment and c candidate covariate. Default is FALSE for univariate Cox regression including an arm variable. When no arm variable is included in the univariate Cox regression, then also TRUE should be used to tabulate the covariate effect estimates instead of the treatment arm effect estimate across models.

vars

(character)
the name of statistics to be reported among n (number of observation), hr (Hazard Ratio), ci (confidence interval), pval (p.value of the treatment effect) and pval_inter (the p.value of the interaction effect between the treatment and the covariate).

Details

Cox models are the most commonly used methods to estimate the magnitude of the effect in survival analysis. It assumes proportional hazards: the ratio of the hazards between groups (e.g., two arms) is constant over time. This ratio is referred to as the "hazard ratio" (HR) and is one of the most commonly reported metrics to describe the effect size in survival analysis (NEST Team, 2020).

Functions

  • s_coxreg(): transforms the tabulated results from fit_coxreg_univar() and fit_coxreg_multivar() into a list. Not much calculation is done here, it rather prepares the data to be used by the layout creating function.

  • summarize_coxreg(): layout creating function.

Note

The usual formatting arguments for the layout creating function summarize_coxreg are not yet accepted (.stats, .indent_mod, .formats, .labels).

See also

fit_coxreg for relevant fitting functions, h_cox_regression for relevant helper functions, and tidy_coxreg for custom tidy methods.

Examples

library(survival)

# Testing dataset [survival::bladder].
set.seed(1, kind = "Mersenne-Twister")
dta_bladder <- with(
  data = bladder[bladder$enum < 5, ],
  data.frame(
    time = stop,
    status = event,
    armcd = as.factor(rx),
    covar1 = as.factor(enum),
    covar2 = factor(
      sample(as.factor(enum)),
      levels = 1:4, labels = c("F", "F", "M", "M")
    )
  )
)
labels <- c("armcd" = "ARM", "covar1" = "A Covariate Label", "covar2" = "Sex (F/M)")
formatters::var_labels(dta_bladder)[names(labels)] <- labels
dta_bladder$age <- sample(20:60, size = nrow(dta_bladder), replace = TRUE)

plot(
  survfit(Surv(time, status) ~ armcd + covar1, data = dta_bladder),
  lty = 2:4,
  xlab = "Months",
  col = c("blue1", "blue2", "blue3", "blue4", "red1", "red2", "red3", "red4")
)

# s_coxreg

univar_model <- fit_coxreg_univar(
  variables = list(
    time = "time", event = "status", arm = "armcd",
    covariates = c("covar1", "covar2")
  ),
  data = dta_bladder
)
df1 <- broom::tidy(univar_model)
s_coxreg(df = df1, .var = "hr")
#> $hr
#> $hr$`2 vs control (1)`
#> [1] 0.6386426
#> 
#> 
#> $hr
#> $hr$`A Covariate Label`
#> [1] 0.607037
#> 
#> 
#> $hr
#> $hr$`Sex (F/M)`
#> [1] 0.6242738
#> 
#> 

# Only covariates.
univar_covs_model <- fit_coxreg_univar(
  variables = list(
    time = "time", event = "status",
    covariates = c("covar1", "covar2")
  ),
  data = dta_bladder
)
df1_covs <- broom::tidy(univar_covs_model)
s_coxreg(df = df1_covs, .var = "hr")
#> $hr
#> $hr$`2`
#> [1] 0.446633
#> 
#> $hr$`3`
#> [1] 0.3098658
#> 
#> $hr$`4`
#> [1] 0.1828626
#> 
#> 
#> $hr
#> $hr$M
#> [1] 1.3271
#> 
#> 
#> $hr
#> $hr$`A Covariate Label (reference = 1)`
#> numeric(0)
#> 
#> 
#> $hr
#> $hr$`Sex (F/M) (reference = F)`
#> numeric(0)
#> 
#> 

# Multivariate.
multivar_model <- fit_coxreg_multivar(
  variables = list(
    time = "time", event = "status", arm = "armcd",
    covariates = c("covar1", "covar2")
  ),
  data = dta_bladder
)
df2 <- broom::tidy(multivar_model)
s_coxreg(df = df2, .var = "hr")
#> $hr
#> $hr$`2`
#> [1] 0.4564763
#> 
#> $hr$`3`
#> [1] 0.3069612
#> 
#> $hr$`4`
#> [1] 0.1817017
#> 
#> 
#> $hr
#> $hr$`2`
#> [1] 0.6062777
#> 
#> 
#> $hr
#> $hr$M
#> [1] 1.289373
#> 
#> 
#> $hr
#> $hr$`ARM (reference = 1)`
#> numeric(0)
#> 
#> 
#> $hr
#> $hr$`A Covariate Label (reference = 1)`
#> numeric(0)
#> 
#> 
#> $hr
#> $hr$`Sex (F/M) (reference = F)`
#> numeric(0)
#> 
#> 

# Multivariate without treatment arm.
multivar_covs_model <- fit_coxreg_multivar(
  variables = list(
    time = "time", event = "status",
    covariates = c("covar1", "covar2")
  ),
  data = dta_bladder
)
df2_covs <- broom::tidy(multivar_covs_model)
s_coxreg(df = df2_covs, .var = "hr")
#> $hr
#> $hr$`2`
#> [1] 0.4600728
#> 
#> $hr$`3`
#> [1] 0.3100455
#> 
#> $hr$`4`
#> [1] 0.1854177
#> 
#> 
#> $hr
#> $hr$M
#> [1] 1.285406
#> 
#> 
#> $hr
#> $hr$`A Covariate Label (reference = 1)`
#> numeric(0)
#> 
#> 
#> $hr
#> $hr$`Sex (F/M) (reference = F)`
#> numeric(0)
#> 
#> 
# summarize_coxreg
result_univar <- basic_table() %>%
  split_rows_by("effect") %>%
  split_rows_by("term", child_labels = "hidden") %>%
  summarize_coxreg(conf_level = 0.95) %>%
  build_table(df1)
result_univar
#>                        n    Hazard Ratio      95% CI      p-value
#> —————————————————————————————————————————————————————————————————
#> Treatment:                                                       
#>   2 vs control (1)    340       0.64       (0.43, 0.94)   0.0242 
#> Covariate:                                                       
#>   A Covariate Label   340       0.61       (0.41, 0.90)   0.0126 
#>   Sex (F/M)           340       0.62       (0.42, 0.92)   0.0182 

result_multivar <- basic_table() %>%
  split_rows_by("term", child_labels = "hidden") %>%
  summarize_coxreg(multivar = TRUE, conf_level = .95) %>%
  build_table(df2)
result_multivar
#>                                     Hazard Ratio      95% CI      p-value
#> —————————————————————————————————————————————————————————————————————————
#> ARM (reference = 1)                                                      
#> 2                                       0.61       (0.41, 0.90)   0.0123 
#> A Covariate Label (reference = 1)                                 <0.0001
#> 2                                       0.46       (0.28, 0.73)   0.0011 
#> 3                                       0.31       (0.18, 0.51)   <0.0001
#> 4                                       0.18       (0.10, 0.33)   <0.0001
#> Sex (F/M) (reference = F)                                                
#> M                                       1.29       (0.88, 1.89)   0.1911 

# When tabulating univariate models with only covariates, also `multivar = TRUE`
# is used.
result_univar_covs <- basic_table() %>%
  split_rows_by("term", child_labels = "hidden") %>%
  summarize_coxreg(multivar = TRUE, conf_level = 0.95) %>%
  build_table(df1_covs)
result_univar_covs
#>                                     Hazard Ratio      95% CI      p-value
#> —————————————————————————————————————————————————————————————————————————
#> A Covariate Label (reference = 1)                                 <0.0001
#> 2                                       0.45       (0.28, 0.71)   0.0007 
#> 3                                       0.31       (0.19, 0.52)   <0.0001
#> 4                                       0.18       (0.10, 0.33)   <0.0001
#> Sex (F/M) (reference = F)                                                
#> M                                       1.33       (0.91, 1.94)   0.1414 

# No change for the multivariate tabulation when no treatment arm is included.
result_multivar_covs <- basic_table() %>%
  split_rows_by("term", child_labels = "hidden") %>%
  summarize_coxreg(multivar = TRUE, conf_level = .95) %>%
  build_table(df2_covs)
result_multivar_covs
#>                                     Hazard Ratio      95% CI      p-value
#> —————————————————————————————————————————————————————————————————————————
#> A Covariate Label (reference = 1)                                 <0.0001
#> 2                                       0.46       (0.29, 0.74)   0.0012 
#> 3                                       0.31       (0.19, 0.52)   <0.0001
#> 4                                       0.19       (0.10, 0.34)   <0.0001
#> Sex (F/M) (reference = F)                                                
#> M                                       1.29       (0.88, 1.88)   0.1958