class: center, middle, inverse, title-slide .title[ # Time Series Data ] .author[ ### Yue Jiang ] .date[ ### STA 440 / Spring 2025 ] --- ### Disclaimer (even moreso than usual) (I guess it's too late for you now, but) take STA 444! Like most (all?) of the models we cover in STA 440, we're going to provide a very surface-level treatment of these modeling topics which fully deserve an entire class of their own. We'll be focusing on general concepts and ideas, not mathematical or technical details. --- ### What could these data be? <img src="time-series_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> .question[ What do you notice about these data? ] --- ### Surface ozone <img src="img/ozone.jpg" width="100%" style="display: block; margin: auto;" /> --- ### The data again <img src="time-series_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- ### A residual plot <img src="time-series_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- ### Descriptive aspects There are lots of aspects to consider descriptively that relate to different "scales" of the data: - What is the overall .vocab[trend] in the data? Do average values change through time? - What is the .vocab[seasonality] of the data? On what time-scale(s) does seasonality operate? - Do you notice any patterns in the .vocab[random errors]? Is the variance constant? Are there extreme values? .question[ Why might simply using "time" as a variable in a linear regression model be a bad idea? ] --- ### A few (hopefully) obvious things **Order matters** - unlike many other datasets, rearranging the order of the observations will lose important information **Independence is violated** - one observation is likely related to "neighboring" ones --- ### The data again: *non-stationary* <img src="time-series_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- ### Lag 1, lag 2, ... <img src="time-series_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- ### Lag 1, lag 2, ... <img src="time-series_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- ### Lag 1, lag 2, ... <img src="time-series_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> --- ### ACF plots <img src="time-series_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> --- ### PACF plots <img src="time-series_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> --- ### An AR(p) model... An .vocab[autoregressive] model of order `\(p\)` is given by the following expression: `\begin{align*} y_t = \mu + \sum_{i = 1}^p \phi_i y_{t - i} + \epsilon_t \end{align*}` .question[ What parameters are being estimated, and what are the "predictors" being used? ] .small[(in an actual class, we'd probably have spent a lot of time discussing what a .vocab[stationary] process is beyond the brief definition provided today!)] --- ### ...vs an MA(q) model A .vocab[moving average] model of order `\(q\)` is given by the following expression: `\begin{align*} y_t = \mu + \epsilon_t + \sum_{i = 1}^q \theta_i \epsilon_{t - i} \end{align*}` It has a confusing name - do not confuse this type of model with *smoothing* using a moving average of observations! .question[ What parameters are being estimated, and what are the "predictors" being used? How does this differ from an AR model? ] --- ### An ARMA(p, q) model We can combine the two into an ARMA(p, q) model, which consists of an autoregressive component of order p and a moving average component of order q: `\begin{align*} y_t = \mu + \epsilon_t + \sum_{i = 1}^p \phi_i y_{t - i} + \sum_{i = 1}^q \theta_i \epsilon_{t - i} \end{align*}` .question[ A question we've ignored until now: how might you choose `\(p\)` and `\(q\)`? ] --- ### Differencing ``` r dat %>% diff() %>% ggtsdisplay() ``` <img src="time-series_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> --- ### ...second order differencing ``` r dat %>% diff(differences = 2) %>% # a function composition ggtsdisplay() ``` <img src="time-series_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> --- ### An ARIMA (p, d, q) model ARMA models assume that the data come from a stationary process; an ARIMA model (the I stands for .vocab[integrated]) extends this by allowing for removing of trends by differencing (such that the resulting data are stationary). (There are also seasonal differencing approaches to remove periodic variation that we won't talk about) --- ### Model selection .center[Way beyond the scope of this course.] <img src="img/mathlady.jpg" width="70%" style="display: block; margin: auto;" /> --- ### Model selection ``` r # Note Arima() function with capital A from `forecast` m1 <- Arima(dat, order = c(3, 1, 1)) m1 ``` ``` ## Series: dat ## ARIMA(3,1,1) ## ## Coefficients: ## ar1 ar2 ar3 ma1 ## 0.4810 -0.0090 -0.0884 -0.8945 ## s.e. 0.0339 0.0339 0.0319 0.0166 ## ## sigma^2 = 471: log likelihood = -4922.06 ## AIC=9854.12 AICc=9854.18 BIC=9879.12 ``` --- ### Model selection ``` r checkresiduals(m1) ``` <img src="time-series_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> ``` ## ## Ljung-Box test ## ## data: Residuals from ARIMA(3,1,1) ## Q* = 226.57, df = 215, p-value = 0.2808 ## ## Model df: 4. Total lags used: 219 ```