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

Fix tm_t_coxreg errors when applying filters #831

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
* Fixed label indentation in `tm_t_abnormality_by_worst_grade`.
* Added `total_label` argument to enable customization of the "All Patients" column/row label in the following modules: `tm_a_mmrm`, `tm_t_abnormality`, `tm_t_abnormality_by_worst_grade`, `tm_t_binary_outcome`, `tm_t_events`, `tm_t_events_by_grade`, `tm_t_events_patyear`, `tm_t_events_summary`, `tm_t_exposure`, `tm_t_mult_events`, `tm_t_shift_by_arm`, `tm_t_shift_by_arm_worst`, `tm_t_shift_by_grade`, `tm_t_smq`, `tm_t_summary`, `tm_t_summary_by`, and `tm_t_tte`.
* Increased default width of `tm_g_forest_tte` plot to prevent overlapping text.
* Improve default annotation table sizing in `tm_g_km`.
* Improved default annotation table sizing in `tm_g_km`.
* Refactored `tm_t_exposure` to display "total" row as last row in table instead of as a summary row. Added parameters `add_total_row` to set whether the total row should be displayed and `total_row_label` to set the total row label.
* Update `tm_t_events` to maintain indentation after pruning.
* Updated `tm_t_events` to maintain indentation after pruning.
* Updated default reference/comparison arm level selection to work when arm variable levels are filtered out.
* Updated `tm_t_coxreg` to drop factor covariate variable levels that are not present to avoid errors when filtering.


### Miscellaneous
* Updated `control_incidence_rate` parameter names in `tm_t_events_patyear` from `time_unit_input` and `time_unit_output` to `input_time_unit` and `num_pt_year`, respectively, after parameter names were changed in `tern`.
Expand Down
2 changes: 1 addition & 1 deletion R/arm_ref_comp.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ arm_ref_comp_observer <- function(session,
teal::validate_has_elements(arm, "Treatment variable is empty.")

arm_levels <- if (is.factor(arm)) {
levels(arm)
intersect(levels(arm), unique(arm))
} else {
unique(arm)
}
edelarua marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
16 changes: 16 additions & 0 deletions R/tm_t_coxreg.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ template_coxreg_u <- function(dataname,
)
)

data_pipe <- add_expr(
data_pipe,
substitute(
expr = dplyr::mutate(across(where(is.factor) & cov_var, droplevels)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the across is.factor is because droplevels raises error for characters. If we cannot be sure that cov_var is a factor, fair enough, if it is enforced, however, I would just go with mutate(cov_var = droplevels(cov_var)).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's because of the error for characters but also because cov_var can be a single variable name or a vector of variable names and we want to account for all of these variables.

env = list(cov_var = cov_var)
)
)

data_pipe <- add_expr(data_pipe, quote(df_explicit_na(na_level = "")))

data_list <- add_expr(
Expand Down Expand Up @@ -214,6 +222,14 @@ template_coxreg_m <- function(dataname,
)
)

data_pipe <- add_expr(
data_pipe,
substitute(
expr = dplyr::mutate(across(where(is.factor) & cov_var, droplevels)),
env = list(cov_var = cov_var)
)
)

data_pipe <- add_expr(data_pipe, quote(df_explicit_na(na_level = "")))

data_list <- add_expr(
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/_snaps/tm_t_coxreg.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
anl <- adrs %>% dplyr::filter(ARMCD %in% c("ARM A", "ARM B",
"ARM C")) %>% dplyr::mutate(ARMCD = stats::relevel(ARMCD,
ref = "ARM A")) %>% dplyr::mutate(ARMCD = droplevels(ARMCD)) %>%
dplyr::mutate(event = 1 - CNSR) %>% df_explicit_na(na_level = "")
dplyr::mutate(event = 1 - CNSR) %>% dplyr::mutate(across(where(is.factor) &
NULL, droplevels)) %>% df_explicit_na(na_level = "")
control <- list(pval_method = "wald", ties = "efron", conf_level = 0.95,
interaction = FALSE)
}
Expand Down Expand Up @@ -38,7 +39,8 @@
anl <- adrs %>% dplyr::filter(ARMCD %in% c("ARM A", "ARM B",
"ARM C")) %>% dplyr::mutate(ARMCD = stats::relevel(ARMCD,
ref = "ARM A")) %>% dplyr::mutate(ARMCD = droplevels(ARMCD)) %>%
dplyr::mutate(event = 1 - CNSR) %>% df_explicit_na(na_level = "")
dplyr::mutate(event = 1 - CNSR) %>% dplyr::mutate(across(where(is.factor) &
NULL, droplevels)) %>% df_explicit_na(na_level = "")
control <- list(pval_method = "wald", ties = "efron", conf_level = 0.95,
interaction = TRUE)
}
Expand Down Expand Up @@ -70,7 +72,8 @@
ref = "A: Drug X")) %>% dplyr::mutate(ARM = droplevels(ARM)) %>%
dplyr::mutate(ARM = combine_levels(x = ARM, levels = c("B: Placebo",
"C: Combination"))) %>% dplyr::mutate(event = 1 -
CNSR) %>% df_explicit_na(na_level = "")
CNSR) %>% dplyr::mutate(across(where(is.factor) & c("AGE",
"SEX"), droplevels)) %>% df_explicit_na(na_level = "")
}

$layout
Expand Down