Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add character method to h_coxreg_inter_effect #974

Merged
merged 19 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

# tern 0.8.3

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

# tern 0.8.3

### Enhancements
* Added explicit zero counts to `g_km` plot "at risk" annotation tables.
* Added a flag for total level split in `analyze_patients_exposure_in_cols`.
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
47 changes: 45 additions & 2 deletions R/cox_regression_inter.R
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -154,6 +158,45 @@ 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
#'
#' @param verbose (`logical`)\cr Defaults to `FALSE`, which prints out warnings and messages. It is mainly used
#' to print out information about factor casting.
#'
#' @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,
clarkliming marked this conversation as resolved.
Show resolved Hide resolved
effect,
covar,
mod,
label,
control,
data,
verbose = FALSE,
...) {
y <- as_factor_keep_attributes(x, verbose = verbose)
edelarua marked this conversation as resolved.
Show resolved Hide resolved
if (is.character(data[[effect]])) data[[effect]] <- as_factor_keep_attributes(data[[effect]], verbose = FALSE)
if (is.character(data[[covar]])) data[[covar]] <- as_factor_keep_attributes(data[[covar]], verbose = FALSE)

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.

30 changes: 28 additions & 2 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