Skip to content

Commit

Permalink
update the calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
wangben718 committed Aug 19, 2024
1 parent a089dd3 commit 13ffb00
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 43 deletions.
4 changes: 2 additions & 2 deletions R/avg.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ avg_event <- function(id, group, par = NULL) {
FUN.VALUE = numeric(1)
)
count <- vapply(split(res, res$Var2),
nrow,
function(x) sum(x$Freq, na.rm = TRUE),
FUN.VALUE = numeric(1)
)
} else {
Expand All @@ -79,7 +79,7 @@ avg_event <- function(id, group, par = NULL) {
par = unique(X$par),
avg = mean(X$n, na.rm = TRUE),
se = sd(X$n, na.rm = TRUE) / sqrt(nrow(X)),
count = nrow(X)
count = sum(X$n)
)
}) |>
do.call(what = rbind) |>
Expand Down
6 changes: 4 additions & 2 deletions R/extend_ae_specific.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ extend_ae_specific_duration <- function(outdata,
#' parameter = "rel"
#' ) |>
#' extend_ae_specific_events() |>
#' format_ae_specific(display = c("n", "prop", "events"))
#' format_ae_specific(display = c("n", "prop", "events_avg"))
#' head(tbl$tbl)
extend_ae_specific_events <- function(outdata) {
meta <- outdata$meta
Expand Down Expand Up @@ -338,9 +338,11 @@ extend_ae_specific_events <- function(outdata) {
count <- rbind(count, blank_row)[order(index), ]
names(count) <- paste0("eventscount_", seq_len(ncol(count)))

# fill NA value with zero
outdata$events_avg <- avg
outdata$events_se <- se
outdata$events_count <- count
# fill NA value with zero
outdata$events_count <- replace(count, is.na(count), 0)
outdata$extend_call <- c(outdata$extend_call, match.call())

outdata
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/test-independent-testing-avg_event.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_that("if par = NULL, return the average number of events in each group (tak

count <- vapply(
split(res, res$Var2),
function(x) nrow(x),
function(x) sum(x$Freq, na.rm = TRUE),
FUN.VALUE = numeric(1)
)

Expand All @@ -33,7 +33,7 @@ test_that("if par = NULL, return the average number of events in each group (tak
expect_equal(avg1, avg2)
})

test_that("if par = NULL, return the average number of events in each group (take the r2rtf::r2rtf_adae dataset as an example)", {
test_that("if par not NULL, return the average number of events in each group (take the r2rtf::r2rtf_adae dataset as an example)", {
r2rtf_adae <- r2rtf::r2rtf_adae
r2rtf_adae$TRTA <- factor(r2rtf_adae$TRTA)

Expand All @@ -49,7 +49,7 @@ test_that("if par = NULL, return the average number of events in each group (tak
dplyr::summarise(
avg = mean(n, na.rm = TRUE),
se = sd(n, na.rm = TRUE) / sqrt(dplyr::n()),
count = dplyr::n(),
count = sum(n),
.groups = "keep"
) |>
tidyr::pivot_wider(id_cols = par, names_from = group, values_from = c("avg", "se", "count"))
Expand Down

0 comments on commit 13ffb00

Please sign in to comment.