Skip to content

Commit 4928981

Browse files
authored
Adding unit tests for all crit fns (#2)
* Adding unit tests for all crit fns * Fixing dep bug. Still need to consider the need for stat helper fns
1 parent 7e3e453 commit 4928981

File tree

8 files changed

+127
-153
lines changed

8 files changed

+127
-153
lines changed

DESCRIPTION

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Description: A collection of criterion functions for statistical evidence genera
1010
License: MIT + file LICENSE
1111
Encoding: UTF-8
1212
Roxygen: list(markdown = TRUE)
13-
RoxygenNote: 7.2.2
13+
RoxygenNote: 7.3.1
1414
Config/testthat/edition: 3
1515
Imports:
1616
chef,
@@ -21,10 +21,10 @@ Imports:
2121
dplyr,
2222
knitr
2323
Suggests:
24-
testthat (>= 3.0.0)
24+
testthat (>= 3.0.0),
2525
pharmaverseadam
2626
Remotes:
27-
hta-pharma/chefStats
27+
hta-pharma/chefStats,
2828
hta-pharma/chef
2929
VignetteBuilder:
3030
knitr

R/crit_by_strata_by_trt.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
crit_bb_nsubev_01 <- function(dat, event_index, subjectid_var, n_subj_event_min, ...){
1818

1919
# Evaluate criterion
20-
crit_accept <- dat[J(event_index)] |>
20+
crit_accept <- dat[list(event_index)] |>
2121
unique(by = c(subjectid_var)) |>
2222
nrow() >= n_subj_event_min
2323

R/crit_endpoint.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ crit_ep_nsubev_01 <- function(dat,
1919
event_index,
2020
subjectid_var,
2121
treatment_var,
22-
n_subj_event_min = 10,
22+
n_subj_event_min,
2323
...) {
24-
stat <- dat[J(event_index)] |>
24+
stat <- dat[list(event_index)] |>
2525
unique(by = c(subjectid_var, treatment_var))
2626

2727
return(any(table(stat[[treatment_var]]) >= n_subj_event_min))

man/crit_ep_nsubev_01.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/helper-04-stats.R

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ n_sub <- function(dat,
1515

1616
return(data.table(
1717
description = "Number of subjects",
18+
qualifiers = NA_character_,
1819
label = "N",
1920
value = stat
2021
))
@@ -35,6 +36,7 @@ n_subev <- function(dat,
3536

3637
return(data.table(
3738
description = "Number of subjects with events",
39+
qualifiers = NA_character_,
3840
label = "n",
3941
value = stat
4042
))
@@ -58,6 +60,7 @@ p_subev <- function(dat,
5860

5961
out <-
6062
data.table(description = "Proportion of subjects with events",
63+
qualifiers = NA_character_,
6164
label = "(%)",
6265
value = n_subev / n_sub * 100)
6366

@@ -101,6 +104,7 @@ summary_stats <- function(dat,
101104

102105
return(data.table(
103106
description = "Summary statistics",
107+
qualifiers = NA_character_,
104108
label = names(stat),
105109
value = as.list(stat)
106110
))
@@ -126,12 +130,10 @@ n_subev_trt_diff <- function(dat,
126130
# In case stat is invalid, e.g. if obs. only exists in one treatment arm then replace stat with NA
127131
stat <- ifelse(length(stat) == 0, NA, stat)
128132

129-
out <-
130-
131-
data.table(description = "Absolute difference in number of subjects with events between treatment arms",
132-
label = "n_trt_diff",
133-
value = stat)
134-
133+
out <- data.table(description = "Absolute difference in number of subjects with events between treatment arms",
134+
qualifiers = NA_character_,
135+
label = "n_trt_diff",
136+
value = stat)
135137

136138
return(out)
137139
}
@@ -157,6 +159,7 @@ contingency2x2_ptest <- function(dat,
157159
# Prepare output
158160
out = data.table::data.table(
159161
description = "Fisher's exact test for count data",
162+
qualifiers = NA_character_,
160163
label = c("Pval_independency", "CI_upper", "CI_lower"),
161164
value = c(res$p.value, res$conf.int[1], res$conf.int[2])
162165
)
@@ -189,6 +192,7 @@ contingency2x2_strata_test <- function(dat,
189192
# Prepare output
190193
out <- data.table::data.table(
191194
description = "Cochran-mante-haenszel test for odds ratios across strata",
195+
qualifiers = NA_character_,
192196
label = c("Pval_independency", "CI_lower", "CI_upper"),
193197
value = c(res$p.value, res$conf.int[[1]], res$conf.int[[2]])
194198

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
test_that("Check crit_bb_nsubev_01", {
2+
# SETUP -------------------------------------------------------------------
3+
4+
dat <- data.table(
5+
INDEX_ = 1:10,
6+
USUBJID = 1:10
7+
)
8+
9+
setkey(dat, INDEX_)
10+
event_index <- c(1, 3, 5, 7)
11+
n_subj_event_min <- 5
12+
subjectid_var <- "USUBJID"
13+
14+
# ACT ---------------------------------------------------------------------
15+
16+
crit_eval_1 <- crit_bb_nsubev_01(dat, event_index, subjectid_var, n_subj_event_min = 3)
17+
crit_eval_2 <- crit_bb_nsubev_01(dat, event_index, subjectid_var, n_subj_event_min = 5)
18+
19+
# EXPECT ------------------------------------------------------------------
20+
21+
expect_true(crit_eval_1)
22+
expect_false(crit_eval_2)
23+
expect_error(crit_bb_nsubev_01(dat, event_index, subjectid_var))
24+
})
25+
26+
test_that("Check crit_bb_pval_01", {
27+
# SETUP -------------------------------------------------------------------
28+
29+
dat <- mk_adae()
30+
dat[["INDEX_"]] <- 1:nrow(dat)
31+
event_index <- c(1:10, 50:100, 600:700)
32+
cell_index <- 500:700
33+
treatment_var <- "TRT01A"
34+
treatment_refval <- "Xanomeline High Dose"
35+
subjectid_var <- "USUBJID"
36+
37+
pval_max_1 <- 0.87
38+
pval_max_2 <- 0.88
39+
40+
# ACT ---------------------------------------------------------------------
41+
42+
crit_eval_1 <- crit_bb_pval_01(
43+
dat = dat,
44+
event_index = event_index,
45+
cell_index = cell_index,
46+
treatment_var = treatment_var,
47+
treatment_refval = treatment_refval,
48+
subjectid_var = subjectid_var,
49+
pval_max = pval_max_1
50+
)
51+
52+
crit_eval_2 <- crit_bb_pval_01(
53+
dat = dat,
54+
event_index = event_index,
55+
cell_index = cell_index,
56+
treatment_var = treatment_var,
57+
treatment_refval = treatment_refval,
58+
subjectid_var = subjectid_var,
59+
pval_max = pval_max_2
60+
)
61+
62+
# EXPECT ------------------------------------------------------------------
63+
64+
p_value <- chefStats::p_val(
65+
dat = dat,
66+
event_index = event_index,
67+
cell_index = dat[["INDEX_"]],
68+
treatment_var = treatment_var,
69+
treatment_refval = treatment_refval,
70+
subjectid_var = subjectid_var
71+
)[["value"]]
72+
73+
expect_equal(crit_eval_1, p_value < pval_max_1)
74+
expect_equal(crit_eval_2, p_value < pval_max_2)
75+
76+
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
test_that("Check crit_ep_nsubev_01", {
2+
# SETUP -------------------------------------------------------------------
3+
4+
dat <- mk_adae()
5+
dat[["INDEX_"]] <- 1:nrow(dat)
6+
setkey(dat, INDEX_)
7+
event_index <- c(1:10, 50:100)
8+
treatment_var <- "TRT01A"
9+
treatment_refval <- "Xanomeline High Dose"
10+
subjectid_var <- "USUBJID"
11+
12+
# ACT ---------------------------------------------------------------------
13+
14+
crit_eval_1 <- crit_ep_nsubev_01(
15+
dat = dat,
16+
event_index = event_index,
17+
subjectid_var = subjectid_var,
18+
treatment_var = treatment_var,
19+
n_subj_event_min = 5
20+
)
21+
crit_eval_2 <- crit_ep_nsubev_01(
22+
dat = dat,
23+
event_index = event_index,
24+
subjectid_var = subjectid_var,
25+
treatment_var = treatment_var,
26+
n_subj_event_min = 10
27+
)
28+
29+
# EXPECT ------------------------------------------------------------------
30+
31+
expect_true(crit_eval_1)
32+
expect_false(crit_eval_2)
33+
34+
})

tests/testthat/test-crit_socpt_01.R

Lines changed: 0 additions & 140 deletions
This file was deleted.

0 commit comments

Comments
 (0)