-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathlab_5_key.Rmd
305 lines (223 loc) · 7.74 KB
/
lab_5_key.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
---
title: 'Lab week 5: gh-pages, time series with feast & fable, a map'
author: "Allison Horst"
date: "2/2/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
```
## gh-pages set-up
Some of you have already been working in blogdown. But there are *other* ways that you can make your RMarkdown materials available for others to see. We'll create a repo that contains .Rmd and knitted .html files that we can share as web pages!
**Note**: The gh-pages section of this lab is inspired by materials from Dr. Julia Lowndes (https://jules32.github.io/).
- On GitHub: @jules32
- Twitter: @juliesquid
Julia has written a post on how to make website with RMarkdown and GitHub here: https://jules32.github.io/rmarkdown-website-tutorial/
Follow along with these steps to make your gh-pages branch:
- Create a new GitHub repo called 'my-webpage-test'
- Click the 'Branch: master' button
- Type in (**exactly**): `gh-pages`, and create a new branch
- Go to Settings > Branches
- Update the 'Default' branch to `gh-pages`
- Click on `2 branches` and **delete** the master branch
- Update your ReadMe to say something unique, then commit
- Check that it can be viewed as a website with `your-user-name.github.io/repo-name/`
- Clone your repo to work locally in RStudio
- Copy and paste the `data` folder for this week into your project
- Create a new .Rmd called `us-renewables.Rmd` in the project
In that .Rmd:
### 0. Attach packages
```{r}
# For general stuff:
library(tidyverse)
library(janitor)
library(lubridate)
library(here)
library(paletteer)
# For ts stuff:
library(tsibble)
library(fable)
library(fabletools)
library(feasts)
library(forecast)
# For spatial stuff:
library(sf)
library(tmap)
library(mapview)
```
## Monthly US energy consumption (renewables)
### 1. Get the data
We'll explore, then forecast, US energy consumption and production by renewables source. Get the data from `renewables_cons_prod.csv`:
```{r}
us_renew <- read_csv(here("data", "renewables_cons_prod.csv")) %>%
clean_names()
```
Explore the data frame:
```{r}
# View(us_renew)
# names(us_renew)
# unique(us_renew$description)
```
We'll focus on consumption data.
### Clean up data
- Convert description to all lowercase
- Only keep observations for "consumption"
- Remove any "total" observations
```{r}
renew_clean <- us_renew %>%
mutate(description = str_to_lower(description)) %>%
filter(str_detect(description, pattern = "consumption")) %>%
filter(!str_detect(description, pattern = "total"))
```
### Convert `yyyymm` column to date with `lubridate`
```{r}
renew_date <- renew_clean %>%
mutate(yr_mo_day = lubridate::parse_date_time(yyyymm, "ym")) %>%
mutate(month_sep = yearmonth(yr_mo_day)) %>% #coerce to tsibble `yearmonth` format
mutate(value = as.numeric(value)) %>%
drop_na(month_sep, value)
# Want to parse the year and month? We may use this later...
renew_parsed <-renew_date %>%
mutate(month = month(yr_mo_day, label = TRUE)) %>%
mutate(year = year(yr_mo_day))
```
### Make a ggplot
Make, then save, the baseline plot as `renew_gg`:
```{r}
renew_gg <- ggplot(data = renew_date, aes(x = month_sep, y = value, group = description)) +
geom_line(aes(color = description)) +
theme_minimal() +
scale_y_continuous(limits = c(0, 350))
renew_gg
```
Now try updating your color palette using options from paletteer. Use `View(palettes_d_names)` to see all of the discrete scale options. We'll want a palette with at least 7 colors (length >= 7). Find a name of a palette that you like, then update your graph by adding `scale_color_paletteer_d("package::palette")`. Like, if I want to use the `calecopal::figmtn` palette, I'd add:
`renew_gg + scale_color_paletteer_d("calecopal::figmtn")`
Try some out!
```{r}
renew_gg +
scale_color_paletteer_d("calecopal::figmtn")
```
Have some fun trying out different color palettes.
### Coerce to a tsibble:
```{r}
renew_ts <- as_tsibble(renew_parsed, key = description, index = month_sep)
```
### Look at the data in a few different ways:
```{r}
renew_ts %>% autoplot(value)
renew_ts %>% gg_subseries(value)
renew_ts %>% gg_season(value)
# What if gg_season() didn't work? Well we can make this with ggplot anyway!
# Remember our other parsed version (renew parsed):
ggplot(data = renew_parsed, aes(x = month, y = value, group = year)) +
geom_line(aes(color = year)) +
facet_wrap(~ description,
ncol = 1,
scales = "free",
strip.position = "right")
```
### Get just the hydroelectric energy consumption data:
```{r}
hydro_ts <- renew_ts %>%
filter(description == "hydroelectric power consumption")
# Explore:
hydro_ts %>% autoplot(value)
hydro_ts %>% gg_subseries(value)
hydro_ts %>% gg_season(value)
# OK, what if gg_season() doesn't work?
# It's just a function that uses ggplot() to do things we already know how to do in ggplot()!
ggplot(hydro_ts, aes(x = month, y = value, group = year)) +
geom_line(aes(color = year))
```
### Calculate summary data by time using `index_by()`
What if we want to calculate consumption by quarter? We'll use `index_by()` to tell R which "windows" to calculate a value with in.
Quarterly:
```{r}
hydro_quarterly <- hydro_ts %>%
index_by(year_qu = ~ yearquarter(.)) %>% # monthly aggregates
summarise(
avg_consumption = mean(value)
)
head(hydro_quarterly)
```
Or annually:
```{r}
hydro_annual <- hydro_ts %>%
index_by(annual = ~year(.)) %>%
summarize(
avg_consumption = mean(value)
)
ggplot(data = hydro_annual, aes(x = annual, y = avg_consumption)) +
geom_line(color = "darkorange") +
geom_smooth(color = "purple",
size = 0.2,
linetype = "dashed",
fill = "purple",
alpha = 0.2) +
theme_minimal()
```
And if you have higher interval data (e.g. hourly), then you can calculate summaries by week, month, etc. using functions from `tsibble` like:
- `yearweek`
- `yearmonth`
### Decompose the hydro consumption ts data
First, let's check the decomposition (STL):
```{r}
# Find STL decomposition
dcmp <- hydro_ts %>%
model(STL(value ~ season(window = 5)))
# View the components
# components(dcmp)
# Visualize the decomposed components
components(dcmp) %>% autoplot() +
theme_minimal()
# Let's check out the residuals:
hist(components(dcmp)$remainder)
```
### Explore the ACF
```{r}
hydro_ts %>%
ACF(value) %>%
autoplot()
```
We see peaks at 12 months: annual-difference similarities in consumption.
### Forecast future hydro power consumption
```{r}
hydro_model <- hydro_ts %>%
model(
arima = ARIMA(value),
ets = ETS(value)
) %>%
fabletools::forecast(h = "2 years")
hydro_model %>%
autoplot(filter(hydro_ts,
year(month_sep) > 2010),
level = NULL)
```
## Map-of-the-day
A world map with bubbles!
```{r}
# Get spatial data:
world <- read_sf(dsn = here("data","TM_WORLD_BORDERS_SIMPL-0.3-1"), layer = "TM_WORLD_BORDERS_SIMPL-0.3") %>% clean_names()
# Quick & easy option to see those polygons (also for points, lines!)
mapview(world)
# ggplot (static)
world_base <- ggplot(data = world) +
geom_sf(aes(fill = pop2005),
color = NA) +
scale_fill_paletteer_c("viridis::viridis") +
theme_minimal()
world_base
# Let's crop it:
world_base +
coord_sf(xlim = c(-20, 50), ylim = c(-40, 40), expand = FALSE)
```
## Making this a web page:
- Knit to create the html
- Push all updates back to GitHub
Since you are working in a gh-pages branch, this can be a webpage!
- Go to your-github-username.github.io/repo-name/file-name-prefix
- You should see the webpage containing your knitted document!
- Troubleshooting:
- Don't including a trailing slash (404 error)
- Don't include the .Rmd extension (will ask to download .Rmd)
# End Lab Week 5