Week 11: Dynamic regression

Tutorial exercises

What you will learn this week

  • How to combine regression models with ARIMA models to form dynamic regression models
  • Dynamic harmonic regression to handle complex seasonality
  • Lagged predictors

Pre-class seminar

Read Chapter 10 of the textbook and watch all embedded videos

Seminar activities

  1. Review the model for US gasoline data from last week to now be a dynamic harmonic regression model.

  2. Turn the half hourly electricity data into daily data using the following code. Try and understand what each line does.

    vic_elec_daily <- vic_elec |>
      index_by(Date = date(Time)) |>    # index by date to turn into daily
      summarise(                        # summarise() below
        Demand = sum(Demand)/1e3,       # Total daily and scaling Mega to Gigawatts
        Temperature = max(Temperature), # take highest temperature for the day
        Holiday = any(Holiday)          # Hol for any half hour is Hol for day
      ) |> # create new variable Day_Type
    mutate(Day_Type = case_when(       # Separate weekdays, weekends and holidays
      Holiday ~ "Holiday",             # If Holiday=TRUE call it a Holiday
      wday(Date) %in% 2:6 ~ "Weekday", # wday() returns 1:7 starting from a Sunday
      TRUE ~ "Weekend"                 # Call everything else a weekend
      ))

    Explore the seasonal patterns.

    1. Fit an ETS, ARIMA and a dynamic harmonic regression model using the following code:
    elec_fit <- vic_elec_daily |>
      model(
        ets = ETS(Demand),
        arima = ARIMA(log(Demand)),
        dhr = ARIMA(log(Demand) ~ Temperature + I(Temperature^2) + 
                      (Day_Type == "Weekday") + 
                      fourier(period = "year", K = 4))
      )

    Explore the model fits and residuals.

    1. Generate forecast for 14-days-ahead using the following code.
    vic_elec_future <- new_data(vic_elec_daily, 14) |>
      mutate(
        Temperature = c(rep(32, 7), rep(25, 7)),
        Holiday = c(TRUE, rep(FALSE, 13)),
        Day_Type = case_when(
          Holiday ~ "Holiday",
          wday(Date) %in% 2:6 ~ "Weekday",
          TRUE ~ "Weekend"
        )
      )
  3. Exam 2024

    • Section A: Q4
    • Section B: Q3 (i-j)
    • Section E: all questions
  4. Exam 2023

    • Section A: Q6
    • Section B: Q3 (i-j)
    • Section D: all questions
    • Section E: all questions

Seminar code

Assignments

  • IA4 is due on Monday 18 May.
  • GA4 is due on Monday 25 May.

Weekly quiz