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 2 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Updated `summarize_coxreg` to print covariates in data rows for univariate Cox regression with no interactions and content rows otherwise.
* Removed "baseline status" text from `d_count_abnormal_by_baseline` labels.
* Improved default sizing of annotation tables in `g_km` and added dynamic scaling of the `surv_med` and `coxph` annotation tables, with customization via the `width_annots` argument.
* Added method for `character` class to `h_coxreg_inter_effect` enabling `character` covariates in `summarize_coxreg`.

### Bug Fixes
* Fixed bug in `split_text_grob` preventing titles and footnotes from being properly formatted and printed by `decorate_grob`.
Expand Down
37 changes: 37 additions & 0 deletions R/cox_regression_inter.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,43 @@ h_coxreg_inter_effect.factor <- function(x,
)
}

#' @describeIn cox_regression_inter Estimate the interaction with a `character` covariate. This makes an automatic
#' conversion to `factor` and then forwards to the method for factors.
#'
#' @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
data[[effect]] <- as_factor_keep_attributes(data[[effect]], verbose = FALSE)
data[[covar]] <- as_factor_keep_attributes(data[[covar]], verbose = FALSE)
edelarua marked this conversation as resolved.
Show resolved Hide resolved

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
26 changes: 26 additions & 0 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