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 ard_event_rates() #244

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
edelarua committed Nov 25, 2024
commit 83bc5845981c5936d2906bc17d33b4c0d6d8ef92
143 changes: 143 additions & 0 deletions tests/testthat/_snaps/ard_event_rates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# ard_event_rates() works with default settings

Code
print(res, n = 20, columns = "all")
Message
{cards} data frame: 207 x 11
Output
group1 group1_level variable variable_level context stat_name stat_label stat fmt_fn warning error
1 TRTA Placebo AESOC CARDIAC … event_ra… n n 13 0
2 TRTA Placebo AESOC CARDIAC … event_ra… N N 301 0
3 TRTA Placebo AESOC CARDIAC … event_ra… p % 0.043 <fn>
4 TRTA Placebo AESOC CONGENIT… event_ra… n n 0 0
5 TRTA Placebo AESOC CONGENIT… event_ra… N N 301 0
6 TRTA Placebo AESOC CONGENIT… event_ra… p % 0 <fn>
7 TRTA Placebo AESOC EAR AND … event_ra… n n 1 0
8 TRTA Placebo AESOC EAR AND … event_ra… N N 301 0
9 TRTA Placebo AESOC EAR AND … event_ra… p % 0.003 <fn>
10 TRTA Placebo AESOC EYE DISO… event_ra… n n 4 0
11 TRTA Placebo AESOC EYE DISO… event_ra… N N 301 0
12 TRTA Placebo AESOC EYE DISO… event_ra… p % 0.013 <fn>
13 TRTA Placebo AESOC GASTROIN… event_ra… n n 17 0
14 TRTA Placebo AESOC GASTROIN… event_ra… N N 301 0
15 TRTA Placebo AESOC GASTROIN… event_ra… p % 0.056 <fn>
16 TRTA Placebo AESOC GENERAL … event_ra… n n 21 0
17 TRTA Placebo AESOC GENERAL … event_ra… N N 301 0
18 TRTA Placebo AESOC GENERAL … event_ra… p % 0.07 <fn>
19 TRTA Placebo AESOC HEPATOBI… event_ra… n n 1 0
20 TRTA Placebo AESOC HEPATOBI… event_ra… N N 301 0
Message
i 187 more rows
i Use `print(n = ...)` to see more rows

---

Code
print(ard_event_rates(group_by(cards::ADAE, TRTA), variables = AESOC, id = USUBJID, denominator = dplyr::rename(cards::ADSL, TRTA = ARM)), n = 20, columns = "all")
Message
{cards} data frame: 207 x 11
Output
group1 group1_level variable variable_level context stat_name stat_label stat fmt_fn warning error
1 TRTA Placebo AESOC CARDIAC … event_ra… n n 13 0
2 TRTA Placebo AESOC CARDIAC … event_ra… N N 86 0
3 TRTA Placebo AESOC CARDIAC … event_ra… p % 0.151 <fn>
4 TRTA Placebo AESOC CONGENIT… event_ra… n n 0 0
5 TRTA Placebo AESOC CONGENIT… event_ra… N N 86 0
6 TRTA Placebo AESOC CONGENIT… event_ra… p % 0 <fn>
7 TRTA Placebo AESOC EAR AND … event_ra… n n 1 0
8 TRTA Placebo AESOC EAR AND … event_ra… N N 86 0
9 TRTA Placebo AESOC EAR AND … event_ra… p % 0.012 <fn>
10 TRTA Placebo AESOC EYE DISO… event_ra… n n 4 0
11 TRTA Placebo AESOC EYE DISO… event_ra… N N 86 0
12 TRTA Placebo AESOC EYE DISO… event_ra… p % 0.047 <fn>
13 TRTA Placebo AESOC GASTROIN… event_ra… n n 17 0
14 TRTA Placebo AESOC GASTROIN… event_ra… N N 86 0
15 TRTA Placebo AESOC GASTROIN… event_ra… p % 0.198 <fn>
16 TRTA Placebo AESOC GENERAL … event_ra… n n 21 0
17 TRTA Placebo AESOC GENERAL … event_ra… N N 86 0
18 TRTA Placebo AESOC GENERAL … event_ra… p % 0.244 <fn>
19 TRTA Placebo AESOC HEPATOBI… event_ra… n n 1 0
20 TRTA Placebo AESOC HEPATOBI… event_ra… N N 86 0
Message
i 187 more rows
i Use `print(n = ...)` to see more rows

# ard_event_rates(statistic) works

Code
ard_event_rates(cards::ADAE, variables = SEX, id = USUBJID, by = TRTA, denominator = dplyr::rename(cards::ADSL, TRTA = ARM), statistic = ~"n")
Message
{cards} data frame: 6 x 11
Output
group1 group1_level variable variable_level stat_name stat_label stat
1 TRTA Placebo SEX F n n 40
2 TRTA Placebo SEX M n n 29
3 TRTA Xanomeli… SEX F n n 37
4 TRTA Xanomeli… SEX M n n 42
5 TRTA Xanomeli… SEX F n n 44
6 TRTA Xanomeli… SEX M n n 33
Message
i 4 more variables: context, fmt_fn, warning, error

# ard_event_rates(ordered) works

Code
print(res, n = 20, columns = "all")
Message
{cards} data frame: 27 x 11
Output
group1 group1_level variable variable_level context stat_name stat_label stat fmt_fn warning error
1 TRTA Placebo AESEV MILD event_ra… n n 36 0
2 TRTA Placebo AESEV MILD event_ra… N N 86 0
3 TRTA Placebo AESEV MILD event_ra… p % 0.419 <fn>
4 TRTA Placebo AESEV MODERATE event_ra… n n 26 0
5 TRTA Placebo AESEV MODERATE event_ra… N N 86 0
6 TRTA Placebo AESEV MODERATE event_ra… p % 0.302 <fn>
7 TRTA Placebo AESEV SEVERE event_ra… n n 7 0
8 TRTA Placebo AESEV SEVERE event_ra… N N 86 0
9 TRTA Placebo AESEV SEVERE event_ra… p % 0.081 <fn>
10 TRTA Xanomeli… AESEV MILD event_ra… n n 22 0
11 TRTA Xanomeli… AESEV MILD event_ra… N N 84 0
12 TRTA Xanomeli… AESEV MILD event_ra… p % 0.262 <fn>
13 TRTA Xanomeli… AESEV MODERATE event_ra… n n 49 0
14 TRTA Xanomeli… AESEV MODERATE event_ra… N N 84 0
15 TRTA Xanomeli… AESEV MODERATE event_ra… p % 0.583 <fn>
16 TRTA Xanomeli… AESEV SEVERE event_ra… n n 8 0
17 TRTA Xanomeli… AESEV SEVERE event_ra… N N 84 0
18 TRTA Xanomeli… AESEV SEVERE event_ra… p % 0.095 <fn>
19 TRTA Xanomeli… AESEV MILD event_ra… n n 19 0
20 TRTA Xanomeli… AESEV MILD event_ra… N N 84 0
Message
i 7 more rows
i Use `print(n = ...)` to see more rows

# ard_event_rates() errors with incomplete factor columns

Code
ard_event_rates(dplyr::mutate(cards::ADAE, AESOC = factor(AESOC, levels = character(
0))), variables = AESOC, id = USUBJID, by = TRTA)
Message
* Removing 1191 rows from `data` with NA or NaN values in "TRTA" and "AESOC" columns.
Condition
Error in `ard_event_rates()`:
! Factors with empty "levels" attribute are not allowed, which was identified in column "AESOC".

---

Code
ard_event_rates(dplyr::mutate(cards::ADAE, SEX = factor(SEX, levels = c("F",
"M", NA), exclude = NULL)), variables = SEX, id = USUBJID, by = TRTA)
Condition
Error in `ard_event_rates()`:
! Factors with NA levels are not allowed, which are present in column "SEX".

# ard_event_rates() works without any variables

Code
ard_event_rates(data = cards::ADAE, variables = starts_with("xxxx"), id = USUBJID,
by = c(TRTA, AESEV))
Message
{cards} data frame: 0 x 0
Output
data frame with 0 columns and 0 rows

211 changes: 211 additions & 0 deletions tests/testthat/test-ard_event_rates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
test_that("ard_event_rates() works with default settings", {
withr::local_options(list(width = 200))

expect_silent(
res <- ard_event_rates(
cards::ADAE,
variables = AESOC,
id = USUBJID,
by = TRTA
)
)
expect_snapshot(res |> print(n = 20, columns = "all"))

expect_equal(
res |>
dplyr::filter(
group1_level == "Placebo",
variable_level == "CARDIAC DISORDERS",
stat_name == "n"
) |>
get_ard_statistics(),
list(
n = cards::ADAE |>
dplyr::filter(
TRTA == "Placebo",
AESOC == "CARDIAC DISORDERS"
) |>
dplyr::slice_tail(n = 1L, by = all_of(c("USUBJID", "TRTA", "AESOC"))) |>
nrow()
)
)

# with denominator
expect_snapshot(
ard_event_rates(
cards::ADAE |> group_by(TRTA),
variables = AESOC,
id = USUBJID,
denominator = cards::ADSL |> dplyr::rename(TRTA = ARM)
) |>
print(n = 20, columns = "all")
)

# with multiple variables
expect_silent(
res2 <- ard_event_rates(
cards::ADAE,
variables = c(SEX, AESOC),
id = USUBJID,
by = TRTA
)
)
expect_equal(unique(res2$variable), c("SEX", "AESOC"))
expect_equal(
res,
res2[-c(1:18), ]
)
})

test_that("ard_event_rates(statistic) works", {
withr::local_options(list(width = 200))

expect_snapshot(
ard_event_rates(
cards::ADAE,
variables = SEX,
id = USUBJID,
by = TRTA,
denominator = cards::ADSL |> dplyr::rename(TRTA = ARM),
statistic = ~"n"
)
)
})

test_that("ard_event_rates(ordered) works", {
withr::local_options(list(width = 200))

# pre-ordered factor variable
adae <- cards::ADAE |>
mutate(AESEV = factor(cards::ADAE$AESEV, ordered = TRUE))

expect_silent(
res <- ard_event_rates(
cards::ADAE,
variables = AESEV,
id = USUBJID,
by = TRTA,
denominator = cards::ADSL |> dplyr::rename(TRTA = ARM),
ordered = TRUE
)
)
expect_snapshot(res |> print(n = 20, columns = "all"))

expect_equal(
res |>
dplyr::filter(
group1_level == "Placebo",
variable_level == "MODERATE",
stat_name == "n"
) |>
get_ard_statistics(),
list(
n = adae |>
dplyr::arrange(AESEV) |>
dplyr::slice_tail(n = 1L, by = all_of(c("USUBJID", "TRTA"))) |>
dplyr::filter(
TRTA == "Placebo",
AESEV == "MODERATE"
) |>
nrow()
)
)

res_unord <- ard_event_rates(
cards::ADAE,
variables = AESEV,
id = USUBJID,
by = TRTA,
denominator = cards::ADSL |> dplyr::rename(TRTA = ARM)
)
expect_true(res$stat[[1]] != res_unord$stat[[1]])

res2 <- ard_event_rates(
adae,
variables = AESEV,
id = USUBJID,
by = TRTA,
denominator = cards::ADSL |> dplyr::rename(TRTA = ARM)
)
expect_equal(res, res2)
})

test_that("ard_event_rates() errors with incomplete factor columns", {
# Check error when factors have no levels
expect_snapshot(
error = TRUE,
ard_event_rates(
cards::ADAE |>
dplyr::mutate(AESOC = factor(AESOC, levels = character(0))),
variables = AESOC,
id = USUBJID,
by = TRTA
)
)

# Check error when factor has NA level
expect_snapshot(
error = TRUE,
ard_event_rates(
cards::ADAE |>
dplyr::mutate(SEX = factor(SEX, levels = c("F", "M", NA), exclude = NULL)),
variables = SEX,
id = USUBJID,
by = TRTA
)
)
})

test_that("ard_hierarchical_count() works with by variable not present in 'denominator'", {
expect_silent(
ard_events_with_by <- ard_event_rates(
data = cards::ADAE,
variables = AESOC,
id = USUBJID,
by = c(TRTA, AESEV),
statistic = ~"n"
)
)

expect_equal(
ard_events_with_by |>
dplyr::filter(
group1_level == "Placebo",
group2_level == "MILD",
variable_level == "CARDIAC DISORDERS"
) |>
get_ard_statistics(),
list(
n = cards::ADAE |>
dplyr::filter(
TRTA == "Placebo",
AESEV == "MILD",
AESOC == "CARDIAC DISORDERS"
) |>
dplyr::slice_tail(n = 1L, by = all_of(c("USUBJID", "TRTA", "AESEV", "AESOC"))) |>
nrow()
)
)
})

test_that("ard_event_rates() works without any variables", {
expect_snapshot(
ard_event_rates(
data = cards::ADAE,
variables = starts_with("xxxx"),
id = USUBJID,
by = c(TRTA, AESEV)
)
)
})

test_that("ard_event_rates() follows ard structure", {
expect_silent(
ard_event_rates(
cards::ADAE,
variables = AESOC,
id = USUBJID
) |>
cards::check_ard_structure(method = FALSE)
)
})