## 6.10 Exercises

Half-hourly electricity demand for Victoria, Australia is contained in

`vic_elec`

. Extract the January 2014 electricity demand, and aggregate this data to daily with daily total demands and maximum temperatures.`jan14_vic_elec <- vic_elec %>% filter(yearmonth(Time) == yearmonth("2014 Jan")) %>% index_by(Date = as_date(Time)) %>% summarise(Demand = sum(Demand), Temperature = max(Temperature))`

- Plot the data and find the regression model for Demand with temperature as an explanatory variable. Why is there a positive relationship?
- Produce a residual plot. Is the model adequate? Are there any outliers or influential observations?
- Use the model to forecast the electricity demand that you would expect for the next day if the maximum temperature was \(15^\circ\) and compare it with the forecast if the with maximum temperature was \(35^\circ\). Do you believe these forecasts?
- Give prediction intervals for your forecasts. The following R code will get you started:

`jan14_vic_elec %>% gather("Variable", "Value", Demand, Temperature) %>% ggplot(aes(x = Date, y = Value)) + geom_line() + facet_grid(vars(Variable), scales = "free_y") jan14_vic_elec %>% ggplot(aes(x=Temperature, y=Demand)) + geom_point() + geom_smooth(method="lm", se=FALSE) fit <- jan14_vic_elec %>% model(TSLM(Demand ~ Temperature)) augment(fit) %>% gg_tsdisplay(.resid) fit %>% forecast(new_data(jan14_vic_elec, 1) %>% mutate(Temperature = 15)) %>% mutate(interval = hilo(.distribution, 80))`

- Plot Demand vs Temperature for all of the available data in
`vic_elec`

aggregated to daily total demand and maximum temperature. What does this say about your model?

Data set

`olympic_running`

contains the winning times (in seconds) in each Olympic Games sprint from 1896 to 2018.- Plot the winning time against the year. Describe the main features of the plot.
- Fit a regression line to the data. Obviously the winning times have been decreasing, but at what
*average*rate per year? - Plot the residuals against the year. What does this indicate about the suitability of the fitted line?
- Predict the winning time for each race in the 2020 Olympics. Give a prediction interval for your forecasts. What assumptions have you made in these calculations?

An elasticity coefficient is the ratio of the percentage change in the forecast variable (\(y\)) to the percentage change in the predictor variable (\(x\)). Mathematically, the elasticity is defined as \((dy/dx)\times(x/y)\). Consider the log-log model, \[ \log y=\beta_0+\beta_1 \log x + \varepsilon. \] Express \(y\) as a function of \(x\) and show that the coefficient \(\beta_1\) is the elasticity coefficient.

The data set

`fma::fancy`

concerns the monthly sales figures of a shop which opened in January 1987 and sells gifts, souvenirs, and novelties. The shop is situated on the wharf at a beach resort town in Queensland, Australia. The sales volume varies with the seasonal population of tourists. There is a large influx of visitors to the town at Christmas and for the local surfing festival, held every March since 1988. Over time, the shop has expanded its premises, range of products, and staff.- Produce a time plot of the data and describe the patterns in the graph. Identify any unusual or unexpected fluctuations in the time series.
- Explain why it is necessary to take logarithms of these data before fitting a model.
- Use R to fit a regression model to the logarithms of these sales data with a linear trend, seasonal dummies and a “surfing festival” dummy variable.
- Plot the residuals against time and against the fitted values. Do these plots reveal any problems with the model?
- Do boxplots of the residuals for each month. Does this reveal any problems with the model?
- What do the values of the coefficients tell you about each variable?
- What does the Breusch-Godfrey test tell you about your model?
- Regardless of your answers to the above questions, use your regression model to predict the monthly sales for 1994, 1995, and 1996. Produce prediction intervals for each of your forecasts.
- Transform your predictions and intervals to obtain predictions and intervals for the raw data.
- How could you improve these predictions by modifying the model?

The

`fpp2::gasoline`

series consists of weekly data for supplies of US finished motor gasoline product, from 2 February 1991 to 20 January 2017. The units are in “million barrels per day”. Consider only the data to the end of 2004.- Fit a harmonic regression with trend to the data. Experiment with changing the number Fourier terms. Plot the observed gasoline and fitted values and comment on what you see.
- Select the appropriate number of Fourier terms to include by minimising the AICc or CV value.
- Check the residuals of the final model using the
`gg_tsdisplay()`

function. Use a Ljung-Box test to check for correlation in the residuals. Even though the residuals fail the correlation tests, the results are probably not severe enough to make much difference to the forecasts and prediction intervals. (Note that the correlations are relatively small, even though they are significant.) - Forecast the next year of data.
- Plot the forecasts along with the actual data for 2005. What do you find?

Data set

`fma::huron`

gives the water level of Lake Huron in feet from 1875 to 1972.- Plot the data and comment on its features.
- Fit a linear regression and compare this to a piecewise linear trend model with a knot at 1915.
- Generate forecasts from these two models for the period up to 1980 and comment on these.

*(For advanced readers following on from Section 6.7)*.Using matrix notation it was shown that if \(\bm{y}=\bm{X}\bm{\beta}+\bm{\varepsilon}\), where \(\bm{e}\) has mean \(\bm{0}\) and variance matrix \(\sigma^2\bm{I}\), the estimated coefficients are given by \(\hat{\bm{\beta}}=(\bm{X}'\bm{X})^{-1}\bm{X}'\bm{y}\) and a forecast is given by \(\hat{y}=\bm{x}^*\hat{\bm{\beta}}=\bm{x}^*(\bm{X}'\bm{X})^{-1}\bm{X}'\bm{y}\) where \(\bm{x}^*\) is a row vector containing the values of the regressors for the forecast (in the same format as \(\bm{X}\)), and the forecast variance is given by \(var(\hat{y})=\sigma^2 \left[1+\bm{x}^*(\bm{X}'\bm{X})^{-1}(\bm{x}^*)'\right].\)

Consider the simple time trend model where \(y_t = \beta_0 + \beta_1t\). Using the following results, \[ \sum^{T}_{t=1}{t}=\frac{1}{2}T(T+1),\quad \sum^{T}_{t=1}{t^2}=\frac{1}{6}T(T+1)(2T+1) \] derive the following expressions:

\(\displaystyle\bm{X}'\bm{X}=\frac{1}{6}\left[ \begin{array}{cc} 6T & 3T(T+1) \\ 3T(T+1) & T(T+1)(2T+1) \\ \end{array} \right]\)

\(\displaystyle(\bm{X}'\bm{X})^{-1}=\frac{2}{T(T^2-1)}\left[ \begin{array}{cc} (T+1)(2T+1) & -3(T+1) \\ -3(T+1) & 6 \\ \end{array} \right]\)

\(\displaystyle\hat{\beta}_0=\frac{2}{T(T-1)}\left[(2T+1)\sum^T_{t=1}y_t-3\sum^T_{t=1}ty_t \right]\)

\(\displaystyle\hat{\beta}_1=\frac{6}{T(T^2-1)}\left[2\sum^T_{t=1}ty_t-(T+1)\sum^T_{t=1}y_t \right]\)

\(\displaystyle\text{Var}(\hat{y}_{t})=\hat{\sigma}^2\left[1+\frac{2}{T(T-1)}\left(1-4T-6h+6\frac{(T+h)^2}{T+1}\right)\right]\)