cox_regression {tern} | R Documentation |
Cox proportional hazards regression
Description
Fits a Cox regression model and estimates hazard ratio to describe the effect size in a survival analysis.
Usage
summarize_coxreg(
lyt,
variables,
control = control_coxreg(),
at = list(),
multivar = FALSE,
common_var = "STUDYID",
.stats = c("n", "hr", "ci", "pval", "pval_inter"),
.formats = c(n = "xx", hr = "xx.xx", ci = "(xx.xx, xx.xx)", pval =
"x.xxxx | (<0.0001)", pval_inter = "x.xxxx | (<0.0001)"),
varlabels = NULL,
.indent_mods = NULL,
na_str = "",
.section_div = NA_character_
)
s_coxreg(model_df, .stats, .which_vars = "all", .var_nms = NULL)
a_coxreg(
df,
labelstr,
eff = FALSE,
var_main = FALSE,
multivar = FALSE,
variables,
at = list(),
control = control_coxreg(),
.spl_context,
.stats,
.formats,
.indent_mods = NULL,
na_str = "",
cache_env = NULL
)
Arguments
lyt |
( |
variables |
(named |
control |
( |
at |
( |
multivar |
( |
common_var |
( |
.stats |
(
|
.formats |
(named |
varlabels |
( |
.indent_mods |
(named |
na_str |
( |
.section_div |
( |
model_df |
( |
.which_vars |
( |
.var_nms |
( |
df |
( |
labelstr |
( |
eff |
( |
var_main |
( |
.spl_context |
( |
cache_env |
( |
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).
Value
-
summarize_coxreg()
returns a layout object suitable for passing to further layouting functions, or tortables::build_table()
. Adding this function to anrtable
layout will add a Cox regression table containing the chosen statistics to the table layout.
-
s_coxreg()
returns the selected statistic for from the Cox regression model for the selected variable(s).
-
a_coxreg()
returns formattedrtables::CellValue()
.
Functions
-
summarize_coxreg()
: Layout-creating function which creates a Cox regression summary table layout. This function is a wrapper for severalrtables
layouting functions. This function is a wrapper forrtables::analyze_colvars()
andrtables::summarize_row_groups()
. -
s_coxreg()
: Statistics function that transforms results tabulated fromfit_coxreg_univar()
orfit_coxreg_multivar()
into a list. -
a_coxreg()
: Analysis function which is used asafun
inrtables::analyze()
andcfun
inrtables::summarize_row_groups()
withinsummarize_coxreg()
.
See Also
fit_coxreg for relevant fitting functions, h_cox_regression for relevant helper functions, and tidy_coxreg for custom tidy methods.
fit_coxreg_univar()
and fit_coxreg_multivar()
which also take the variables
, data
,
at
(univariate only), and control
arguments but return unformatted univariate and multivariate
Cox regression models, respectively.
Examples
library(survival)
# Testing dataset [survival::bladder].
set.seed(1, kind = "Mersenne-Twister")
dta_bladder <- with(
data = bladder[bladder$enum < 5, ],
tibble::tibble(
TIME = stop,
STATUS = event,
ARM = as.factor(rx),
COVAR1 = as.factor(enum) %>% formatters::with_label("A Covariate Label"),
COVAR2 = factor(
sample(as.factor(enum)),
levels = 1:4, labels = c("F", "F", "M", "M")
) %>% formatters::with_label("Sex (F/M)")
)
)
dta_bladder$AGE <- sample(20:60, size = nrow(dta_bladder), replace = TRUE)
dta_bladder$STUDYID <- factor("X")
u1_variables <- list(
time = "TIME", event = "STATUS", arm = "ARM", covariates = c("COVAR1", "COVAR2")
)
u2_variables <- list(time = "TIME", event = "STATUS", covariates = c("COVAR1", "COVAR2"))
m1_variables <- list(
time = "TIME", event = "STATUS", arm = "ARM", covariates = c("COVAR1", "COVAR2")
)
m2_variables <- list(time = "TIME", event = "STATUS", covariates = c("COVAR1", "COVAR2"))
# summarize_coxreg
result_univar <- basic_table() %>%
summarize_coxreg(variables = u1_variables) %>%
build_table(dta_bladder)
result_univar
result_univar_covs <- basic_table() %>%
summarize_coxreg(
variables = u2_variables,
) %>%
build_table(dta_bladder)
result_univar_covs
result_multivar <- basic_table() %>%
summarize_coxreg(
variables = m1_variables,
multivar = TRUE,
) %>%
build_table(dta_bladder)
result_multivar
result_multivar_covs <- basic_table() %>%
summarize_coxreg(
variables = m2_variables,
multivar = TRUE,
varlabels = c("Covariate 1", "Covariate 2") # custom labels
) %>%
build_table(dta_bladder)
result_multivar_covs
# s_coxreg
# Univariate
univar_model <- fit_coxreg_univar(variables = u1_variables, data = dta_bladder)
df1 <- broom::tidy(univar_model)
s_coxreg(model_df = df1, .stats = "hr")
# Univariate with interactions
univar_model_inter <- fit_coxreg_univar(
variables = u1_variables, control = control_coxreg(interaction = TRUE), data = dta_bladder
)
df1_inter <- broom::tidy(univar_model_inter)
s_coxreg(model_df = df1_inter, .stats = "hr", .which_vars = "inter", .var_nms = "COVAR1")
# Univariate without treatment arm - only "COVAR2" covariate effects
univar_covs_model <- fit_coxreg_univar(variables = u2_variables, data = dta_bladder)
df1_covs <- broom::tidy(univar_covs_model)
s_coxreg(model_df = df1_covs, .stats = "hr", .var_nms = c("COVAR2", "Sex (F/M)"))
# Multivariate.
multivar_model <- fit_coxreg_multivar(variables = m1_variables, data = dta_bladder)
df2 <- broom::tidy(multivar_model)
s_coxreg(model_df = df2, .stats = "pval", .which_vars = "var_main", .var_nms = "COVAR1")
s_coxreg(
model_df = df2, .stats = "pval", .which_vars = "multi_lvl",
.var_nms = c("COVAR1", "A Covariate Label")
)
# Multivariate without treatment arm - only "COVAR1" main effect
multivar_covs_model <- fit_coxreg_multivar(variables = m2_variables, data = dta_bladder)
df2_covs <- broom::tidy(multivar_covs_model)
s_coxreg(model_df = df2_covs, .stats = "hr")
a_coxreg(
df = dta_bladder,
labelstr = "Label 1",
variables = u1_variables,
.spl_context = list(value = "COVAR1"),
.stats = "n",
.formats = "xx"
)
a_coxreg(
df = dta_bladder,
labelstr = "",
variables = u1_variables,
.spl_context = list(value = "COVAR2"),
.stats = "pval",
.formats = "xx.xxxx"
)