|
| 1 | +#' Use the confidence interval method to select the number of replications. |
| 2 | +#' |
| 3 | +#' @param replications Number of times to run the model. |
| 4 | +#' @param desired_precision Desired mean deviation from confidence interval. |
| 5 | +#' @param metric Name of performance metric to assess. |
| 6 | +#' @param yaxis_title Label for y axis. |
| 7 | +#' @param path Path inc. filename to save figure to. |
| 8 | +#' @param min_rep A suggested minimum number of replications (default=NULL). |
| 9 | +#' |
| 10 | +#' @importFrom stats sd t.test |
| 11 | +#' @importFrom dplyr filter slice_head select pull |
| 12 | +#' @importFrom ggplot2 ggplot aes geom_line geom_ribbon geom_vline labs |
| 13 | +#' theme_minimal ggsave |
| 14 | +#' @importFrom rlang .data |
| 15 | +#' |
| 16 | +#' @return Dataframe with results from each replication. |
| 17 | +#' @export |
| 18 | + |
| 19 | +confidence_interval_method <- function(replications, desired_precision, metric, |
| 20 | + yaxis_title, path, min_rep = NULL) { |
| 21 | + # Run model for specified number of replications |
| 22 | + param <- parameters(number_of_runs = replications) |
| 23 | + raw_results <- runner(param) |
| 24 | + results <- get_run_results(raw_results) |
| 25 | + |
| 26 | + # If mean of metric is less than 1, multiply by 100 |
| 27 | + if (mean(results[[metric]]) < 1L) { |
| 28 | + results[[paste0("adj_", metric)]] <- results[[metric]] * 100L |
| 29 | + metric <- paste0("adj_", metric) |
| 30 | + } |
| 31 | + |
| 32 | + # Initialise list to store the results |
| 33 | + cumulative_list <- list() |
| 34 | + |
| 35 | + # For each row in the dataframe, filter to rows up to the i-th replication |
| 36 | + # then perform calculations |
| 37 | + for (i in 1L:replications) { |
| 38 | + |
| 39 | + # Filter rows up to the i-th replication |
| 40 | + subset <- results[[metric]][1L:i] |
| 41 | + |
| 42 | + # Calculate mean |
| 43 | + mean <- mean(subset) |
| 44 | + |
| 45 | + # Some calculations require more than 1 observation else will error... |
| 46 | + if (i == 1L) { |
| 47 | + # When only one observation, set to NA |
| 48 | + std_dev <- NA |
| 49 | + ci_lower <- NA |
| 50 | + ci_upper <- NA |
| 51 | + deviation <- NA |
| 52 | + } else { |
| 53 | + # Else, calculate standard deviation, 95% confidence interval, and |
| 54 | + # percentage deviation |
| 55 | + std_dev <- sd(subset) |
| 56 | + ci <- t.test(subset)[["conf.int"]] |
| 57 | + ci_lower <- ci[[1L]] |
| 58 | + ci_upper <- ci[[2L]] |
| 59 | + deviation <- ((ci_upper - mean) / mean) * 100L |
| 60 | + } |
| 61 | + |
| 62 | + # Append to the cumulative list |
| 63 | + cumulative_list[[i]] <- data.frame( |
| 64 | + replications = i, |
| 65 | + cumulative_mean = mean, |
| 66 | + cumulative_std = std_dev, |
| 67 | + ci_lower = ci_lower, |
| 68 | + ci_upper = ci_upper, |
| 69 | + perc_deviation = deviation |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + # Combine the list into a single data frame |
| 74 | + cumulative <- do.call(rbind, cumulative_list) |
| 75 | + |
| 76 | + # Get the minimum number of replications where deviation is less than target |
| 77 | + compare <- cumulative %>% |
| 78 | + filter(.data[["perc_deviation"]] <= desired_precision * 100L) |
| 79 | + if (nrow(compare) > 0L) { |
| 80 | + # Get minimum number |
| 81 | + n_reps <- compare %>% |
| 82 | + slice_head() %>% |
| 83 | + dplyr::select(replications) %>% |
| 84 | + pull() |
| 85 | + print(paste0("Reached desired precision (", desired_precision, ") in ", |
| 86 | + n_reps, " replications.")) |
| 87 | + } else { |
| 88 | + warning("Running ", replications, " replications did not reach ", |
| 89 | + "desired precision (", desired_precision, ").") |
| 90 | + } |
| 91 | + |
| 92 | + # Plot the cumulative mean and confidence interval |
| 93 | + p <- ggplot(cumulative, aes(x = .data[["replications"]], |
| 94 | + y = .data[["cumulative_mean"]])) + |
| 95 | + geom_line() + |
| 96 | + geom_ribbon(aes(ymin = ci_lower, ymax = ci_upper), alpha = 0.2) |
| 97 | + |
| 98 | + # If specified, plot the minimum suggested number of replications |
| 99 | + if (!is.null(min_rep)) { |
| 100 | + p <- p + |
| 101 | + geom_vline(xintercept = min_rep, linetype = "dashed", color = "red") |
| 102 | + } |
| 103 | + |
| 104 | + # Modify labels and style |
| 105 | + p <- p + |
| 106 | + labs(x = "Replications", y = yaxis_title) + |
| 107 | + theme_minimal() |
| 108 | + |
| 109 | + # Save the plot |
| 110 | + ggsave(filename = path, width = 6.5, height = 4L, bg = "white") |
| 111 | + |
| 112 | + return(cumulative) |
| 113 | +} |
0 commit comments