-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.Rmd
101 lines (80 loc) · 3.32 KB
/
index.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
---
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 = "<br>"),
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<br> 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<br> Source: US Energy Information Administration",
range = c(start, max(fc_df$time))),
hovermode = "compare")
```