|
2 | 2 |
|
3 | 3 | When observations are not independent, GAMs can be used to either incorporate: |
4 | 4 |
|
5 | | -- a serial correlation structure to model residual autocorrelation |
6 | | - (autoregressive: AR; moving average: MA; or a combination of the two: |
7 | | - ARMA), |
| 5 | +- a correlation structure to model autocorrelated residuals, such as: |
| 6 | + - the autoregressive (AR) model |
| 7 | + - the moving average model (MA); or, |
| 8 | + - a combination of both models (ARMA). |
8 | 9 | - random effects that model independence among observations from the |
9 | 10 | same site. |
10 | 11 |
|
11 | 12 | That is, in addition to changing the basis as with the `nottem` example, we can also add complexity to the model by incorporating an autocorrelation structure or mixed effects using the `gamm()` function in the `mgcv` package. Although we will not be using it here, the [`gamm4`](https://cran.r-project.org/web/packages/gamm4/gamm4.pdf) package can also be used to estimate GAMMs in R. |
12 | 13 |
|
13 | 14 | ## Residual autocorrelation |
14 | 15 |
|
| 16 | +**Autocorrelation of residuals** refers to the degree of correlation between the residuals (the differences between the actual and predicted values) in a time series model. |
| 17 | + |
| 18 | +In other words, if there is an autocorrelation of residuals in a time series model, it means that there is a pattern or relationship between the residuals at one point in time and the residuals at other points in time. |
| 19 | + |
| 20 | +Autocorrelation of residuals is usually measured using the **ACF (autocorrelation function)** and **pACF (partial autocorrelation function)** graphs, which show the correlation between residuals at different lags. |
| 21 | + |
| 22 | +#### The autocorrelation function |
| 23 | + |
| 24 | +The autocorrelation function (ACF) of a stationary time series can be defined using the following equation: |
| 25 | + |
| 26 | +$$ACF(k) = Corr(Y_t, Y_{t-k})$$ |
| 27 | +where $Y_t$ is the value of the time series at time $t$, $Y_{t-k}$ is the value of the time series at time $t-k$, and $Corr()$ is the correlation coefficient between two random variables. |
| 28 | + |
| 29 | +In other words, the ACF($k$) is the correlation between the values of the time series $Y_t$ and $Y_{t-k}$, where $k$ is the lag between the two points in time. The ACF is a measure of the strength of the correlation between each value in the time series and its lagged values at different times. |
| 30 | + |
| 31 | +#### The partial autocorrelation function |
| 32 | + |
| 33 | +The partial autocorrelation function (pACF) of a stationary time series can be defined using the following recursive formula: |
| 34 | + |
| 35 | +$$pACF(1) = Corr(Y_1, Y_2)$$ |
| 36 | + |
| 37 | +$$pACF(k) = [ Corr(Y_k, Y_{k+1} - \hat{\phi}{k,1}Y{k}) ] / [ Corr(Y_1, Y_2 - \hat{\phi}_{1,1}Y_1) ]$$ |
| 38 | + |
| 39 | +for $k > 1$ |
| 40 | + |
| 41 | +where $Y_t$ is the value of the time series at time $t$, $\hat{\phi}{k,1}$, $\hat{\phi}{1,1}$, $...$ $\hat{\phi}{k-1,k-1}$ are the coefficients of the autoregressive model of order $k-1$ fitted to the time series, and $Corr()$ is the coefficient of correlation between two random variables |
| 42 | + |
| 43 | +In other words, the pACF($k$) is the correlation between the values of the time series $Y_k$ and $Y_{k+j}$ after removing the influence of intermediate lags $Y_{k+1}, Y_{k+2}, ..., Y_{k+j-1}$ using an autoregressive model of order $k-1$. |
| 44 | +The pACF measures the correlation between $Y_k$ and $Y_{k+j}$ after removing the effect of any shorter intermediate lags. |
| 45 | + |
| 46 | +If the **ACF** or **pACF** graphs show significant correlations at non-zero lags, there is evidence of autocorrelation in the residuals and the model may need to be modified or improved to better capture the underlying patterns in the data. |
| 47 | + |
| 48 | +Let's see how this works with our `year_gam` model! |
| 49 | + |
15 | 50 | To start, let's have a look at a model with temporal autocorrelation in the residuals. We will revisit the Nottingham temperature model and test for correlated errors using the (partial) autocorrelation function. |
16 | 51 |
|
17 | 52 | ```{r, echo = TRUE, eval = FALSE} |
|
0 commit comments