Shumay and Stoffer Time series analysis with R adapted by Macgilchrist examples 02.R
Shumway examples
#White noise and simple moving average
#White noise and Autoregressive processes
#White noise and Double Autoregressive processes
Cosine wave with period 50 points (top panel) compared with the cosine wave contaminated with additive white Gaussian noise, w = 1 (middle panel) and w = 5 (bottom panel).
If you have trouble accessing the R package that goes with the Shumway and Stoffer text, you can access it via this link to tsa3.rda http://www-stat.wharton.upenn.edu/~stine/stat910/tsa3.rda. (Download the file rather than open it.)
#Detrending and Differencing a series To detrend in the series in R, use the following commands. We also show how to difference and plot the differenced data; we discuss differencing after this example. In addition, we show how to generate the sample ACFs displayed in Figure 2.5.
Autocorrelation functions of original, detrended and differenced series
Figure 2.11 shows the weekly mortality series discussed in Example 2.2, a moving average (which is essentially a monthly average with k = 2) that helps bring out the seasonal component and a 53-point moving average (which is essentially a yearly average with k = 26) that helps bring out the (negative) trend in cardiovascular mortality. In both cases, the weights, a????k; : : : ; a0; : : : ; ak, we used were all the same, and equal to 1=(2k + 1).9 To reproduce Figure 2.11 in R:
#Kernel smoothing Kernel smoothing is a moving average smoother that uses a weight function, or kernel, to average the observations.
the plot below shows a time plot of two AR(1) processes, one with phi= :9 and one with phi = -0.9; in both cases, sigma(w)^2= 1.
Consider the MA(1) model x(t) = w(t) + theta*w(t-1). Then, E(xt) = 0,
rho(h)=(1+theta^2) x sigma(w)^2 if h=0 rho(h)= theta^2 x sigma(w)^2 if h=1 rho(h)= 0 x sigma(w)^2 if h>1
Note abs(rho(1)) < 1/2 for all values of theta. Also, xt is correlated with x(t-1), but not with x(t-1),x(t-2),x(t-3) etc.???2; This Contrast this with the case of the AR(1) model in which the correlation between x(t) and x(t-k) is never zero. When theta = 0.5, for example, x(t) and x(t-1) are positively correlated, and rho(1) = 0.4. When theta = -0.5, for example, x(t) and x(t-1) are negatively correlated, and rho(1) = -0.4.