R/aa_troubleshoot.R

Defines functions troubleshoot_aa

Documented in troubleshoot_aa

#' AA Troubleshooter
#'
#' This function prints aggregate statistics from the AA buy/sell matrix. This
#' can help an analyst determine which commodities (if any) may cause problems.
#'
#' @param db A scenario database.
#' @return A [DT](`datatable`) object.
#'
#' @export
troubleshoot_aa <- function(db){

  a <- tbl(db, "ExchangeResults") %>%
    group_by(TSTEP, Commodity) %>%
    summarise(
      demand_weighted_price = sum(Demand * Price) / sum(Demand),
      mean_price = mean(price)
    ) %>%

    collect() %>%
    gather(series, value, demand_weighted_price, mean_price) %>%

  # calculate indexed value and volatility for each data series
    group_by(Commodity, series) %>%
    mutate(
      indexed = round(calc_index(value), 2),
      indexed = ifelse(is.nan(indexed), 0, indexed),
      growth = round(calc_grt(value), 2)
    ) %>%
    group_by(Commodity, series) %>%

    summarise(
      maximum_index = max(indexed),
      which_max = which.max(indexed)[1],
      minimum_index = min(indexed),
      which_min = which.min(indexed)[1],
      volatile_grt = max(growth) - min(growth),
      volatility = sd(growth)
    )

  DT::datatable(a)

}
pbsag/swimr documentation built on Dec. 12, 2020, 3:08 a.m.