diff --git a/week11/activities.md b/week11/activities.md index 98326e9..5b8a09e 100644 --- a/week11/activities.md +++ b/week11/activities.md @@ -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? diff --git a/week11/index.qmd b/week11/index.qmd index b17d754..2a5ea4f 100644 --- a/week11/index.qmd +++ b/week11/index.qmd @@ -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) ``` diff --git a/week11/seminar_code.R b/week11/seminar_code.R index e522e36..9e3525a 100644 --- a/week11/seminar_code.R +++ b/week11/seminar_code.R @@ -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) @@ -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( @@ -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)") diff --git a/week11/slides.qmd b/week11/slides.qmd index cf7df76..c132c5d 100644 --- a/week11/slides.qmd +++ b/week11/slides.qmd @@ -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} @@ -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. @@ -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 +```