From f0aa8110a5deb47be8ae21e35bff08f746ce0d0f Mon Sep 17 00:00:00 2001 From: smritia Date: Wed, 18 Sep 2024 05:26:07 +0000 Subject: [PATCH] Migration from tlfcarver to minimise issues --- R/dataset_merge.R | 134 +++++++++++++ R/graph_utils.R | 8 +- tests/testthat/test-ae_volcano_plot.R | 85 ++++++++ tests/testthat/test-dataset_merge.R | 54 +++++ tests/testthat/test-forest_plot.R | 243 +++++++++++----------- tests/testthat/test-mcatstat.R | 278 +++++++++++++------------- tests/testthat/test-mdisplay.R | 148 -------------- tests/testthat/test-risk_stat.R | 215 +++++++++++--------- tests/testthat/test-volcano_plot.R | 113 ----------- 9 files changed, 662 insertions(+), 616 deletions(-) create mode 100644 R/dataset_merge.R create mode 100644 tests/testthat/test-ae_volcano_plot.R create mode 100644 tests/testthat/test-dataset_merge.R delete mode 100644 tests/testthat/test-mdisplay.R delete mode 100644 tests/testthat/test-volcano_plot.R diff --git a/R/dataset_merge.R b/R/dataset_merge.R new file mode 100644 index 0000000..bdb5f0c --- /dev/null +++ b/R/dataset_merge.R @@ -0,0 +1,134 @@ +# Copyright 2024 Pfizer Inc +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#' Merge Datasets +#' +#' @param ... Datasets to be merged. +#' @param byvars By variables required to perform merge. +#' @param subset Dataset specific subset conditions as `list`, default is `NULL`. +#' Has to be specified in the same order of datasets to be merged +#' +#' @return A `data.frame` +#' @export +#' +#' @examples +#' dataset_merge( +#' lab_data$adsl, +#' lab_data$adlb, +#' byvars = "STUDYID~USUBJID~SUBJID", +#' subset = list("SEX=='F'", "PARAMCD == 'L00021S'") +#' ) +#' +#' dataset_merge( +#' lab_data$adsl, +#' lab_data$adlb, +#' byvars = "STUDYID~USUBJID~SUBJID", +#' subset = list("SEX=='F'", NA_character_) +#' ) +#' +#' dataset_merge( +#' lab_data$adsl, +#' lab_data$adlb, +#' byvars = "STUDYID~USUBJID~SUBJID", +#' subset = list(NA_character_, "PARAMCD == 'L00021S'") +#' ) +#' +#' dataset_merge( +#' lab_data$adsl, +#' lab_data$adlb, +#' byvars = "STUDYID~USUBJID~SUBJID", +#' subset = list("USUBJID == 'XYZ1 1003 10031009'", NA_character_) +#' ) +#' +#' dataset_merge( +#' waterfall_plot_data$adrs, +#' waterfall_plot_data$adtr, +#' byvars = "STUDYID~USUBJID~TRT01P", +#' subset = list("PARAMCD == 'BOR_C'", NA_character_) +#' ) +#' +#' ## more than 2 datasets +#' +#' dataset_merge( +#' dplyr::filter(lab_data$adsl, USUBJID == "XYZ1 1003 10031009"), +#' lab_data$adsl, +#' lab_data$adlb, +#' byvars = "STUDYID~USUBJID~SUBJID" +#' ) +#' +dataset_merge <- function(..., byvars, subset = NULL) { + dfs <- list2(...) + stopifnot("At least two datasets required for merging" = length(dfs) >= 2) + byvars <- str_to_vec(byvars) + if (!every(dfs, \(x) all(byvars %in% names(x)))) stop("`byvars` not present") + + if (length(subset) > 0) { + stopifnot("Length of subsets and datasets should be equal" = length(dfs) == length(subset)) + if (every(subset, is.na)) stop("All subsets cannot be `NA`, use `subset = NULL` instead") + dfs <- map(seq_along(dfs), \(i) { + df_sub <- dfs[[i]] + if (!is.na(subset[[i]])) { + df_sub <- df_sub |> + filter(!!!parse_exprs(subset[[i]])) + } + df_sub + }) + } + + df_list <- map(seq_along(dfs), \(x) { + out <- dfs[[x]] + if (x < length(dfs)) { + out <- out |> + select(all_of(union(byvars, setdiff( + names(dfs[[x]]), names(dfs[[x + 1]]) + )))) + } + out + }) + reduce(df_list, left_join, byvars) +} + +#' Merge adsl dataset with the analysis dataset +#' +#' @param adsl adsl dataset +#' @param adsl_subset population variable subset condition +#' @param dataset_add analysis dataset +#' +#' @return merged dataset +#' @export +#' +#' @examples +#' data(lab_data) +#' +#' adsl_merge( +#' adsl = lab_data$adsl, +#' adsl_subset = "SAFFL=='Y'", +#' dataset_add = lab_data$adlb +#' ) +#' +adsl_merge <- function(adsl = NULL, adsl_subset = "", dataset_add = NULL) { + stopifnot(length(adsl) > 0) + stopifnot(nrow(adsl) > 0) + stopifnot(length(dataset_add) > 0) + + if (adsl_subset != "" && !is.na(adsl_subset)) { + adsl <- adsl |> + filter(!!!parse_exprs(adsl_subset)) + } + + byvars <- grep("STUDYID|USUBJID|SUBJID", names(dataset_add), value = TRUE) + + adsl |> + select(all_of(c(byvars, setdiff(names(adsl), names(dataset_add))))) |> + left_join(dataset_add, by = byvars) +} diff --git a/R/graph_utils.R b/R/graph_utils.R index d9d2b95..42b50d1 100644 --- a/R/graph_utils.R +++ b/R/graph_utils.R @@ -21,7 +21,7 @@ #' @export #' #' @examples -#' library(tlfcarver) +#' library(carver) #' library(ggplot2) #' ggplot(data = mtcars, mapping = aes(x = mpg, y = hp)) + #' geom_point() + @@ -160,7 +160,7 @@ g_seriessym <- function(gdata, #' @export #' #' @examples -#' library(tlfcarver) +#' library(carver) #' empty_plot() empty_plot <- function(message = "No data available for these values", fontsize = 8) { @@ -205,7 +205,7 @@ def_axis_spec <- function(arg, vec, val) { #' @export #' #' @examples -#' library(tlfcarver) +#' library(carver) #' #' plot_axis_opts( #' xlinearopts = list( @@ -600,7 +600,7 @@ plot_title_nsubj <- function(datain, plot_data, by) { #' @export #' #' @examples -#' library(tlfcarver) +#' library(carver) #' MPG <- ggplot2::mpg #' MPG[["cyl"]] <- as.character(MPG[["cyl"]]) #' tbl_to_plot( diff --git a/tests/testthat/test-ae_volcano_plot.R b/tests/testthat/test-ae_volcano_plot.R new file mode 100644 index 0000000..45b389b --- /dev/null +++ b/tests/testthat/test-ae_volcano_plot.R @@ -0,0 +1,85 @@ +data("ae_risk") +vaxis_opts <- ae_volcano_opts( + datain = ae_risk, + statistic = "Risk Ratio", + trt1_label = "Control", + trt2_label = "Exposure", + pvalue_trans = "-log10", + xref_offset = 1 +) +axis_opts <- plot_axis_opts( + ylinearopts = vaxis_opts$ylinearopts, + yaxis_scale = vaxis_opts$yaxis_scale, + xaxis_label = vaxis_opts$xaxis_label, + yaxis_label = vaxis_opts$yaxis_label +) + +test_that("Test 1: Volcano plot Options works", { + expect_type(vaxis_opts, "list") + expect_named( + vaxis_opts, + c("xaxis_label", "yaxis_label", "ylinearopts", "yaxis_scale", "xref") + ) + expected <- list( + xaxis_label = "<--- Favors Control (N=69) ---- Favors Exposure (N=79) --->\nRisk Ratio", + yaxis_label = "-log10 p-value", + ylinearopts = list(breaks = as.numeric(paste0("1e-", 0:20)), labels = as.character(0:20)), + yaxis_scale = reverselog_trans(10), + xref = c(1, 0, 2) + ) + expect_equal(vaxis_opts, expected) + vaxis_opts2 <- ae_volcano_opts( + datain = ae_risk, + pvalue_trans = "none" + ) + expected2 <- list( + xaxis_label = "<--- Favors Control (N=69) ---- Favors Exposure (N=79) --->\nRisk Ratio", + yaxis_label = "p-value", + ylinearopts = list( + breaks = c(0.05, 0, rep(1, 10) / 10^(9:0)), + labels = as.character(c(0.05, 0, rep(1, 10) / 10^(9:0))) + ), + yaxis_scale = "identity", + xref = c(1, 0, 2) + ) + expect_equal(vaxis_opts2, expected2) +}) + +test_that("Test 1: Volcano plot with standard inputs", { + volcano_test <- ae_volcano_plot( + datain = ae_risk, + axis_opts = axis_opts, + legend_opts = list(label = "", pos = "bottom", dir = "horizontal"), + xref = vaxis_opts$xref, + pvalue_sig = 0.05 + ) + expect_type(volcano_test, "list") + expect_true("ggplot" %in% class(volcano_test)) + expect_snapshot(volcano_test[["mapping"]]) + expect_snapshot(volcano_test[["labels"]]) + volcano_test2 <- ae_volcano_plot( + datain = ae_risk, + axis_opts = axis_opts, + xref = vaxis_opts$xref, + pvalue_sig = 0.004 + ) + purrr::walk( + volcano_test2$layers, + \(x) expect_snapshot(x$aes_params) + ) +}) + +test_that("Test 2: Volcano plot with interactive output", { + volcano_test <- ae_volcano_plot( + datain = ae_risk, + axis_opts = axis_opts, + legend_opts = list(label = "", pos = "bottom", dir = "horizontal"), + xref = vaxis_opts$xref, + pvalue_sig = 0.05, + interactive = "Y" + ) + expect_type(volcano_test, "list") + expect_true("plotly" %in% class(volcano_test)) + expect_equal(volcano_test$height, 700) + expect_equal(volcano_test$width, 800) +}) diff --git a/tests/testthat/test-dataset_merge.R b/tests/testthat/test-dataset_merge.R new file mode 100644 index 0000000..020a258 --- /dev/null +++ b/tests/testthat/test-dataset_merge.R @@ -0,0 +1,54 @@ +df1 <- iris |> + dplyr::select(Species) |> + dplyr::distinct() + +df2 <- iris |> + dplyr::select(Species, dplyr::ends_with("Width")) + +test_that("dataset_merge works", { + expected <- dplyr::left_join(df1, df2, by = "Species") + actual <- dataset_merge(df1, df2, byvars = "Species") + expect_identical(expected, actual) +}) + +test_that("dataset_merge works with subsets and for > 2 datasets to merge", { + df12 <- dplyr::filter(df1, Species == "setosa") + df21 <- dplyr::filter(df2, Sepal.Width > 3) + df13 <- dplyr::filter(df1, Species != "versicolor") + expected1 <- dplyr::left_join(df1, df21, by = "Species") + actual1 <- + dataset_merge(df1, df2, byvars = "Species", subset = list(NA_character_, "Sepal.Width > 3")) + expected2 <- dplyr::left_join(df12, df21, by = "Species") + actual2 <- + dataset_merge(df1, df2, byvars = "Species", subset = list("Species == 'setosa'", "Sepal.Width > 3")) # nolint + expected3 <- purrr::reduce(list(df13, df12, df2), dplyr::left_join, "Species") + actual3 <- + dataset_merge( + df1, df1, df2, + byvars = "Species", + subset = list("Species != 'versicolor'", "Species == 'setosa'", NA_character_) + ) + expect_identical(expected1, actual1) + expect_identical(expected2, actual2) + expect_identical(expected3, actual3) +}) + +test_that("dataset_merge returns expected errors", { + expect_error( + dataset_merge( + df1, + df2, + byvars = "xxx" + ), + "`byvars` not present" + ) + expect_error( + dataset_merge( + df1, + df2, + byvars = "Species", + subset = list(NA_character_, NA_character_) + ), + "All subsets cannot be `NA`, use `subset = NULL` instead" + ) +}) diff --git a/tests/testthat/test-forest_plot.R b/tests/testthat/test-forest_plot.R index 71dbbb3..6ded5ca 100644 --- a/tests/testthat/test-forest_plot.R +++ b/tests/testthat/test-forest_plot.R @@ -1,138 +1,133 @@ -data(ae_pre) - -risk_dat <- risk_stat( - datain = ae_pre$dsin, - d_datain = ae_pre$dout, - summary_by = "Patients", - eventVar = "AEDECOD", - ctrlgrp = "Placebo", - trtgrp = "Xanomeline High Dose~~Xanomeline Low Dose", - statistics = "Risk Ratio", - alpha = 0.05, - cutoff = 5, - sort_opt = "Ascending", - sort_var = "Count" -) -risk_dat1 <- risk_stat( - datain = ae_pre$dsin, - d_datain = ae_pre$dout, - summary_by = "Events", - eventVar = "AEBODSYS", - ctrlgrp = "Placebo", - trtgrp = "Xanomeline High Dose~~Xanomeline Low Dose", - statistics = "Risk Difference", - alpha = 0.05, - cutoff = 5, - sort_opt = "Ascending", - sort_var = "Count" +data("ae_risk") +options(warn = -1) +fp <- forest_plot_base( + ae_risk, + xvar = "RISK", + yvar = "DPTVAL", + xminvar = "RISKCIL", + xmaxvar = "RISKCIU", + hovervar = "HOVER_RISK", + series_var = "TRTPAIR", + xrefline = 1, + hline_y = "Y", + axis_opts = plot_axis_opts( + xaxis_label = "Risk Ratio", + xopts = list(labelsize = 8) + ), + legend_opts = list(pos = "bottom", dir = "horizontal") ) - -test_that("Forest Plot Works with standard inputs", { - fp_out <- forest_plot( - datain = risk_dat, - AE_Filter = "Any", - review_by = c("AEBODSYS", "AEDECOD"), - summary_by = "Patients", - statistics = "Risk Ratio", - xref = 1, - pvalcut = 0.05, - trtbign = "Y", - scale_trans = "identity" - ) - # Check that expected 6 outputs are returned: - expect_equal(names(fp_out), c("ptly", "plot", "drill_plt", "rpt_data", "n", "title", "footnote")) - - # Check type of plotly object returned - expect_true(fp_out$ptly$x$subplot) - # Check that the event variable selected (label) is correct: - expect_equal( - fp_out$ptly$x$layout$xaxis$title$text, - "AE Dictionary-Derived Term" - ) - # Check the chosen statistic as label - expect_equal( - fp_out$ptly$x$layout$xaxis3$title$text, - "Risk Ratio" - ) - # Check title and footnote - expect_equal(fp_out$title, "Forest plot for Risk Ratio of Any Adverse Events") - - expect_equal( - fp_out$footnote, - paste0( - "* N is the total number of participants. \nClassifications of adverse ", - "events are based on the Medical Dictionary for Regulatory Activities (MedDRA v21.1", - "). \nDashed Vertical line represents risk value reference line. \nTotals for the ", - "No. of Participants/Events at a higher level are not necessarily the sum of those ", - "at the lower levels since a participant may report two or more. \nThe number of ", - "participants reporting at least 1 occurrence of the event specified." +test_that("Test case 1: Forest Plot Base Works with standard inputs", { + expect_true(is.ggplot(fp)) + expect_equal(fp[["data"]], ae_risk) + purrr::walk(c("mapping", "layers", "theme"), \(x) expect_snapshot(fp[[x]])) + expect_error(forest_plot_base( + datain = data.frame(), + xvar = "RISK", + yvar = "DPTVAL", + xminvar = "RISKCIL", + xmaxvar = "RISKCIU", + hovervar = "HOVER_RISK", + series_var = "TRTPAIR", + xrefline = 1, + hline_y = "N", + axis_opts = plot_axis_opts( + xaxis_label = "Risk Ratio", + xopts = list(labelsize = 8) ) - ) - # Graph data is from input data: - expect_type(fp_out$rpt_data, "list") - expect_true(nrow(fp_out$rpt_data) <= nrow(risk_dat)) + )) }) -test_that("Forest plot arguments reflect correctly", { - fp_out1 <- forest_plot( - datain = risk_dat1, - AE_Filter = NULL, - review_by = "AEBODSYS", - summary_by = "Events", - statistics = "Risk Difference", - trtbign = "N", - scale_trans = "log10" +# Forest Scatter plot +sp <- + forest_plot_scatter( + datain = ae_risk, + xvar = "PCT", + yvar = "DPTVAL", + series_var = "TRTVAR", + series_opts = list( + color = g_seriescol(ae_risk, c("black", "goldenrod"), "TRTVAR"), + shape = g_seriessym(ae_risk, NA, "TRTVAR"), + size = rep(1, 2) + ), + hovervar = "HOVER_PCT", + xaxis_pos = "top", + legend_opts = list(pos = "bottom", dir = "horizontal"), + hline_y = "Y", + axis_opts = list(xsize = 8, xtsize = 4, xaxis_label = "Percentage") ) +test_that("Test case 1: Forest Plot Scatter Works with standard inputs", { + expect_true(is.ggplot(sp)) + expect_equal(sp[["data"]], ae_risk) + purrr::walk(c("mapping", "layers", "theme"), \(x) expect_snapshot(sp[[x]])) +}) - # Review term reflects in legend - expect_equal( - fp_out1$ptly$x$layout$xaxis$title$text, - "Body System or Organ Class" +test_that("Test Case 2: Forest plot scatter errors resolve correctly", { + expect_error( + forest_plot_scatter( + datain = data.frame(), + xvar = "PCT", + yvar = "DPTVAL", + series_var = "TRTVAR", + series_opts = list( + color = g_seriescol(ae_risk, c("black", "goldenrod"), "TRTVAR"), + shape = g_seriessym(ae_risk, NA, "TRTVAR"), + size = rep(1, 2) + ), + hovervar = "HOVER_PCT", + xaxis_pos = "top", + hline_y = "N", + axis_opts = list(xsize = 8, xtsize = 6, xaxis_label = "Percentage") + ) ) +}) - # Treatment legend does not contain (N = count) - legendtext <- unlist(purrr::map( - seq_along(fp_out1$ptly$x$data), - function(i) fp_out1$ptly$x$data[[i]]$name - )) - expect_false(any(str_detect(legendtext[1:3], "\\(N="))) - expect_equal(legendtext[1:3], unique(risk_dat1$TRTVAR)) - - # Check the chosen statistic as label - expect_equal( - fp_out1$ptly$x$layout$xaxis3$title$text, - "Risk Difference" +tt <- ae_risk |> + mutate(XVAR = "HT") |> + tbl_to_plot( + yvar = "DPTVAL", + labelvar = "DPTVAL", + text_size = 2, + axis_opts = list(xaxis_label = "", xsize = 8, xtsize = 0) + ) + + theme(axis.text.x = element_blank()) +test_that("Test Case 1: forest_display static works correctly", { + actual <- forest_display( + plot_list = list(splot = sp, fplot = fp), + rel_widths = c(0.6, 0.4), + interactive = "N" ) - # Check title and footnote - expect_equal(fp_out1$title, "Forest plot for Risk Difference of Adverse Events") - - expect_equal( - fp_out1$footnote, - paste0( - "* N is the total number of events. \nClassifications of adverse events are ", - "based on the Medical Dictionary for Regulatory Activities (MedDRA v21.1). ", - "\nDashed Vertical line represents risk value reference line. \nTotals for ", - "the No. of Participants/Events at a higher level are not necessarily the ", - "sum of those at the lower levels since a participant may report two or more.", - " \nEvent counts are the sum of individual occurrences within that category." + expect_type(actual, "list") + expect_true("ggplot" %in% class(actual)) + expect_true("waiver" %in% class(actual$data)) + expect_length(actual$layers, 3) + expect_error( + forest_display( + plot_list = list(sp = sp, fp = fp), + rel_widths = c(0.6, 0.4), + interactive = "N" ) ) + expect_error( + forest_display( + plot_list = list(splot = sp, fplot = fp), + rel_widths = c(0.6), + interactive = "N" + ), + "rel_widths should be equal to the number of plot columns" + ) }) -## Test with empty data -test_that("Forest Plot works as expected with empty `risk_stat()` output", { - risk_stat_null <- tibble() - fp_null <- forest_plot( - datain = risk_stat_null, - AE_Filter = "Any", - review_by = c("AEBODSYS", "AEDECOD"), - summary_by = "Patients", - statistics = "Risk Ratio", - xref = 1, - pvalcut = 0.05, - trtbign = "Y", - scale_trans = "identity" + +test_that("Test Case 1: forest_display interactive works correctly", { + actual <- forest_display( + plot_list = list(termtable = tt, splot = sp, fplot = fp), + rel_widths = c(0.25, 0.38, 0.27), + interactive = "Y", + plot_height = 800, + xpos = "top" ) - expect_equal(length(fp_null), 3) - expect_equal(names(fp_null), c("ptly", "plot", "rpt_data")) - expect_identical(risk_stat_null, fp_null$rpt_data) + expect_type(actual, "list") + expect_true("plotly" %in% class(actual)) + expect_true(actual$x$subplot) + expect_equal(actual$height, 800) + expect_snapshot(actual$x$layout) }) diff --git a/tests/testthat/test-mcatstat.R b/tests/testthat/test-mcatstat.R index b3826e4..21f58ed 100644 --- a/tests/testthat/test-mcatstat.R +++ b/tests/testthat/test-mcatstat.R @@ -1,194 +1,204 @@ -data(adsl) -data(ae_pre) - -# Sample Input 1 -ad_entry <- mentry( - datain = adsl, - ui_aSubset = NA, - ui_dSubset = "USUBJID!=''", - ui_byvar = NA, - ui_subgrpvar = NA, - ui_trtvar = "TRT01A", - ui_trtsort = "TRT01AN", - ui_addGrpMiss = "N", - ui_pop_fil = NA -) +data("adsl") +data("ae_pre_process") +ae_entry <- + ae_pre_process$data |> mentry( + subset = NA, + byvar = "AEBODSYS", + trtvar = "TRTA", + trtsort = "TRTAN", + subgrpvar = NA, + trttotalyn = "N", + add_grpmiss = "N", + sgtotalyn = "N", + pop_fil = "SAFFL" + ) +ad_entry <- + adsl |> mentry( + subset = "EFFFL=='Y'", + trtvar = "TRT01A", + trtsort = "TRT01AN", + trttotalyn = "N", + add_grpmiss = "N", + sgtotalyn = "N", + pop_fil = NA + ) # Sample Output created from defaults -ad_sum <- ad_entry$dsin %>% - group_by(TRTVAR, SEX) %>% - summarise(FREQ = n_distinct(USUBJID)) %>% - ungroup() %>% - group_by(TRTVAR) %>% +ad_sum <- ad_entry |> + group_by(across(all_of(c("TRTVAR", "SEX")))) |> + summarise(FREQ = length(unique(.data[["USUBJID"]]))) |> + ungroup() |> + group_by(across(all_of("TRTVAR"))) |> mutate( - DPTVAL = as.character(SEX), DENOMN = sum(FREQ), - PCT = format(round(100 * FREQ / DENOMN, 2), nsmall = 2), - CVALUE = paste0(FREQ, " (", PCT, "%)"), CN = "C", - DPTVARN = 1, XVAR = DPTVAL, - DPTVAR = "SEX" - ) %>% - ungroup() %>% - select(-SEX) %>% - arrange(TRTVAR, DPTVAL) - -# sample input 2 -ad_entry1 <- mentry( - datain = adsl, - ui_aSubset = NA, - ui_dSubset = "USUBJID!=''", - ui_byvar = "RACE", - ui_subgrpvar = "ETHNIC", - ui_trtvar = "TRT01A", - ui_trtsort = "TRT01AN", - ui_addGrpMiss = "N", - ui_pop_fil = NA -) - - -test_that("Case 1:mcatstat output with standard inputs", { + DPTVAL = as.character(.data[["SEX"]]), DENOMN = sum(.data[["FREQ"]]), + PCT = round_f(100 * .data[["FREQ"]] / .data[["DENOMN"]], 2), + CVALUE = paste0(.data[["FREQ"]], " (", .data[["PCT"]], "%)"), CN = "C", + DPTVARN = 1, XVAR = .data[["DPTVAL"]], + DPTVAR = "SEX", DPTVALN = as.numeric(factor(.data[["SEX"]])) + ) |> + ungroup() |> + select(-all_of("SEX")) +# Sample Cumulative output +ad_cum <- ad_entry |> + group_by(across(all_of(c("TRTVAR", "SEX")))) |> + summarise(FREQ = length(unique(.data[["USUBJID"]]))) |> + mutate(DPTVAL = as.character(.data[["SEX"]]), FREQ = cumsum(.data[["FREQ"]])) |> + select(-all_of("SEX")) |> + ungroup() + + +test_that("Case 1:mcatstat output with standard inputs and filters", { ad_mcat <- mcatstat( - datain = ad_entry$dsin, - d_datain = ad_entry$dout, - ui_dptvar = "SEX", - ui_pctdisp = "TRT" + datain = ad_entry, + uniqid = "USUBJID", + dptvar = "SEX", + pctdisp = "TRT" ) - # Check output returned is in expected format expect_type(ad_mcat, "list") - expect_identical(names(ad_mcat), c( - "DPTVAR", "DPTVAL", "XVAR", "TRTVAR", - "DENOMN", "DPTVALN", "FREQ", "PCT", - "CVALUE", "DPTVARN", "CN" - )) - + expect_true(all(names(ad_mcat) %in% names(ad_sum))) # Check values are matched with expected: - expect_true(all_equal(ad_mcat %>% select(-DPTVALN), ad_sum)) + expect_equal(ad_mcat, ad_sum |> select(all_of(names(ad_mcat))), ignore_attr = TRUE) }) test_that("Case 2: Empty input", { - m_zero <- mcatstat( - datain = ad_entry$dsin %>% filter(USUBJID == "A"), - d_datain = ad_entry$dout, - ui_dptvar = "SEX", - ui_pctdisp = "TRT" + expect_error( + mcatstat( + datain = ad_entry |> filter(USUBJID == "A"), + dptvar = "SEX", + pctdisp = "TRT" + ), + "No data for mcatstat" + ) + expect_error( + mcatstat( + datain = ad_entry, + uniqid = "XYZVAR", + dptvar = "SEX", + pctdisp = "TRT" + ), + "uniqid should exist in data or be ALLCT" ) - - expect_identical(m_zero, NULL) }) -test_that("Case 3: Unique ID variation", { +test_that("Case 3: Unique ID and sign variation", { m_subj <- mcatstat( - datain = ae_pre$dsin, - d_datain = ae_pre$dout, - ui_uniqid = "USUBJID", - ui_dptvar = "AEDECOD", - ui_pctdisp = "TRT" + datain = ae_entry, + uniqid = "USUBJID", + dptvar = "AEDECOD", + pctdisp = "TRT", + pctsyn = "N" ) m_na <- mcatstat( - datain = ae_pre$dsin, - d_datain = ae_pre$dout, - ui_uniqid = NA, - ui_dptvar = "AEDECOD", - ui_pctdisp = "TRT" + datain = ae_entry, + uniqid = "ALLCT", + dptvar = "AEDECOD", + pctdisp = "TRT", + pctsyn = "N" ) # All groups and order except actual count values should be equal expect_equal( - m_subj %>% select(-c(DENOMN, FREQ, PCT, CVALUE)), - m_na %>% select(-c(DENOMN, FREQ, PCT, CVALUE)) + m_subj |> select(-all_of(c("DENOMN", "FREQ", "PCT", "CVALUE"))), + m_na |> select(-all_of(c("DENOMN", "FREQ", "PCT", "CVALUE"))) ) # Counts are different expect_false(setequal(unique(m_subj$DENOMN), unique(m_na$DENOMN))) # Check values (for denominator, should also apply to numerator) - check_na <- ae_pre$dout %>% - ungroup() %>% - group_by(TRTVAR) %>% - summarise(DENOMN = n()) %>% + check_na <- ae_entry |> + ungroup() |> + group_by(TRTVAR) |> + summarise(DENOMN = n()) |> ungroup() - expect_equal(m_na %>% distinct(TRTVAR, DENOMN), check_na) + expect_equal(m_na |> distinct(across(all_of(c("TRTVAR", "DENOMN")))), + check_na, + ignore_attr = TRUE + ) + expect_false(any(grep("%", m_subj$CVALUE))) }) -test_that("Case 4: Percentage denominator variation", { - # Invalid value for ui_pctdisp: - expect_null(mcatstat( - datain = ad_entry1$dsin, - d_datain = ad_entry1$dout, - ui_dptvar = "SEX", - ui_pctdisp = "ABC" - )) +test_that("Case 4: Percentage denominator variation and denomyn", { + # Invalid value for pctdisp: + expect_error(mcatstat( + datain = ad_entry, + dptvar = "SEX", + pctdisp = "ABC" + ), "Invalid pctdisp") m_none <- mcatstat( - datain = ad_entry1$dsin, - d_datain = ad_entry1$dout, - ui_dptvar = "SEX", - ui_pctdisp = "NONE" + datain = ad_entry, + dptvar = "SEX", + pctdisp = "NONE" ) - # No percentage columns: - expect_identical(names(m_none), c( - "DPTVAR", "DPTVAL", "XVAR", "TRTVAR", - "SUBGRPVAR1", "SUBGRPVAR1N", "BYVAR1", - "BYVAR1N", "DPTVALN", "FREQ", - "CVALUE", "DPTVARN", "CN" - )) + expect_false(any(c("PCT", "DENOMN") %in% names(m_none))) expect_equal(m_none$FREQ, m_none$CVALUE) # Variable total as denominator m_var <- mcatstat( - datain = ad_entry1$dsin, - d_datain = ad_entry1$dout, - ui_dptvar = "SEX", - ui_pctdisp = "VAR" + datain = ad_entry, + dptvar = "SEX", + pctdisp = "VAR", + denomyn = "Y" ) - # Entire matrix has same denominator - check value expect_length(unique(m_var$DENOMN), 1) - expect_equal(unique(m_var$DENOMN), length(unique(ad_entry1$dout$USUBJID))) + expect_equal(unique(m_var$DENOMN), length(unique(ad_entry$USUBJID))) + expect_true(all(str_detect(m_var$CVALUE, paste0("/", unique(m_var$DENOMN))))) }) -test_that("Case 5: Cumulative Frequency", { +test_that("Case 5: Cumulative Frequency and Total", { m_cum <- mcatstat( - datain = ad_entry$dsin, - d_datain = ad_entry$dout, - ui_dptvar = "SEX", - ui_pctdisp = "NO", + datain = ad_entry, + dptvar = "SEX", + pctdisp = "NO", cum_ctyn = "Y" ) - ad_cum <- ad_entry$dsin %>% - group_by(TRTVAR, SEX) %>% - summarise(FREQ = n_distinct(USUBJID)) %>% - mutate(DPTVAL = as.character(SEX), FREQ = cumsum(FREQ)) %>% - select(-SEX) %>% - ungroup() - - expect_equal(m_cum %>% select(TRTVAR, FREQ, DPTVAL), ad_cum) -}) - + expect_equal(m_cum[, c("TRTVAR", "FREQ", "DPTVAL")], ad_cum) -test_that("Case 6: Total category count", { m_total <- mcatstat( - datain = ad_entry$dsin, - d_datain = ad_entry$dout, - ui_dptvar = "SEX", - ui_pctdisp = "TRT", + datain = ad_entry, + dptvar = "SEX", + pctdisp = "TRT", total_catyn = "Y", cum_ctyn = "N" ) - expect_true("Total" %in% unique(m_total$DPTVAL)) # Test single group - m_pl1 <- ad_sum %>% - filter(TRTVAR == "Placebo") %>% - select(FREQ) %>% + m_pl1 <- ad_sum |> + filter(.data[["TRTVAR"]] == "Placebo") |> + select(FREQ) |> sum() - m_pl2 <- m_total %>% - filter(TRTVAR == "Placebo", DPTVAL == "Total") %>% + m_pl2 <- m_total |> + filter(.data[["TRTVAR"]] == "Placebo", .data[["DPTVAL"]] == "Total") |> pull(FREQ) + expect_equal(m_pl2, m_pl1) +}) - expect_identical(m_pl2, m_pl1) +test_that("Case 6: Filters check", { + m_num <- mcatstat( + datain = ad_entry, + a_subset = "SEX == 'F'", + uniqid = "USUBJID", + dptvar = "SEX", + pctdisp = "TRT" + ) + m_denom <- mcatstat( + datain = ad_entry, + denom_subset = "SEX == 'F'", + uniqid = "USUBJID", + dptvar = "SEX", + pctdisp = "VAR" + ) + expect_equal(unique(m_num$DPTVAL), "F") + denomvalue <- ad_entry |> + filter(.data[["SEX"]] == "F") |> + distinct(USUBJID) |> + nrow() + expect_equal(unique(m_denom$DENOMN), denomvalue) }) diff --git a/tests/testthat/test-mdisplay.R b/tests/testthat/test-mdisplay.R deleted file mode 100644 index 55a59d8..0000000 --- a/tests/testthat/test-mdisplay.R +++ /dev/null @@ -1,148 +0,0 @@ -# test case-1 -data("ae_pre") -# mcatstat output will the input for mdisplay -mout <- mcatstat( - datain = ae_pre$dsin, - d_datain = ae_pre$dout, - ui_uniqid = "USUBJID", - ui_dptvar = "AEDECOD", - ui_pctdisp = "TRT", - miss_catyn = "N", - cum_ctyn = "N", - total_catyn = "N", - dptvarn = 1 -) -# test case 1 -test_that("Test Case 1", { - actual <- mdisplay( - datain = mout, - ui_bylabel = NA, - ui_catlabel = NA, - ui_statlabel = NA, - trtbign = NA, - extra_df = NULL, - extra_mergeby = "DPTVAL", - extra_vars = "", - colformat = "", - indent = NULL, - dptlabel = " " - ) - - BYVAR <- var_start(mout, "BYVAR") - BYVARN <- var_start(mout, "BYVARN") - report <- mout %>% - select(DPTVAR, TRTVAR, BYVAR, BYVARN, DPTVARN, DPTVALN, DPTVAL, CVALUE, CN) %>% - mutate(CVALUE = as.character(CVALUE)) - rep <- report %>% - arrange(across(c("TRTVAR"))) %>% - tidyr::pivot_wider( - names_from = TRTVAR, - values_from = CVALUE, - values_fill = "-", - names_sort = FALSE - ) %>% - ungroup() %>% - mutate(across(!BYVAR & !DPTVAR & !DPTVAL & where(is.character), function(x) { - ifelse(CN == "C", gsub("-", "0", x), x) - })) - rep <- rep %>% select(BYVAR, DPTVAR, DPTVAL, everything()) - repdat <- rep %>% - arrange(across(any_of(c(BYVARN, "DPTVARN", "DPTVALN")))) %>% - select(-any_of(BYVARN)) - - expect_equal(actual$dataf, repdat) -}) - -# test case 2 -test_that("Test Case 2", { - actual <- mdisplay( - datain = mout, - ui_bylabel = "SOC", - ui_catlabel = "VALUE", - ui_statlabel = NA, - trtbign = NA, - extra_df = NULL, - extra_mergeby = "DPTVAL", - extra_vars = "", - colformat = "", - indent = NULL, - dptlabel = " " - ) - - BYVAR <- var_start(mout, "BYVAR") - BYVARN <- var_start(mout, "BYVARN") - report <- mout %>% - select(DPTVAR, TRTVAR, BYVAR, BYVARN, DPTVARN, DPTVALN, DPTVAL, CVALUE, CN) %>% - mutate(CVALUE = as.character(CVALUE)) - rep <- report %>% - arrange(across(c("TRTVAR"))) %>% - tidyr::pivot_wider( - names_from = TRTVAR, - values_from = CVALUE, - values_fill = "-", - names_sort = FALSE - ) %>% - ungroup() %>% - mutate(across(!BYVAR & !DPTVAR & !DPTVAL & where(is.character), function(x) { - ifelse(CN == "C", gsub("-", "0", x), x) - })) - rep <- rep %>% select(BYVAR, DPTVAR, DPTVAL, everything()) - repdat <- rep %>% - arrange(across(any_of(c(BYVARN, "DPTVARN", "DPTVALN")))) %>% - select(-any_of(BYVARN)) %>% - rename("SOC" = BYVAR) %>% - mutate(DPTVAR = "VALUE") - - expect_equal(actual$dataf, repdat) -}) - -# test case 3 -test_that("Test Case 3", { - actual <- mdisplay( - datain = mout, - ui_bylabel = "SOC", - ui_catlabel = "VALUE", - ui_statlabel = NA, - trtbign = ae_pre$bigN, - extra_df = NULL, - extra_mergeby = "DPTVAL", - extra_vars = "", - colformat = " ", - indent = "DPTVALN == 1", - dptlabel = " " - ) - - BYVAR <- var_start(mout, "BYVAR") - BYVARN <- var_start(mout, "BYVARN") - report <- mout %>% - select(DPTVAR, TRTVAR, BYVAR, BYVARN, DPTVARN, DPTVALN, DPTVAL, CVALUE, CN) %>% - mutate(CVALUE = as.character(CVALUE)) - rep <- report %>% - arrange(across(c("TRTVAR"))) %>% - tidyr::pivot_wider( - names_from = TRTVAR, - values_from = CVALUE, - values_fill = "-", - names_sort = FALSE - ) %>% - ungroup() %>% - mutate(across(!BYVAR & !DPTVAR & !DPTVAL & where(is.character), function(x) { - ifelse(CN == "C", gsub("-", "0", x), x) - })) - rep <- rep %>% select(BYVAR, DPTVAR, DPTVAL, everything()) - report <- rep %>% - arrange(across(any_of(c(BYVARN, "DPTVARN", "DPTVALN")))) %>% - select(-any_of(BYVARN)) %>% - rename("SOC" = BYVAR) %>% - mutate(DPTVAR = "VALUE") - trtbign <- ae_pre$bigN - n <- paste0("Placebo (N = ", trtbign$BIGN[1], ")") - repdat <- report %>% rename( - !!paste0("Placebo (N=", trtbign$BIGN[1], ")", " ") := "Placebo", - !!paste0("Xanomeline Low Dose (N=", trtbign$BIGN[2], ")", " ") := "Xanomeline Low Dose", - !!paste0("Xanomeline High Dose (N=", trtbign$BIGN[3], ")", " ") := "Xanomeline High Dose", - !!paste0("Total (N=", trtbign$BIGN[4], ")", " ") := "Total" - ) - - expect_equal(actual$dataf, repdat) -}) diff --git a/tests/testthat/test-risk_stat.R b/tests/testthat/test-risk_stat.R index 2608a8e..723380b 100644 --- a/tests/testthat/test-risk_stat.R +++ b/tests/testthat/test-risk_stat.R @@ -1,53 +1,58 @@ -data(ae_pre) -dsin <- ae_pre$dsin -dout <- ae_pre$dout - +data("ae_pre_process") +ae_entry <- mentry( + datain = ae_pre_process$data, + subset = NA, + byvar = "AEBODSYS", + trtvar = "TRTA", + trtsort = "TRTAN", + subgrpvar = NA, + trttotalyn = "N", + add_grpmiss = "N", + sgtotalyn = "N", + pop_fil = "SAFFL" +) +dsin1 <- ae_entry |> + filter(AEDECOD %in% c("NAUSEA", "SINUS BRADYCARDIA")) + +denom <- dsin1 |> + filter(TRTVAR %in% c("Placebo", "Xanomeline High Dose")) |> + group_by(TRTVAR) |> + summarise(N = length(unique(USUBJID))) |> + ungroup() + +freq <- dsin1 |> + filter(TRTVAR %in% c("Placebo", "Xanomeline High Dose") & + eval(parse(text = ae_pre_process$a_subset))) |> + group_by(TRTVAR, AEBODSYS, AEDECOD) |> + summarise(n = length(unique(USUBJID))) |> + ungroup() + +exp <- left_join(denom, freq, by = "TRTVAR") + +idvar <- c("AEBODSYS", "AEDECOD") +exp1 <- exp |> + mutate(TRTVAR = case_when( + TRTVAR == "Placebo" ~ "ctrlgrp", + TRTVAR == "Xanomeline High Dose" ~ "trtgrp" + )) |> + tidyr::pivot_wider(id_cols = any_of(c(idvar)), names_from = TRTVAR, values_from = c(N, n)) |> + mutate( + temp1 = N_ctrlgrp - n_ctrlgrp, + temp2 = N_trtgrp - n_trtgrp + ) +mat <- matrix(c(2, 7, 3, 6), nrow = 2) # testcase 1 test_that("Test Case 1: Check if the function gives expected statistic values", { - dsin1 <- dsin %>% - filter(AEDECOD %in% c("NAUSEA", "SINUS BRADYCARDIA")) %>% - select(USUBJID, TRTA, AEDECOD, AEBODSYS, TRTVAR, BYVAR1) - dout1 <- dout %>% - filter(AEDECOD %in% c("NAUSEA", "SINUS BRADYCARDIA")) %>% - select(USUBJID, TRTA, AEDECOD, AEBODSYS, TRTVAR, BYVAR1) - - denom <- dsin1 %>% - filter(TRTVAR %in% c("Placebo", "Xanomeline High Dose")) %>% - group_by(TRTVAR) %>% - summarise(N = length(unique(USUBJID))) %>% - ungroup() - - freq <- dsin1 %>% - filter(TRTVAR %in% c("Placebo", "Xanomeline High Dose")) %>% - group_by(TRTVAR, AEBODSYS, AEDECOD) %>% - summarise(n = length(unique(USUBJID))) %>% - ungroup() - - exp <- left_join(denom, freq, by = "TRTVAR") - - idvar <- c("AEBODSYS", "AEDECOD") - exp1 <- exp %>% - mutate(TRTVAR = case_when( - TRTVAR == "Placebo" ~ "ctrlgrp", - TRTVAR == "Xanomeline High Dose" ~ "trtgrp" - )) %>% - tidyr::pivot_wider(id_cols = any_of(c(idvar)), names_from = TRTVAR, values_from = c(N, n)) %>% - mutate( - temp1 = N_ctrlgrp - n_ctrlgrp, - temp2 = N_trtgrp - n_trtgrp - ) - - mat <- matrix(c(2, 7, 3, 6), nrow = 2) - risk <- riskratio.wald(mat, conf.level = 1 - 0.05) + risk <- suppressWarnings(epitools::riskratio.wald(mat, conf.level = 1 - 0.05)) risk_val <- round(risk$measure[2, 1], 3) pval <- round(risk$p.value[2, 3], 4) cil <- round(risk$measure[2, 2], 2) ciu <- round(risk$measure[2, 3], 2) - expected <- exp %>% - filter(AEDECOD == "NAUSEA") %>% + expected <- exp |> + filter(AEDECOD == "NAUSEA") |> mutate( RISK = risk_val, PVALUE = pval, @@ -59,9 +64,9 @@ test_that("Test Case 1: Check if the function gives expected statistic values", risk_s <- risk_stat( datain = dsin1, - d_datain = dout1, + a_subset = ae_pre_process$a_subset, summary_by = "Patients", - eventVar = "AEDECOD", + eventvar = "AEDECOD", ctrlgrp = "Placebo", trtgrp = "Xanomeline High Dose", statistics = "Risk Ratio", @@ -71,97 +76,121 @@ test_that("Test Case 1: Check if the function gives expected statistic values", sort_var = "Count" ) - actual <- risk_s %>% - rename(AEBODSYS = BYVAR1, AEDECOD = DPTVAL, N = TOTAL_N, n = FREQ) %>% - filter(AEDECOD == "NAUSEA") %>% + actual <- risk_s |> + rename(AEBODSYS = BYVAR1, AEDECOD = DPTVAL, N = TOTAL_N, n = FREQ) |> + filter(AEDECOD == "NAUSEA") |> mutate( N = as.integer(N), n = as.integer(n) - ) %>% + ) |> select(TRTVAR, N, AEBODSYS, AEDECOD, n, RISK, PVALUE, RISKCIL, RISKCIU, PCT) expect_equal(actual$RISK, expected$RISK) expect_equal(actual$PVALUE, expected$PVALUE) - expect_identical(actual, expected) + expect_equal(actual, expected, ignore_attr = TRUE) }) # testcae 2 test_that("Test Case 2: Check if the function works as expected for risk difference", { - dsin1 <- dsin %>% - filter(AEDECOD %in% c("NAUSEA", "SINUS BRADYCARDIA")) %>% - select(USUBJID, TRTA, AEDECOD, AEBODSYS, TRTVAR, BYVAR1) - dout1 <- dout %>% - filter(AEDECOD %in% c("NAUSEA", "SINUS BRADYCARDIA")) %>% - select(USUBJID, TRTA, AEDECOD, AEBODSYS, TRTVAR, BYVAR1) - - denom <- dsin1 %>% - filter(TRTVAR %in% c("Placebo", "Xanomeline High Dose")) %>% - group_by(TRTVAR) %>% - summarise(N = length(unique(USUBJID))) %>% - ungroup() - - freq <- dsin1 %>% - filter(TRTVAR %in% c("Placebo", "Xanomeline High Dose")) %>% - group_by(TRTVAR, AEBODSYS, AEDECOD) %>% - summarise(n = length(unique(USUBJID))) %>% - ungroup() - - exp <- left_join(denom, freq, by = "TRTVAR") - - idvar <- c("AEBODSYS", "AEDECOD") - exp1 <- exp %>% - mutate(TRTVAR = case_when( - TRTVAR == "Placebo" ~ "ctrlgrp", - TRTVAR == "Xanomeline High Dose" ~ "trtgrp" - )) %>% - tidyr::pivot_wider(id_cols = any_of(c(idvar)), names_from = TRTVAR, values_from = c(N, n)) %>% - mutate( - temp1 = N_ctrlgrp - n_ctrlgrp, - temp2 = N_trtgrp - n_trtgrp - ) - - mat <- matrix(c(2, 7, 3, 6), nrow = 2) - risk <- riskdiff_wald(mat, conf.level = 1 - 0.05) + risk <- suppressWarnings(riskdiff_wald(mat, conf.level = 1 - 0.05)) risk_val <- round(risk$measure[2, 1], 3) pval <- round(risk$p.value[2, 3], 4) ciu <- round(risk$measure[2, 2], 4) cil <- round(risk$measure[2, 3], 4) - expected <- exp %>% - filter(AEDECOD == "NAUSEA") %>% + expected <- exp |> + filter(AEDECOD == "NAUSEA") |> mutate( RISK = risk_val, PVALUE = pval, TRTVAR = as.character(TRTVAR) - ) %>% + ) |> arrange(desc(RISK)) risk_s <- risk_stat( datain = dsin1, - d_datain = dout1, + a_subset = ae_pre_process$a_subset, summary_by = "Patients", - eventVar = "AEDECOD", + eventvar = "AEDECOD", ctrlgrp = "Placebo", trtgrp = "Xanomeline High Dose", statistics = "Risk Difference", alpha = 0.05, - cutoff = 2, + cutoff = 0, sort_opt = "Descending", sort_var = "RiskValue" ) - actual <- risk_s %>% - rename(AEBODSYS = BYVAR1, AEDECOD = DPTVAL, N = TOTAL_N, n = FREQ) %>% - filter(AEDECOD == "NAUSEA") %>% + actual <- risk_s |> + rename(AEBODSYS = BYVAR1, AEDECOD = DPTVAL, N = TOTAL_N, n = FREQ) |> + filter(AEDECOD == "NAUSEA") |> mutate( N = as.integer(N), n = as.integer(n) - ) %>% + ) |> select(TRTVAR, N, AEBODSYS, AEDECOD, n, RISK, PVALUE) expect_equal(actual$RISK, expected$RISK) expect_equal(actual$PVALUE, expected$PVALUE) + expect_equal(actual, expected, ignore_attr = TRUE) +}) + +# test case 1 + +test_that("riskdiff_wald: check if the function works as expected", { + evts <- 4 + non_evts <- 6 + control_evts <- 3 + cne <- 8 + + expected_output <- (evts / (evts + non_evts)) - (control_evts / (control_evts + cne)) + actual <- suppressWarnings(riskdiff_wald(matrix(c(evts, control_evts, non_evts, cne), nrow = 2))) + actual_output <- actual$measure[2, 1] + + expect_equal(actual_output, expected_output, ignore_attr = TRUE) +}) + +# test case 2 + +test_that("riskdiff_wald: check for error if `y` argument is not NULL", { + evts <- 4 + non_evts <- 6 + control_evts <- 3 + cne <- 8 + input <- matrix(c(evts, control_evts, non_evts, cne), nrow = 2) + + expect_error( + suppressWarnings( + riskdiff_wald( + x = input, + y = 2, + conf.level = 0.95, + rev = c("neither", "rows", "columns", "both"), + correction = FALSE, + verbose = FALSE + ) + ), + regexp = paste("y argument should be NULL") + ) +}) + +test_that("risk_stat: returns empty data frame when cutoff is too high", { + actual <- risk_stat( + datain = ae_entry, + a_subset = ae_pre_process$a_subset, + summary_by = "Patients", + eventvar = "AEDECOD", + ctrlgrp = "Placebo", + trtgrp = "Xanomeline High Dose", + statistics = "Risk Ratio", + alpha = 0.05, + cutoff = 500, + sort_opt = "Ascending", + sort_var = "Count" + ) + + expected <- data.frame(NULL) expect_identical(actual, expected) }) diff --git a/tests/testthat/test-volcano_plot.R b/tests/testthat/test-volcano_plot.R deleted file mode 100644 index c133a04..0000000 --- a/tests/testthat/test-volcano_plot.R +++ /dev/null @@ -1,113 +0,0 @@ -data(ae_pre) - -risk_stat_out <- risk_stat( - datain = ae_pre$dsin, - d_datain = ae_pre$dout, - summary_by = "Patients", - eventVar = "AEDECOD", - ctrlgrp = "Placebo", - trtgrp = "Xanomeline High Dose", - statistics = "Risk Ratio", - alpha = 0.05, - cutoff = 2, - sort_opt = "Ascending", - sort_var = "Count" -) - -test_that("Test Case 1: volcano_plot works with expected inputs", { - vout <- volcano_plot( - datain = ae_pre, - AE_Filter = "Safety", - statistics_data = risk_stat_out, - statistics = "Risk Difference", - treatment1 = "Placebo", - treatment2 = "Xanomeline Low Dose", - X_ref = 1, - summary_by = "Patients", - pvalue_label = "None", - treatment1_label = "Control", - treatment2_label = "Exposure", - pvalcut = 0.05 - ) - - ptly_data <- vout[["ptly"]][["x"]][["data"]] - legendgroups <- - unlist(purrr::compact(purrr::map( - seq_along(ptly_data), - function(x) ptly_data[[x]][["legendgroup"]] - ))) - - expect_equal(length(vout), 5) - expect_equal(names(vout), c("ptly", "plot", "rpt_data", "title", "footnote")) - expect_true(nrow(vout$rpt_data) > 0) - expect_type(vout$ptly, "list") - expect_equal(legendgroups, sort(unique(vout[["rpt_data"]][["BYVAR1"]]))) - expect_identical(vout$plot$data, vout$rpt_data) - expect_equal(vout$title, "Volcano plot for Risk Difference of Safety Adverse Events") - expect_equal(vout$footnote, "* N is the total number of participants. \nClassifications of adverse events are based on the Medical Dictionary for Regulatory Activities (MedDRA v21.1). \nDashed horizontal line represents p-value of 0.05.\nDotted horizontal line represents FDR adjusted p-value of approximately 0.05 (when applicable). \nDashed Vertical line represents risk value reference line. \nTotals for the No. of Participants/Events at a higher level are not necessarily the sum of those at the lower levels since a participant may report two or more. \nThe number of participants reporting at least 1 occurrence of the event specified.") # nolint -}) - - -test_that("Test Case 2: volcano_plot returns pre processed AE data with - empty `risk_stat()` output", { - risk_stat_out_ <- tibble() - vout <- volcano_plot( - datain = ae_pre, - AE_Filter = "Safety", - statistics_data = risk_stat_out_, - statistics = "Risk Difference", - treatment1 = "Placebo", - treatment2 = "Xanomeline Low Dose", - X_ref = 1, - summary_by = "Patients", - pvalue_label = "None", - treatment1_label = "Control", - treatment2_label = "Exposure", - pvalcut = 0.05 - ) - - expect_equal(length(vout), 3) - expect_equal(names(vout), c("ptly", "plot", "rpt_data")) - expect_identical(ae_pre, vout$rpt_data) - expect_identical(ae_pre$dsin, vout$rpt_data$dsin) -}) - - -test_that("Test Case 3: volcano_plot transforms p values correctly based on `pvalue_label`", { - vout_none <- volcano_plot( - datain = ae_pre, - AE_Filter = "Safety", - statistics_data = risk_stat_out, - statistics = "Risk Difference", - treatment1 = "Placebo", - treatment2 = "Xanomeline Low Dose", - X_ref = 1, - summary_by = "Patients", - pvalue_label = "None", - treatment1_label = "Control", - treatment2_label = "Exposure", - pvalcut = 0.05 - ) - - vout_log <- volcano_plot( - datain = ae_pre, - AE_Filter = "Safety", - statistics_data = risk_stat_out, - statistics = "Risk Difference", - treatment1 = "Placebo", - treatment2 = "Xanomeline Low Dose", - X_ref = 1, - summary_by = "Patients", - pvalue_label = "-log10", - treatment1_label = "Control", - treatment2_label = "Exposure", - pvalcut = 0.05 - ) - - expect_equal(length(vout_none), length(vout_log)) - expect_equal(names(vout_none), names(vout_log)) - expect_false(identical( - layer_scales(vout_log$plot)$y$break_positions(), - layer_scales(vout_none$plot)$y$break_positions() - )) -})