Skip to content

Commit

Permalink
Week 11 seminar
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed May 13, 2024
1 parent b5e48e7 commit 0531892
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 59 deletions.
39 changes: 13 additions & 26 deletions week11/activities.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@


Repeat the daily electricity example, but instead of using a quadratic function of temperature, use a piecewise linear function with the "knot" around 20 degrees Celsius (use predictors `Temperature` & `Temp2`). How can you optimize the choice of knot?

The data can be created as follows.

```r
vic_elec_daily <- vic_elec |>
filter(year(Time) == 2014) |>
index_by(Date = date(Time)) |>
summarise(
Demand = sum(Demand)/1e3,
Temperature = max(Temperature),
Holiday = any(Holiday)
) |>
mutate(
Temp2 = I(pmax(Temperature-20,0)),
Day_Type = case_when(
Holiday ~ "Holiday",
wday(Date) %in% 2:6 ~ "Weekday",
TRUE ~ "Weekend"
)
)
```

Repeat but using all available data, and handling the annual seasonality using Fourier terms.
1. Fit a regression model with a piecewise linear trend and Fourier terms for the US leisure employment data.

```r
leisure <- us_employment |>
filter(Title == "Leisure and Hospitality", year(Month) > 2001) |>
mutate(Employed = Employed / 1000) |>
select(Month, Employed)
```

2. Add a dynamic regression model with the same predictors.
3. How do the models compare on AICc?
4. Does the additional ARIMA component fix the residual autocorrelation problem in the regression model?
5. How different are the forecasts from each model?
13 changes: 12 additions & 1 deletion week11/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ Complete Exercises 1-7 from [Section 7.10 of the book](https://otexts.com/fpp3/r
```{r}
#| output: asis
show_slides(week)
show_activity(week)
```

## Seminar activities

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

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

```{r}
#| output: asis
show_assignments(week)
```
12 changes: 4 additions & 8 deletions week11/seminar_code.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ elec_fit <- vic_elec_daily |>
dhr = ARIMA(log(Demand) ~ Temperature + I(Temperature^2) +
(Day_Type == "Weekday") + fourier(period = "year", K = 4))
)
elec_fit |>
pivot_longer(ets:dhr, names_to="model", values_to="model_fit")

accuracy(elec_fit)

Expand Down Expand Up @@ -87,13 +89,6 @@ elec_fit |>
geom_line() +
geom_line(aes(y = .fitted), col = "red")

# Forecast one day ahead
vic_next_day <- new_data(vic_elec_daily, 1) |>
mutate(Temperature = 26, Day_Type = "Holiday")
forecast(elec_fit, new_data = vic_next_day) |>
autoplot(vic_elec_daily |> tail(14), level = 80) +
labs(y = "Electricity demand (GW)")

# Forecast 14 days ahead
vic_elec_future <- new_data(vic_elec_daily, 14) |>
mutate(
Expand All @@ -105,7 +100,8 @@ vic_elec_future <- new_data(vic_elec_daily, 14) |>
TRUE ~ "Weekend"
)
)
forecast(elec_fit, new_data = vic_elec_future) |>
elec_fit |>
forecast(new_data = vic_elec_future) |>
autoplot(vic_elec_daily |> tail(14), level = 80) +
labs(y = "Electricity demand (GW)")

Expand Down
34 changes: 10 additions & 24 deletions week11/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,15 @@ format:
include-in-header: ../header.tex
---


```{r setup, include=FALSE}
source(here::here("setup.R"))
library(readr)
vic_elec_daily <- vic_elec |>
filter(year(Time) == 2014) |>
index_by(Date = date(Time)) |>
summarise(
Demand = sum(Demand) / 1e3,
Temperature = max(Temperature),
Holiday = any(Holiday)
) |>
mutate(Day_Type = case_when(
Holiday ~ "Holiday",
wday(Date) %in% 2:6 ~ "Weekday",
TRUE ~ "Weekend"
))
```

## Regression with ARIMA errors

\vspace*{0.2cm}\begin{block}{Regression models}\vspace*{-0.2cm}
\begin{block}{Regression models}\vspace*{-0.2cm}
\[
y_t = \beta_0 + \beta_1 x_{1,t} + \dots + \beta_k x_{k,t} + \varepsilon_t,
\]
\end{block}\vspace*{-0.3cm}

* $y_t$ modeled as function of $k$ explanatory variables
$x_{1,t},\dots,x_{k,t}$.
* $y_t$ modeled as function of $k$ explanatory variables $x_{1,t},\dots,x_{k,t}$.
* In regression, we assume that $\varepsilon_t$ is WN.
* Now we want to allow $\varepsilon_t$ to be autocorrelated.
\vspace*{0.1cm}
Expand Down Expand Up @@ -93,7 +72,7 @@ $y_t' = (1-B)^dy_t$,\quad $x_{i,t}' = (1-B)^dx_{i,t}$,\quad and $\eta_t' = (1-B)

## Regression with ARIMA errors

* In R, we can specify an ARIMA($p,d,q$) for the errors, and $d$ levels of differencing will be applied to all variables ($y, x_{1,t},\dots,x_{k,t}$) during estimation.
* In R, we can specify an ARIMA($p,d,q$) for the errors, then $d$ levels of differencing will be applied to all variables ($y, x_{1,t},\dots,x_{k,t}$) during estimation.
* Check that $\varepsilon_t$ series looks like white noise.
* AICc can be calculated for final model.
* Repeat procedure for all subsets of predictors to be considered, and select model with lowest AICc value.
Expand All @@ -106,3 +85,10 @@ results.
* Some predictors are known into the future (e.g., time, dummies).
* Separate forecasting models may be needed for other predictors.
* Forecast intervals ignore the uncertainty in forecasting the predictors.

## Your turn
\fontsize{13}{15}\sf\vspace*{-0.2cm}

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

0 comments on commit 0531892

Please sign in to comment.