Skip to content

Commit 14fc721

Browse files
committed
Add forest plot calculation
1 parent b1b7db8 commit 14fc721

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

R/aaa.R

+3
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ utils::globalVariables("WIN_A")
5353
utils::globalVariables("WIN_P")
5454
utils::globalVariables("TIE_A")
5555
utils::globalVariables("linetype")
56+
utils::globalVariables("wins")
57+
utils::globalVariables("losses")
58+
utils::globalVariables("ties")

R/internal.R

+44-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
}
1313

1414
# Computes the win odds from the internal data.
15-
.compute_win_odds <- function(hce_dat, arm_levels) {
15+
.compute_win_odds <- function(hce_dat, arm_levels,
16+
step_outcomes, last_outcome) {
17+
1618
hce_dat <- base::as.data.frame(hce_dat)
1719
hce_dat <- .with_ordered_column(hce_dat)
1820
fit <- hce::calcWO(x = hce_dat, AVAL = "ordered",
@@ -27,8 +29,45 @@
2729
ref = unname(arm_levels["control"]),
2830
GROUP = "outcome")
2931

32+
endpoints <- c(step_outcomes, last_outcome)
33+
hce_dat <- hce_dat %>%
34+
dplyr::mutate_at(dplyr::vars(outcome), factor, levels = c(endpoints, "X"))
35+
36+
calcs_lst <- lapply(seq_along(endpoints), function(x) {
37+
idx <- !(hce_dat$outcome %in% endpoints[1:x])
38+
hce_dat[idx, "outcome"] <- "X"
39+
hce_dat[idx, "ordered"] <- 1000000
40+
wins <- hce::calcWINS(hce_dat, AVAL = "ordered", TRTP = "arm",
41+
ref = unname(arm_levels["control"]),
42+
GROUP = "outcome")
43+
wo <- hce::summaryWO(hce_dat, AVAL = "ordered", TRTP = "arm",
44+
ref = unname(arm_levels["control"]),
45+
GROUP = "outcome")
46+
list("wins" = wins, "wo" = wo)
47+
})
48+
49+
wins_forest <- do.call("rbind", lapply(calcs_lst, function(c_lst) {
50+
wins <- c_lst$wins
51+
nm <- c("value", "LCL", "UCL", "p value")
52+
rbind(data.frame(setNames(wins$WO, nm), "method" = "win odds"),
53+
data.frame(setNames(wins$WR1, nm), "method" = "win ratio"))
54+
}))
55+
56+
wo_bar <- do.call("rbind", lapply(seq_along(calcs_lst), function(i) {
57+
wo <- head(calcs_lst[[i]]$wo$summary, 1)
58+
wo$outcome <- endpoints[i]
59+
wo %>%
60+
dplyr::rename(dplyr::all_of(c(wins = "WIN", losses = "LOSS",
61+
ties = "TIE"))) %>%
62+
tidyr::pivot_longer(cols = c(wins, losses, ties)) %>%
63+
dplyr::mutate_at(dplyr::vars(name), factor,
64+
levels = c("wins", "losses", "ties"))
65+
}))
66+
3067
return(list("win_odds" = win_odds,
31-
"win_odds_outcome" = win_odds_outcome))
68+
"win_odds_outcome" = win_odds_outcome,
69+
"wins_forest" = wins_forest,
70+
"wo_bar" = wo_bar))
3271

3372
}
3473

@@ -333,8 +372,9 @@
333372
x = base::sum(value, na.rm = TRUE),
334373
average = 100 *
335374
as.numeric(stats::prop.test(x, n)$estimate),
336-
se = abs(average - (100 *
337-
as.numeric(stats::prop.test(x, n)$conf.int)[1]))) %>%
375+
se = abs(average -
376+
(100 * as.numeric(
377+
stats::prop.test(x, n)$conf.int)[1]))) %>%
338378
dplyr::ungroup()
339379

340380
# To create ellipsis shape and avoid overlapping between both of them,

R/maraca.R

+7-3
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,11 @@ maraca <- function(
196196
data_last_outcome <- NULL
197197
}
198198

199-
win_odds <- list("win_odds" = NULL, "win_odds_outcome" = NULL)
199+
win_odds <- list("win_odds" = NULL, "win_odds_outcome" = NULL,
200+
"wins_forest" = NULL, "wo_bar" = NULL)
200201
if (compute_win_odds) {
201-
win_odds <- .compute_win_odds(hce_dat, arm_levels)
202+
win_odds <- .compute_win_odds(hce_dat, arm_levels,
203+
step_outcomes, last_outcome)
202204
}
203205

204206
return(
@@ -215,7 +217,9 @@ maraca <- function(
215217
ecdf_by_outcome = ecdf_by_outcome,
216218
data_last_outcome = data_last_outcome,
217219
win_odds = win_odds[["win_odds"]],
218-
win_odds_outcome = win_odds[["win_odds_outcome"]]
220+
win_odds_outcome = win_odds[["win_odds_outcome"]],
221+
wins_forest = win_odds[["wins_forest"]],
222+
wo_bar = win_odds[["wo_bar"]]
219223
),
220224
class = c("maraca")
221225
)

tests/testthat/test_maraca.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,8 @@ test_that("winOddsData", {
516516
arm_levels, column_names = column_names
517517
)
518518

519-
win_odds_list <- .compute_win_odds(data, arm_levels)
519+
win_odds_list <- .compute_win_odds(data, arm_levels,
520+
step_outcomes, last_outcome)
520521
win_odds <- win_odds_list[["win_odds"]]
521522

522523
expect_equal(class(win_odds), "numeric")

0 commit comments

Comments
 (0)