Demand Forecast

Generation

---
title: "US Electricity"
output: 
  flexdashboard::flex_dashboard:
    vertical_layout: fill
    social: menu
    source_code: embed

---

```{r setup, include=FALSE}
library(flexdashboard)

`%>%` <- magrittr::`%>%`

hex_to_rgb <- function(hex){
  rgb <- paste0(as.numeric(grDevices::col2rgb(hex) %>% base::t()), collapse = ",")
  return(rgb) 
}

load("./data/elec_df.rda")
load("./data/forecast.rda")
load("./data/gen_df.rda")

days <- 3
```

### Demand Forecast

```{r }

fc <- fc_df %>% dplyr::filter(type == "latest")

start <- min(fc$time) - lubridate::hours(24 * days)

df <- elec_df %>% 
  dplyr::filter(date_time > start) %>%
  tidyr::pivot_wider(names_from = type, values_from = series) %>%
  as.data.frame() %>% dplyr::filter(date_time <= max(fc$time))

df$date_time_us <- lubridate::with_tz(time = df$date_time, tzone = "US/Eastern")

mape_df <- fc %>% dplyr::left_join(df %>% 
                                     dplyr::select(time = date_time_us, y = demand), 
                                   by = "time") %>%
  dplyr::filter(!is.na(y)) %>%
  dplyr::mutate(apc = abs(y - yhat) / y,
                coverage_flag = ifelse(y > upper | y < lower, 1, 0))


p <- plotly::plot_ly(data = df) %>%
  plotly::add_lines(x = ~ date_time_us,
                    y = ~ demand,
                    name = "Demand",
                    line = list(color = "#1f77b4")) %>%
  plotly::add_ribbons(x = fc$time, 
                      ymax = fc$upper,
                      ymin = fc$lower,
                      fillcolor = base::paste("rgba(", hex_to_rgb("#457b9d"),",0.2)", sep = ""),
                      line = list(color = base::paste("rgba(", hex_to_rgb("#457b9d"),",0.4)", sep = "")),
                      name = "Prediction Intervals") %>%
  plotly::add_lines(x = fc$time,
                    y = fc$yhat,
                    name = "Demand Forecast",
                    line = list(color = "#457b9d", dash = "dash", width = 2)) %>%
  plotly::add_annotations(text = paste(paste("MAPE: ", round(100 * mean(mape_df$apc), 2), "%", sep = ""), 
                                       paste("Coverage: ", round(100 * (nrow(mape_df) - sum(mape_df$coverage_flag) )/ nrow(mape_df), 2), "%", sep = ""), sep = "
"), xref = "paper", yref = "paper", showarrow = FALSE, x = 0.98, y = 0.05) %>% plotly::layout(title = "The United States (Lower 48) Demand for Electricity Forecast", yaxis = list(title = "Megawatt-Hour"), xaxis = list(title = "Eastern Time
Source: US Energy Information Administration"), hovermode = "compare") p ``` ### Generation ```{r} gen_df %>% dplyr::mutate(time = lubridate::with_tz(time = date_time, tzone = "US/Eastern")) %>% # dplyr::filter(time >= start) %>% plotly::plot_ly( x = ~ date_time, y = ~ value, type = 'scatter', mode = 'none', stackgroup = 'one', fillcolor = ~ type) %>% plotly::layout(title = "The United States (Lower 48) Net Generation by Energy Source", yaxis = list(title = "Megawatt-Hour"), xaxis = list(title = "Eastern Time
Source: US Energy Information Administration", range = c(start, max(fc_df$time))), hovermode = "compare") ```