## 5.10 Exercises

1. Daily electricity demand for Victoria, Australia, during 2014 is contained in elecdaily. The data for the first 20 days can be obtained as follows.

daily20 <- head(elecdaily,20)
1. Plot the data and find the regression model for Demand with temperature as an explanatory variable. Why is there a positive relationship?
2. Produce a residual plot. Is the model adequate? Are there any outliers or influential observations?
3. 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?
4. Give prediction intervals for your forecasts. The following R code will get you started:

autoplot(daily20, facets=TRUE)
daily20 %>%
as.data.frame() %>%
ggplot(aes(x=Temperature, y=Demand)) +
geom_point() +
geom_smooth(method="lm", se=FALSE)
fit <- tslm(Demand ~ Temperature, data=daily20)
checkresiduals(fit)
forecast(fit, newdata=data.frame(Temperature=c(15,35)))
5. Plot Demand vs Temperature for all of the available data in elecdaily. What does this say about your model?

2. Data set mens400 contains the winning times (in seconds) for the men’s 400 meters final in each Olympic Games from 1896 to 2016.

1. Plot the winning time against the year. Describe the main features of the plot.
2. Fit a regression line to the data. Obviously the winning times have been decreasing, but at what average rate per year?
3. Plot the residuals against the year. What does this indicate about the suitability of the fitted line?
4. Predict the winning time for the men’s 400 meters final in the 2020 Olympics. Give a prediction interval for your forecasts. What assumptions have you made in these calculations?
3. Type easter(ausbeer) and interpret what you see.

4. 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.

5. The data set 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.

1. Produce a time plot of the data and describe the patterns in the graph. Identify any unusual or unexpected fluctuations in the time series.
2. Explain why it is necessary to take logarithms of these data before fitting a model.
3. 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.
4. Plot the residuals against time and against the fitted values. Do these plots reveal any problems with the model?
5. Do boxplots of the residuals for each month. Does this reveal any problems with the model?
6. What do the values of the coefficients tell you about each variable?
8. 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.
9. Transform your predictions and intervals to obtain predictions and intervals for the raw data.
10. How could you improve these predictions by modifying the model?
6. The 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 “thousand barrels per day”. Consider only the data to the end of 2004.

1. 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.
2. Select the appropriate number of Fourier terms to include by minimizing the AICc or CV value.
3. Check the residuals of the final model using the checkresiduals() function. 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.)
4. To forecast using harmonic regression, you will need to generate the future values of the Fourier terms. This can be done as follows.

fc <- forecast(fit, newdata=data.frame(fourier(x, K, h)))

where fit is the fitted model using tslm(), K is the number of Fourier terms used in creating fit, and h is the forecast horizon required.

Forecast the next year of data.
5. Plot the forecasts along with the actual data for 2005. What do you find?

7. Data set huron gives the water level of Lake Huron in feet from 1875–1972.

1. Plot the data and comment on its features.
2. Fit a linear regression and compare this to a piecewise linear trend model with a knot at 1915.
3. Generate forecasts from these two models for the period upto 1980 and comment on these.

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:

1. $$\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]$$

2. $$\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]$$

3. $$\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]$$

4. $$\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]$$