Skip to content

Commit

Permalink
Added R code for week3 seminar
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Mar 14, 2024
1 parent 9794cc5 commit de5a782
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 10 deletions.
8 changes: 5 additions & 3 deletions _freeze/week3/index/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"hash": "50901a1a61b70455ef3b4f5e3724c654",
"hash": "d1b4fd6e61e389ce60f2a9b600e83f7a",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Week 3: Time series decomposition\"\n---\n\n::: {.cell}\n\n:::\n\n\n\n\n\n## What you will learn this week\n\n* Transforming data to remove some sources of variation\n* Decomposing a time series into trend-cycle, seasonal and remainder components\n* Seasonal adjustment\n\n## Pre-class activities\n\nRead [Chapter 3 of the textbook](https://otexts.com/fpp3/decomposition.html) and watch all embedded videos\n\n## Exercises (on your own or in tutorial)\n\nComplete Exercises 6-11 from [Section 2.10 of the book](https://otexts.com/fpp3/graphics-exercises.html).\n\n\n\n\n\n\n## Slides for seminar\n\n<iframe src='https://docs.google.com/gview?url=https://af.numbat.space/week3/slides.pdf&embedded=true' width='100%' height=465></iframe>\n<a href=https://af.numbat.space/week3/slides.pdf class='badge badge-small badge-red'>Download pdf</a>\n\n## Seminar activities\n\n\n1. Produce an STL decomposition as follows\n\n ```r\n us_construction <- us_employment |>\n filter(Title == \"Construction\", year(Month) > 1980)\n dcmp <- us_construction |>\n model(stl = STL(Employed ~ trend(window = 9) + season(window = 11)))\n dcmp |>\n components() |>\n autoplot()\n ```\n\n2. What happens as you change the values of the two `window` arguments?\n\n3. How does the seasonal shape change over time? [Hint: Try plotting the seasonal component using `gg_season`.]\n\n4. Can you produce a plausible seasonally adjusted series? [Hint: `season_adjust` is one of the variables returned by `STL`.]\n\n\n\n## Assignments\n\n* [Assignment 2](../assignments/A2.qmd) is due on Friday 22 March.\n",
"supporting": [],
"markdown": "---\ntitle: \"Week 3: Time series decomposition\"\n---\n\n::: {.cell}\n\n:::\n\n\n\n\n## What you will learn this week\n\n* Transforming data to remove some sources of variation\n* Decomposing a time series into trend-cycle, seasonal and remainder components\n* Seasonal adjustment\n\n## Pre-class activities\n\nRead [Chapter 3 of the textbook](https://otexts.com/fpp3/decomposition.html) and watch all embedded videos\n\n## Exercises (on your own or in tutorial)\n\nComplete Exercises 6-11 from [Section 2.10 of the book](https://otexts.com/fpp3/graphics-exercises.html).\n\n\n\n\n## Slides for seminar\n\n<iframe src='https://docs.google.com/gview?url=https://af.numbat.space/week3/slides.pdf&embedded=true' width='100%' height=465></iframe>\n<a href=https://af.numbat.space/week3/slides.pdf class='badge badge-small badge-red'>Download pdf</a>\n\n\n\n\n[**R code used in seminar**](seminar_code.R)\n\n## Seminar activities\n\n\n\n\n\n\n1. Produce an STL decomposition as follows\n\n ```r\n us_construction <- us_employment |>\n filter(Title == \"Construction\", year(Month) > 1980)\n dcmp <- us_construction |>\n model(stl = STL(Employed ~ trend(window = 9) + season(window = 11)))\n dcmp |> components() |> autoplot()\n ```\n2. What happens as you change the values of the two `window` arguments?\n3. How does the seasonal shape change over time? *[Hint:&nbsp;Try&nbsp;plotting the seasonal component using `gg_season`.]*\n4. Can you produce a plausible seasonally adjusted series? *[Hint:&nbsp;`season_adjust` is returned by `components()`.]*\n\n\n## Assignments\n\n* [Assignment 2](../assignments/A2.qmd) is due on Friday 22 March.\n",
"supporting": [
"index_files"
],
"filters": [
"rmarkdown/pagebreak.lua"
],
Expand Down
Binary file modified _freeze/week3/slides/figure-beamer/abs3-1.pdf
Binary file not shown.
Binary file modified _freeze/week3/slides/figure-beamer/abs4-1.pdf
Binary file not shown.
Binary file modified _freeze/week3/slides/figure-beamer/abs5-1.pdf
Binary file not shown.
Binary file modified _freeze/week3/slides/figure-beamer/abs6-1.pdf
Binary file not shown.
Binary file modified _freeze/week3/slides/figure-beamer/abs7-1.pdf
Binary file not shown.
Binary file modified _freeze/week3/slides/figure-beamer/abs8-1.pdf
Binary file not shown.
Binary file modified _freeze/week3/slides/figure-beamer/abs9-1.pdf
Binary file not shown.
15 changes: 13 additions & 2 deletions week3/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,21 @@ Read [Chapter 3 of the textbook](https://otexts.com/fpp3/decomposition.html) and

Complete Exercises 6-11 from [Section 2.10 of the book](https://otexts.com/fpp3/graphics-exercises.html).


```{r}
#| output: asis
show_slides(week)
show_activity(week)
```

[**R code used in seminar**](seminar_code.R)

## Seminar activities

```{r}
#| child: activities.md
```

```{r}
#| output: asis
#| echo: false
show_assignments(week)
```
24 changes: 19 additions & 5 deletions week3/seminar_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ library(fpp3)

# Australian monthly electricity production
elec1 <- as_tsibble(fma::elec) |>
transmute(Month = index, GWh = value)
rename(Month = index, GWh = value)

elec1

elec1 |>
autoplot(box_cox(GWh, lambda = 0.3))
autoplot(GWh)

lambda <- elec1 |> features(GWh, guerrero)
elec1 |>
autoplot(box_cox(GWh, lambda = lambda))

dcmp <- elec1 |>
model(stl = STL(box_cox(GWh, lambda = 0.3)))
Expand All @@ -19,14 +25,16 @@ components(dcmp) |>

elec1 |>
autoplot(GWh, color = "gray") +
autolayer(components(dcmp), inv_box_cox(trend, lambda = 0.3), color = "red")
autolayer(components(dcmp), inv_box_cox(trend, lambda = 0.3),
color = "red")

elec1 |>
autoplot(GWh, color = "gray") +
autolayer(components(dcmp), inv_box_cox(season_adjust, lambda = 0.3), color = "red")

elec1 |>
model(STL(log(GWh) ~ season(window = 3333) + trend(window = 7), robust = TRUE)) |>
model(STL(log(GWh) ~ season(window = 11) + trend(window = 909995),
robust = TRUE)) |>
components() |>
autoplot()

Expand Down Expand Up @@ -68,14 +76,20 @@ us_construction <- us_employment |>
us_construction |> autoplot(Employed)

dcmp <- us_construction |>
model(stl = STL(Employed ~ trend(window = 9) + season(window = 7)))
model(stl = STL(Employed ~ trend(window = 9) +
season(window = 9)))
dcmp |>
components() |>
autoplot()
dcmp |>
components() |>
gg_season(season_year)

dcmp |>
components() |>
as_tsibble() |>
autoplot(season_adjust)


## ABS employment problem

Expand Down

0 comments on commit de5a782

Please sign in to comment.