Ref

  • Robert H. Shumway, David S. Stoffer, Time series analysis and its application with R examples. 4th edition.

1 Some time series example

library(astsa)

1.1 Practical data

判斷以下範例為何不是 (weakly) stationary

Example 1.1 (p.2)

plot(jj, type="o", ylab="Quarterly Earnings per Share")





Example 1.2 (p.3)

plot(globtemp, type="o", ylab="Global Temperature Deviations")





Example 1.3 (p.3) Voice record

plot(speech)





Example 1.5 (p.5)

par(mfrow = c(2,1)) # set up the graphics
plot(soi, ylab="", xlab="", main="Southern Oscillation Index\nindex of ocean ccurrent 洋流")
plot(rec, ylab="", xlab="", main="Recruitment\nindex of fish amount")





Example 1.6 (p.5) MRI data

par(mfrow=c(2,1))
ts.plot(fmri1[,2:5], col=1:4, ylab="BOLD", main="Cortex")
ts.plot(fmri1[,6:9], col=1:4, ylab="BOLD", main="Thalamus & Cerebellum")
mtext("Time (1 pt = 2 sec)",side = 1,line = 2)





Example 1.7 (p.6)

par(mfrow=c(2,1))
plot(EQ5, main="Earthquake")
plot(EXP6, main="Explosion")





1.2 White Noise (WN)

Example 1.9 (p.10)

set.seed(0921)
w = rnorm(500,0,1) # 500 N(0,1) variates
v = filter(w, sides=2, filter=rep(1/3,3)) # moving average
par(mfrow=c(2,1))
plot.ts(w, main="white noise")
plot.ts(v, ylim=c(-3,3), main="moving average")

\[W_t\sim N(0,1)\]

  • sides=2, center at lag 0

\[V_t=\frac1 3(W_{t-1}+W_t+W_{t+1})\]

  • sides=1, for past value only
\[V_t=\frac1 3(W_t+W_{t-1}+W_{t-2})\]

Example 1.10 (p.11)

set.seed(0921)
w = rnorm(550,0,1) # 50 extra to avoid startup problems
x = filter(w, filter=c(1,-.9), method="recursive")[-(1:50)] # remove first 50
plot.ts(x, main="autoregression")

\[W_t\sim N(0,1)\] \(X_t=W_t-0.9W_{t-1}\) (default sides=1)

\(X_t\) and \(X_{t+1}\) are negatively correlated, means when
  • \(X_t\uparrow\:\Rightarrow X_{t+1}\downarrow\)
  • \(X_t\downarrow\:\Rightarrow X_{t+1}\uparrow\)

1.3 Random Walk

Example 1.11 (p.11)

set.seed(154) # so you can reproduce the results
w = rnorm(200,0,1); x = cumsum(w) # two commands in one line
wd = w +.2; xd = cumsum(wd)
plot.ts(xd, ylim=c(-5,55), main="random walk")
lines(x, col=2)
lines(.2*1:200,lty="dashed")
legend('topleft', col=c(1,2,1), lty=c(1,1,2), 
       legend = c(expression(X[d]),expression(X[t]),expression(E(X[t]))))

\(X_t\) is not stationary, \(\because Var(X_t)\) depends on t.

\(X_d\) is not stationary, \(\because E(X_d)\) depends on t.

  • \(W_t\sim N(0,1)\)
  • \(X_t=W_1+W_2+...+W_t\)
    • \(Var(X_t)=Var(W_1)+Var(W_2)+...+Var(W_t)=1\cdot t=t\)
  • \(W_d=W_t+0.2\)
  • \(X_d=\sum W_d=\sum W_t+0.2\cdot t\)
    • \(E(X_d)=0.2\cdot t\)
\(E(aX+b)=aE(X)+b\)
\(Var(aX+b)=a^2Var(X)\)

1.4 Noise influence periodic

Example 1.12 (p.12)

cs = 2*cos(2*pi*1:500/50 + .6*pi); w = rnorm(500,0,1)
par(mfrow=c(3,1), mar=c(3,2,2,1), cex.main=1.5)         # help(par) for info
plot.ts(cs, main=expression(x[t]==2*cos(2*pi*t/50+.6*pi)))
plot.ts(cs+w, main=expression(x[t]==2*cos(2*pi*t/50+.6*pi) + N(0,1)))
plot.ts(cs+5*w, main=expression(x[t]==2*cos(2*pi*t/50+.6*pi) + N(0,25)))

The periodic behavior became less obvious when the noise is large.


1.5 How lag influence plot

Example 1.24 (p.24) \[y_t=Ax_{t-l}+w_t\]

  • \(l>0\), \(x_t\) lead \(y_t\)
    • \(y_t=x_{t-1}+w_t\)
    • \(x\) lead \(y\)
  • \(l<0\), \(x_t\) lag \(y_t\)
    • \(y_t=x_{t+1}+w_t\)
    • \(x\) lag \(y\)

Assume \(w_t\) is uncorrelated with \(x_t\), the cross-covariance function is \[\gamma_{yx}(h)=cov(y_{t+h},x_t)=cov(Ax_{t+h-l}+w_{t+h},x_t)\\=cov(Ax_{t+h-l},x_t)=A\gamma_x(h-l)\]

When \(h=l\), \(\gamma_{yx}(h)=\gamma_x(0)\)

x = rnorm(100)
y = lag(x, -5) + rnorm(100)
par(mfrow=c(1,1))
ccf <- ccf(y, x, type='covariance', 
           ylab='CCovF', xlab='lag h', main=expression(paste('l=5 with', hat(gamma)[yx](h))))

  • \(X\sim N(0,1)\)
  • \(y_t=X_{t-5}+W_t, W_t\sim N(0,1)\)
  • \(\require{enclose} \enclose{horizontalstrike}{Corr(y_t, X_{t+h})=\big\{\substack{1,\:h=5\\0,\:o.w.}}\)
  • \(l=5\to\gamma_{yx}(h)=\gamma_x(h-5)\)
    • \(lag\:h=5 \to\gamma_{yx}(5)=\gamma_x(0)=variance(X)=1\)
    • \(y_{t+h}=Ax_{t+h-5}+w_{t+h}\)
      • \(i=|h-l|=|h-5|\)
      • \(h>5\to\gamma_{yx}(h)=cov(Ax_{t+i},x_t)\to x\:leads\)
      • \(h<5\to\gamma_{yx}(h)=cov(Ax_{t-i},x_t)\to y\:leads\)

Example 1.25 (p.28)

par(mfrow=c(1,2))
r = round(acf(soi, 6, plot=FALSE)$acf[-1], 3)
plot(lag(soi,-1), soi, ylab=expression(X[t]), xlab=expression(X[t-1]))
arrows(-1,-1,1,1, col=2, angle = 15, lwd=5)
legend('topleft', legend=r[1])
plot(lag(soi,-6), soi, ylab=expression(X[t]), xlab=expression(X[t-6]))
arrows(-1,1,1,-1, col=2, angle = 15, lwd=5)
legend('topleft', legend=r[6])