|
| 1 | +#' This function generates a table of events for specified populations based on the provided hypotheses. |
| 2 | +#' |
| 3 | +#' @param event` dataframe should have the following structure: |
| 4 | +#' - `Population`: A character vector indicating the population groups (e.g., "Population 1", "Population 2", "Population 1 Intersection 2", and "Overall population"). |
| 5 | +#' - `IA`: Numeric vector indicating the number of events observed in each group during interim analysis. |
| 6 | +#' - `FA`: Numeric vector indicating the number of events observed in each group during final analysis. |
| 7 | +#' The dataframe must contain at least these columns and can include additional analysis columns as needed. |
| 8 | +#' @param hypothesis A list of strings where each item represents a hypothesis regarding efficacy, formatted as follows: |
| 9 | +#' - H1: "Efficacy in Population 1" |
| 10 | +#' - H2: "Efficacy in Population 2" |
| 11 | +#' - H3: "Efficacy in Overall population" |
| 12 | +#' Each hypothesis is used for comparisons in the generated event table. |
| 13 | +#' |
| 14 | +#' @return A dataframe with the following columns: |
| 15 | +#' - `one_hypothesis`: The index of the first selected hypothesis from the provided list. |
| 16 | +#' - `another_hypothesis`: The index of the second selected hypothesis from the provided list. |
| 17 | +#' - `analysis`: The index indicating which analysis is being performed (e.g., interim or final). |
| 18 | +#' - `common_events`: The calculated number of common events associated with the selected hypotheses. |
| 19 | +#' |
| 20 | +#' @export |
| 21 | +#' |
| 22 | +#' @examples |
| 23 | +#' #------------------------Example of IA and FA |
| 24 | +#' event <- data.frame( |
| 25 | +#' Population = c("Population 1", "Population 2", "Population 1 Intersection 2", "Overall population"), |
| 26 | +#' IA = c(100, 110, 80, 225), # Interim Analysis values indicating the number of events observed in each group |
| 27 | +#' FA = c(200, 220, 160, 450) |
| 28 | +#' ) |
| 29 | +#' |
| 30 | +#' hypothesis <- list( |
| 31 | +#' H1 = "Efficacy in Population 1", |
| 32 | +#' H2 = "Efficacy in Population 2", |
| 33 | +#' H3 = "Efficacy in Overall population" |
| 34 | +#' ) |
| 35 | +#' |
| 36 | +#' generate_event_table_ol(event, hypothesis) |
| 37 | +#' |
| 38 | +#' #----------------------Example of two IAs and FA |
| 39 | +#' event <- data.frame( |
| 40 | +#' Population = c("Population 1", "Population 2", "Population 1 Intersection 2", "Overall population"), |
| 41 | +#' IA1 = c(100, 110, 80, 225), # First Interim Analysis values indicating the number of events observed in each group |
| 42 | +#' IA2 = c(120, 130, 90, 240), # Second Interim Analysis values indicating the number of events observed in each group |
| 43 | +#' FA = c(200, 220, 160, 450) |
| 44 | +#' ) |
| 45 | +#' |
| 46 | +#' hypothesis <- list( |
| 47 | +#' H1 = "Efficacy in Population 1", |
| 48 | +#' H2 = "Efficacy in Population 2", |
| 49 | +#' H3 = "Efficacy in Overall population" |
| 50 | +#' ) |
| 51 | +#' |
| 52 | +#' generate_event_table_ol(event, hypothesis) |
| 53 | +#' |
| 54 | +generate_event_table_ol <- function(event, hypothesis) { |
| 55 | + result_df <- tibble( |
| 56 | + one_hypothesis = integer(), |
| 57 | + another_hypothesis = integer(), |
| 58 | + analysis = integer(), |
| 59 | + common_events = integer() |
| 60 | + ) |
| 61 | + |
| 62 | + for (i in 1:length(hypothesis)) { |
| 63 | + for (j in i:length(hypothesis)) { |
| 64 | + for (k in 1:(ncol(event) - 1)) { |
| 65 | + hyp_i <- unlist(strsplit(hypothesis[[i]], "Efficacy in "))[2] |
| 66 | + hyp_j <- unlist(strsplit(hypothesis[[j]], "Efficacy in "))[2] |
| 67 | + |
| 68 | + common_factor <- intersect(hyp_i, hyp_j) |
| 69 | + |
| 70 | + if (length(common_factor) > 0) { |
| 71 | + if ("Overall population" %in% c(hyp_i, hyp_j)) { |
| 72 | + eventn <- event[event$Population == "Overall population", k + 1] |
| 73 | + } else { |
| 74 | + eventn <- event[i, k + 1] |
| 75 | + } |
| 76 | + } else if ("Overall population" %in% c(hyp_i, hyp_j)) { |
| 77 | + eventn <- event[i, k + 1] |
| 78 | + } else { |
| 79 | + eventn <- event[event$Population == "Population 1 Intersection 2", k + 1] |
| 80 | + } |
| 81 | + |
| 82 | + result_df <- rbind(result_df, tibble( |
| 83 | + one_hypothesis = i, |
| 84 | + another_hypothesis = j, |
| 85 | + analysis = k, |
| 86 | + common_events = eventn |
| 87 | + )) |
| 88 | + result_df <- result_df[order(result_df$analysis), ] |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return(result_df) |
| 94 | +} |
0 commit comments