|
| 1 | +--- |
| 2 | +title: "Using parameters from csv" |
| 3 | +author: "Amy Heather" |
| 4 | +date: "`r Sys.Date()`" |
| 5 | +output: |
| 6 | + github_document: |
| 7 | + toc: true |
| 8 | +html_preview: false |
| 9 | +--- |
| 10 | + |
| 11 | +## Set-up |
| 12 | + |
| 13 | +Install the latest version of the local simulation package. If running sequentially, `devtools::load_all()` is sufficient. If running in parallel, you must use `devtools::install()`. |
| 14 | + |
| 15 | +```{r} |
| 16 | +devtools::load_all() |
| 17 | +``` |
| 18 | + |
| 19 | +```{r} |
| 20 | +start_time <- Sys.time() |
| 21 | +``` |
| 22 | + |
| 23 | +## Creating parameter class |
| 24 | + |
| 25 | +We can set up classes based on CSV, replacing the default inputs with those from the csv. |
| 26 | + |
| 27 | +If you were only planning to use parameters from CSV, then you could remove the default inputs from the classes in `parameters.R` altogether. |
| 28 | + |
| 29 | +We use the parameter function to process values from the CSV, ensuring that all required parameter names are present and that no unexpected arguments are included. This approach leverages the function's built-in validation to catch any issues with argument names or types. If the parameter classes themselves have validation, those checks will also be performed when the function is called. |
| 30 | +```{r} |
| 31 | +#' Use parameter function to create parameter list using values from dataframe. |
| 32 | +#' |
| 33 | +#' @param df Dataframe with columns "unit", "parameter", "type", "mean" and |
| 34 | +#' "sd". |
| 35 | +#' @param unit Unit name to filter by ("asu" or "rehab"). |
| 36 | +#' @param parameter Parameter name to filter by ("iat", "los" or "routing"). |
| 37 | +#' @param param_function Function to run |
| 38 | +#' |
| 39 | +#' @return Named list of parameters |
| 40 | +
|
| 41 | +init_param_class <- function(df, unit, parameter, param_function) { |
| 42 | +
|
| 43 | + # Filter data to specified unit and parameter |
| 44 | + df_subset <- df[df[["unit"]] == unit & df[["parameter"]] == parameter, ] |
| 45 | + |
| 46 | + # Create named list of parameter values. |
| 47 | + # If all SD values are missing, use only means and name by 'type'. |
| 48 | + # Otherwise, include both mean and sd for each type, with names like |
| 49 | + # 'type_mean' and 'type_sd'. |
| 50 | + if (all(is.na(df_subset$sd))) { |
| 51 | + param_list <- as.list(setNames(df_subset$mean, df_subset$type)) |
| 52 | + } else { |
| 53 | + param_list <- list() |
| 54 | + for (i in seq_len(nrow(df_subset))) { |
| 55 | + row <- df_subset[i, ] |
| 56 | + param_list[[paste0(row$type, "_mean")]] <- row$mean |
| 57 | + param_list[[paste0(row$type, "_sd")]] <- row$sd |
| 58 | + } |
| 59 | + } |
| 60 | +
|
| 61 | + # Run parameter function using list |
| 62 | + do.call(param_function, param_list) |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +```{r} |
| 67 | +#' Generate named_list with create_parameters() using values loaded from a CSV file. |
| 68 | +#' |
| 69 | +#' @param csv_path Path to csv file containing the parameters. Should have columns "unit", "parameter", "type", "mean" and "sd". Missing values should be marked as "NA". |
| 70 | +#' |
| 71 | +#' @return Named list generated by create_parameters() |
| 72 | +
|
| 73 | +setup_param_from_csv <- function(csv_path) { |
| 74 | + # Load parameter data from CSV, treating "NA" as missing values |
| 75 | + df <- read.csv(csv_path, na.strings = "NA") |
| 76 | + |
| 77 | + # Specify mappings of create_parameters() arguments to their corresponding |
| 78 | + # units, parameter types and parameter classes |
| 79 | + param_specs <- list( |
| 80 | + list(name = "asu_arrivals", |
| 81 | + unit = "asu", |
| 82 | + parameter = "iat", |
| 83 | + param_class = create_asu_arrivals), |
| 84 | + list(name = "rehab_arrivals", |
| 85 | + unit = "rehab", |
| 86 | + parameter = "iat", |
| 87 | + param_class = create_rehab_arrivals), |
| 88 | + list(name = "asu_los", |
| 89 | + unit = "asu", |
| 90 | + parameter = "los", |
| 91 | + param_class = create_asu_los), |
| 92 | + list(name = "rehab_los", |
| 93 | + unit = "rehab", |
| 94 | + parameter = "los", |
| 95 | + param_class = create_rehab_los), |
| 96 | + list(name = "asu_routing", |
| 97 | + unit = "asu", |
| 98 | + parameter = "routing", |
| 99 | + param_class = create_asu_routing), |
| 100 | + list(name = "rehab_routing", |
| 101 | + unit = "rehab", |
| 102 | + parameter = "routing", |
| 103 | + param_class = create_rehab_routing) |
| 104 | + ) |
| 105 | + param_kwargs <- list() |
| 106 | + for (spec in param_specs) { |
| 107 | + param_kwargs[[spec$name]] <- init_param_class(df, spec$unit, spec$parameter, spec$param_class) |
| 108 | + } |
| 109 | +
|
| 110 | + do.call(create_parameters, param_kwargs) |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +```{r} |
| 115 | +setup_param_from_csv("../inputs/parameters.csv") |
| 116 | +``` |
| 117 | + |
| 118 | +## Calculate run time |
| 119 | + |
| 120 | +```{r end_timer} |
| 121 | +# Get run time in seconds |
| 122 | +end_time <- Sys.time() |
| 123 | +runtime <- as.numeric(end_time - start_time, units = "secs") |
| 124 | +
|
| 125 | +# Display converted to minutes and seconds |
| 126 | +minutes <- as.integer(runtime / 60L) |
| 127 | +seconds <- as.integer(runtime %% 60L) |
| 128 | +cat(sprintf("Notebook run time: %dm %ds", minutes, seconds)) |
| 129 | +``` |
| 130 | + |
0 commit comments