library(astsa)
MATH4341: Spatio-temporal statistics
Term 2: Temporal modelling and time series analysis
Computer Lab 1: Introduction to time series
These labs will all make use of R. Load up RStudio via your favourite method. If you can’t use RStudio for some reason, most of what we do in these labs can probably be done using WebR, but the instructions will assume RStudio. You will also want to have the web version of the lecture notes to refer to.
Question 1
Make sure that you have the astsa
package installed (install.packages("astsa")
), then go through Chapter 1 of the lecture notes, copy-and-pasting each block of code one-at-a-time into RStudio, and executing each in turn to make sure that you can reproduce all of the plots and computations from the notes. Note the “Copy to clipboard” button that appears when you hover your mouse over the grey code blocks.
The code blocks for each Chapter of the notes can be executed in this way (from top to bottom) to allow straightforward reproduction of all examples in the notes.
Question 2
This question is concerned with analysing the astsa::cardox
time series. Look at the documentation for this data set before proceeding.
- Deseasonalise the time series using an appropriate convolutional linear filter.
- Plot the deseasonalised time series.
- Estimate the monthly seasonal effects.
Question 3
- Referring to section 2.4.2.1 if necessary, simulate 1,000 observations from a (centred) AR(2) process with \phi_1=1.5, \phi_2=-0.75, \sigma=1, initialised at 0.
- Compute the empirical mean and variance of the simulated process.
- Using the function
acf
, or otherwise, compute the first few auto-correlations of the process (the correlation between the time series and itself, offset by different lags). - Reverse the time series, so that the first observation is now last and the last observation is now first.
- Confirm that the empirical mean and variance of the reversed time series has not changed.
- Compute the first few auto-correlations of the reversed time series. What do you notice? What does this mean?
Question 4
- Referring to section 2.3.2.2 if necessary, simulate 10,000 observations from a (centred) VAR(1) process with
\mathsf{\Phi} = \begin{pmatrix}0.9&0.2\\-0.1&0.8\end{pmatrix},\ \mathsf{\Sigma}=\mathbb{I}_2,\ \mathbf{x}_0 = \binom{0}{0}.
You could do this with a
for
loop, or using the code below.
= matrix(c(0.9, 0.2, -0.1, 0.8), ncol=2, byrow=TRUE)
phi = Reduce(function(xi, z) phi %*% xi + rnorm(2),
xList rep(0, 9999), c(0, 0), acc=TRUE)
= t(sapply(xList, cbind))
x dim(x)
[1] 10000 2
Don’t worry about exactly how this code works yet - it will be explained later in the course.
- By computing the eigenvalues of \mathsf{\Phi}, confirm that the process is stable.
- Compute the empirical covariance matrix for the simulated process. Then, using
netcontrol::dlyap
, or otherwise, compare it to its theoretical value. Note that there is a bug in (the documentation for) thedlyap
function which means you may have to unexpectedly transpose thephi
matrix. - Using
ccf
or otherwise, compute a few empirical cross-correlations between the two components of the simulated time series. Compare these to their theoretical values, as suggested by the stationary covariance function. - Reverse the time series.
- Confirm that the empirical variance matrix has not changed.
- Investigate the cross-correlations between the components of the reversed series. What do you notice? What does this mean?