|
| 1 | +#' Get Percentiles for Events |
| 2 | +#' |
| 3 | +#' @param performances nested tibble (default: \code{\link{performances}}) |
| 4 | +#' @return list with percentiles for "event_sum" and "event_max" |
| 5 | +#' @export |
| 6 | +#' @importFrom tidyselect all_of |
| 7 | +#' @importFrom tidyr unnest |
| 8 | +#' @importFrom dplyr select group_by mutate summarise |
| 9 | +get_event_percentiles <- function(performances = keys.lid::performances) { |
| 10 | + |
| 11 | +volume <- performances %>% |
| 12 | + tidyr::unnest(.data$events_sum) |
| 13 | + |
| 14 | +sel_cols <- c("zone_id", |
| 15 | + "lid_name_tidy", |
| 16 | + "scenario_name", |
| 17 | + "lid_area_fraction", |
| 18 | + "runoff_cbm", |
| 19 | + "tBeg", |
| 20 | + "tEnd") |
| 21 | + |
| 22 | +volume_stats <- volume %>% |
| 23 | + dplyr::select(tidyselect::all_of(sel_cols)) %>% |
| 24 | + dplyr::group_by(.data$zone_id, |
| 25 | + .data$lid_name_tidy, |
| 26 | + .data$scenario_name, |
| 27 | + .data$lid_area_fraction) %>% |
| 28 | + dplyr::mutate(runoff_LitrePerSqm = 1000 * .data$runoff_cbm) %>% |
| 29 | + dplyr::summarise(datetime_min = min(.data$tBeg), |
| 30 | + datetime_max = max(.data$tEnd), |
| 31 | + timeperiod_days = as.numeric(diff(c(datetime_min, datetime_max))), |
| 32 | + timeperiod_years = timeperiod_days/365, |
| 33 | + number_of_events = dplyr::n(), |
| 34 | + events_per_year = number_of_events / timeperiod_years, |
| 35 | + runoff_LitrePerSqm_q00 = quantile(.data$runoff_LitrePerSqm, probs = 0), |
| 36 | + runoff_LitrePerSqm_q01 = quantile(.data$runoff_LitrePerSqm, probs = 0.01), |
| 37 | + runoff_LitrePerSqm_q05 = quantile(.data$runoff_LitrePerSqm, probs = 0.05), |
| 38 | + runoff_LitrePerSqm_q10 = quantile(.data$runoff_LitrePerSqm, probs = 0.10), |
| 39 | + runoff_LitrePerSqm_q25 = quantile(.data$runoff_LitrePerSqm, probs = 0.25), |
| 40 | + runoff_LitrePerSqm_q50 = quantile(.data$runoff_LitrePerSqm, probs = 0.5), |
| 41 | + runoff_LitrePerSqm_q75 = quantile(.data$runoff_LitrePerSqm, probs = 0.75), |
| 42 | + runoff_LitrePerSqm_q90 = quantile(.data$runoff_LitrePerSqm, probs = 0.9), |
| 43 | + runoff_LitrePerSqm_q95 = quantile(.data$runoff_LitrePerSqm, probs = 0.95), |
| 44 | + runoff_LitrePerSqm_q99 = quantile(.data$runoff_LitrePerSqm, probs = 0.99), |
| 45 | + runoff_LitrePerSqm_q100 = quantile(.data$runoff_LitrePerSqm, probs = 1)) |
| 46 | + |
| 47 | + |
| 48 | +peak <- performances %>% |
| 49 | + tidyr::unnest(.data$events_max) |
| 50 | + |
| 51 | +sel_cols <- c("zone_id", |
| 52 | + "lid_name_tidy", |
| 53 | + "scenario_name", |
| 54 | + "lid_area_fraction", |
| 55 | + "max_total_runoff_mmPerHour", |
| 56 | + "tBeg", |
| 57 | + "tEnd") |
| 58 | + |
| 59 | +peak_stats <- peak %>% |
| 60 | + dplyr::select(tidyselect::all_of(sel_cols)) %>% |
| 61 | + dplyr::group_by(.data$zone_id, |
| 62 | + .data$lid_name_tidy, |
| 63 | + .data$scenario_name, |
| 64 | + .data$lid_area_fraction) %>% |
| 65 | + dplyr::summarise(datetime_min = min(.data$tBeg), |
| 66 | + datetime_max = max(.data$tEnd), |
| 67 | + timeperiod_days = as.numeric(diff(c(datetime_min, datetime_max))), |
| 68 | + timeperiod_years = timeperiod_days/365, |
| 69 | + number_of_events = dplyr::n(), |
| 70 | + events_per_year = number_of_events / timeperiod_years, |
| 71 | + runoff_max_mmPerHour_q00 = quantile(.data$max_total_runoff_mmPerHour, probs = 0), |
| 72 | + runoff_max_mmPerHour_q01 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.01), |
| 73 | + runoff_max_mmPerHour_q05 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.05), |
| 74 | + runoff_max_mmPerHour_q10 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.10), |
| 75 | + runoff_max_mmPerHour_q25 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.25), |
| 76 | + runoff_max_mmPerHour_q50 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.5), |
| 77 | + runoff_max_mmPerHour_q75 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.75), |
| 78 | + runoff_max_mmPerHour_q90 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.9), |
| 79 | + runoff_max_mmPerHour_q95 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.95), |
| 80 | + runoff_max_mmPerHour_q99 = quantile(.data$max_total_runoff_mmPerHour, probs = 0.99), |
| 81 | + runoff_max_mmPerHour_q100 = quantile(.data$max_total_runoff_mmPerHour, probs = 1)) |
| 82 | + |
| 83 | +list(event_max_percentiles = peak_stats, |
| 84 | + event_sum_percentiles = volume_stats |
| 85 | + ) |
| 86 | +} |
0 commit comments