Skip to content

Commit

Permalink
Merge 7678607 into 605de00
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua authored Jun 21, 2023
2 parents 605de00 + 7678607 commit 5f3796a
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 13 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ S3method(a_summary,factor)
S3method(a_summary,logical)
S3method(a_summary,numeric)
S3method(as.rtable,data.frame)
S3method(h_coxreg_inter_effect,character)
S3method(h_coxreg_inter_effect,factor)
S3method(h_coxreg_inter_effect,numeric)
S3method(s_compare,character)
Expand Down
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# tern 0.8.3.9001

### Enhancements
* Added method for `character` class to `h_coxreg_inter_effect` enabling `character` covariates in `summarize_coxreg`.

### Miscellaneous
* Began deprecation of `time_unit_input` and `time_unit_output` arguments and replaced them with the `input_time_unit` and `n_pt_years_rate`, respectively, in `control_incidence_rate`.
* Began deprecation of `time_unit_input` and `time_unit_output` arguments and replaced them with the `input_time_unit` and `num_pt_year`, respectively, in `control_incidence_rate`.

# tern 0.8.3

Expand Down
2 changes: 1 addition & 1 deletion R/control_incidence_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' for confidence interval type.
#' @param input_time_unit (`string`)\cr `day`, `week`, `month`, or `year` (default)
#' indicating time unit for data input.
#' @param num_pt_year (`numeric`)\cr number of patient-years to use when calculating AE rate.
#' @param num_pt_year (`numeric`)\cr number of patient-years to use when calculating adverse event rates.
#' @param time_unit_input `r lifecycle::badge("deprecated")` Please use the `input_time_unit` argument instead.
#' @param time_unit_output `r lifecycle::badge("deprecated")` Please use the `num_pt_year` argument instead.
#'
Expand Down
50 changes: 44 additions & 6 deletions R/cox_regression_inter.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' of the covariate, in comparison to the treatment control.
#'
#' @inheritParams argument_convention
#' @param x (`numeric` or `factor`)\cr the values of the effect to be tested.
#' @param x (`numeric` or `factor`)\cr the values of the covariate to be tested.
#' @param effect (`string`)\cr the name of the effect to be tested and estimated.
#' @param covar (`string`)\cr the name of the covariate in the model.
#' @param mod (`coxph`)\cr the Cox regression model.
Expand Down Expand Up @@ -66,7 +66,9 @@ h_coxreg_inter_effect <- function(x,
UseMethod("h_coxreg_inter_effect", x)
}

#' @describeIn cox_regression_inter Estimate the interaction with a `numeric` covariate.
#' @describeIn cox_regression_inter Method for `numeric` class. Estimates the interaction with a `numeric` covariate.
#'
#' @method h_coxreg_inter_effect numeric
#'
#' @param at (`list`)\cr a list with items named after the covariate, every
#' item is a vector of levels at which the interaction should be estimated.
Expand Down Expand Up @@ -118,7 +120,9 @@ h_coxreg_inter_effect.numeric <- function(x,
)
}

#' @describeIn cox_regression_inter Estimate the interaction with a `factor` covariate.
#' @describeIn cox_regression_inter Method for `factor` class. Estimate the interaction with a `factor` covariate.
#'
#' @method h_coxreg_inter_effect factor
#'
#' @param data (`data.frame`)\cr the data frame on which the model was fit.
#'
Expand All @@ -131,19 +135,20 @@ h_coxreg_inter_effect.factor <- function(x,
control,
data,
...) {
lvl_given <- levels(x)
y <- h_coxreg_inter_estimations(
variable = effect, given = covar,
lvl_var = levels(data[[effect]]),
lvl_given = levels(data[[covar]]),
lvl_given = lvl_given,
mod = mod,
conf_level = 0.95
)[[1]]

data.frame(
effect = "Covariate:",
term = rep(covar, nrow(y)),
term_label = as.character(paste0(" ", levels(data[[covar]]))),
level = as.character(levels(data[[covar]])),
term_label = paste0(" ", lvl_given),
level = lvl_given,
n = NA,
hr = y[, "hr"],
lcl = y[, "lcl"],
Expand All @@ -154,6 +159,39 @@ h_coxreg_inter_effect.factor <- function(x,
)
}

#' @describeIn cox_regression_inter Method for `character` class. Estimate the interaction with a `character` covariate.
#' This makes an automatic conversion to `factor` and then forwards to the method for factors.
#'
#' @method h_coxreg_inter_effect character
#'
#' @note
#' * Automatic conversion of character to factor does not guarantee results can be generated correctly. It is
#' therefore better to always pre-process the dataset such that factors are manually created from character
#' variables before passing the dataset to [rtables::build_table()].
#'
#' @export
h_coxreg_inter_effect.character <- function(x,
effect,
covar,
mod,
label,
control,
data,
...) {
y <- as.factor(x)

h_coxreg_inter_effect(
x = y,
effect = effect,
covar = covar,
mod = mod,
label = label,
control = control,
data = data,
...
)
}

#' @describeIn cox_regression_inter A higher level function to get
#' the results of the interaction test and the estimated values.
#'
Expand Down
2 changes: 1 addition & 1 deletion man/control_incidence_rate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions man/cox_regression_inter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions tests/testthat/test-coxreg.R
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,34 @@ testthat::test_that("h_coxreg_inter_effect.numerics works with _:_ in effect lev
testthat::expect_equal(result[, -1], expected[, -1], ignore_attr = TRUE)
})

testthat::test_that("h_coxreg_inter_effect works with character covariate", {
dta_bladder_raw$covar2 <- as.character(dta_bladder_raw$covar2)

mod1 <- survival::coxph(survival::Surv(time, status) ~ armcd * covar2, data = dta_bladder_raw)
testthat::expect_silent(
h_coxreg_extract_interaction(
effect = "armcd", covar = "covar2", mod = mod1, control = control_coxreg(),
at = list(), data = dta_bladder_raw
)
)
testthat::expect_silent(
h_coxreg_inter_effect(
x = dta_bladder_raw[["covar2"]],
effect = "armcd", covar = "covar2", mod = mod1, control = control_coxreg(),
at = list(), data = dta_bladder_raw
)
)

mod2 <- survival::coxph(survival::Surv(time, status) ~ armcd * covar2 + strata(covar1), data = dta_bladder_raw)
testthat::expect_silent(
h_coxreg_inter_effect(
x = dta_bladder_raw[["covar2"]],
effect = "armcd", covar = "covar2", mod = mod2, data = dta_bladder_raw,
at = list(), control = control_coxreg()
)
)
})

# h_coxreg_inter_estimations ----

testthat::test_that("h_coxreg_inter_estimations' results identical to soon deprecated estimate_coef", {
Expand Down
44 changes: 43 additions & 1 deletion tests/testthat/test-summarize_coxreg.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dta_bladder <- local({
tibble::tibble(
TIME = stop,
STATUS = event,
ARM = paste("ARM:", as.factor(rx)),
ARM = as.factor(paste("ARM:", as.factor(rx))),
ARMCD = formatters::with_label(as.factor(rx), "ARM"),
COVAR1 = formatters::with_label(as.factor(enum), "A Covariate Label"),
COVAR2 = formatters::with_label(
Expand Down Expand Up @@ -48,6 +48,18 @@ testthat::test_that("s_coxreg works with which_vars and var_nms arguments", {
testthat::expect_snapshot(res)
})

testthat::test_that("s_coxreg works with character covariates in the univariate case when interaction = TRUE", {
univar_model <- fit_coxreg_univar(
variables = variables,
data = dta_bladder,
control = control_coxreg(interaction = TRUE)
) %>% broom::tidy()
result <- s_coxreg(model_df = univar_model, .stat = "hr")

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

# a_coxreg ----

testthat::test_that("a_coxreg works as expected", {
Expand Down Expand Up @@ -184,6 +196,36 @@ testthat::test_that("summarize_coxreg works with numeric covariate without treat
testthat::expect_snapshot(res)
})

testthat::test_that("summarize_coxreg works with character covariate in univariate case when interaction = TRUE", {
# one character covariate
variables <- list(time = "TIME", event = "STATUS", arm = "ARM", covariates = "COVAR2")
dta_bladder$COVAR2 <- as.character(dta_bladder$COVAR2)

result <- basic_table() %>%
summarize_coxreg(
variables = variables,
control = control_coxreg(interaction = TRUE)
) %>%
build_table(df = dta_bladder)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)

# one factor covariate, one character covariate
variables <- list(time = "TIME", event = "STATUS", arm = "ARM", covariates = c("COVAR1", "COVAR2"))

result <- basic_table() %>%
summarize_coxreg(
variables = variables,
control = control_coxreg(interaction = TRUE)
) %>%
build_table(df = dta_bladder)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})


testthat::test_that("summarize_coxreg adds the multivariate Cox regression layer to rtables", {
variables <- list(time = "TIME", event = "STATUS", arm = "ARMCD", covariates = c("AGE", "COVAR1", "COVAR2"))

Expand Down

0 comments on commit 5f3796a

Please sign in to comment.