\documentclass[notitlepage]{article}
\usepackage{../../Math545}
\usepackage{listings}
\usepackage{numprint}
\usepackage{enumerate}
\usepackage{bbm}
\usepackage{bm}

\usepackage{amsfonts,amsmath,amssymb}

\newtheorem{alg}{Algorithm}
\newtheorem{ex}{Example}
\newtheorem{note}{Note}
\newenvironment{proof}{\par\noindent{\normalfont{\bf{Proof
}}}}
{\hfill \small$\blacksquare$\\[2mm]}


\setlength{\parindent}{0in}

\def\E{\mathbb{E}}
\def\Var{\text{Var}}
\def\Cov{\text{Cov}}
\def\Corr{\text{Corr}}

\def\bW{\mathbf{W}}

\newcommand{\bs}[1]{\bm{#1}}
\newcommand{\Rho}{\mathrm{P}}
\newcommand{\cif}{\text{ if }}
\newcommand{\Ra}{\Longrightarrow}
\newcommand{\ra}{\longrightarrow}

\def\bphi{\boldsymbol \phi}


\lstloadlanguages{R}

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

\lstdefinestyle{Rsettings}{
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp}, % Write as many keywords
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

\lstset{basicstyle=\ttfamily, numbers=none, literate={~} {$\sim$}{2}}

\begin{document}

<<setup, cache=FALSE, echo=FALSE>>=
library(knitr)
# global chunk options
opts_chunk$set(cache=TRUE, autodep=TRUE)
options(scipen=999)
@
\begin{center}
{\textsclarge{Selecting a Time Series Model}}
\end{center}

We consider a systematic approach to analyzing time series data using the following example.   These data record the relative abundance of isotope oxygen-18 to isotope oxygen-16 over a period of over 2.5 million years (West \& Harrison, \textit{Bayesian Forecasting \& Dynamic Models}, 1997, chapter 15).  There are 866 observations, with measurements taken 3000 years apart.  The data can be read in and plotted as follows.  The time axis represents years (in thousands) before present day.
<<Sel1,comment='+', fig.width=7.25, fig.height=4.0, fig.align='center'>>=
O2.data<-read.table('O2.txt',header=T)
O2.data$Time<--O2.data$Time
par(mar=c(4,4,1,0))
plot(O2.data,pch=19,cex=0.5,xlab='Time (thousands of years)');lines(O2.data)
(n<-nrow(O2.data))
@
There is some apparent evidence of a trend (possibly linear), and also an increase in variability, as we approach present day.  To account for the inrease in variability, we take logs of the data.
<<Sel2,comment='+', fig.width=7.25, fig.height=4.5, fig.align='center'>>=
Time<-O2.data$Time
X<-log(O2.data$Ratio)
round(coef(summary(lm(X~Time))),6)
@
Ignoring the trend for the moment, we may plot the acf (first we reverse the order of the data in the data set so that observations near the present day appear last in the sequence):
<<Sel3,comment='+', fig.width=7.25, fig.height=4.0, fig.align='center'>>=
X0<-rev(log(O2.data$Ratio))
acf(X0,lag.max=100,main=expression(paste(O[2],' isotope ratio acf (log scale)')))
@
After de-trending the logged sequence, we may re-plot and re-compute the acf.
<<Sel4,comment='+', fig.width=7.25, fig.height=4.0, fig.align='center'>>=
par(mar=c(4,4,1,0))
X0.res<-residuals(lm(X~Time))
plot(Time,X0.res,pch=19,cex=0.5,ylim=range(-0.3,0.3));lines(Time,X0.res)
abline(h=0,lty=2)
X1<-rev(X0.res)
par(mar=c(4,4,4,0))
acf(X1,lag.max=100,main=expression(paste(O[2],' isotope ratio residuals acf ')))
abline(v=c(1:13)*10,lty=2,col='gray')
@
This plot indicates \textit{periodic} (or \textit{seasonal}) behaviour, and also reasonably strong and positive short range dependence

\medskip
As an alternative method of analysis, we might take first differences to remove the linear trend:
<<Sel5,comment='+', fig.width=7.25, fig.height=4.0, fig.align='center'>>=
par(mar=c(4,4,1,0))
X0.diff<-diff(log(O2.data$Ratio))
plot(Time[-1],X0.diff,pch=19,cex=0.5,ylim=range(-0.3,0.3));lines(Time[-1],X0.diff)
abline(h=0,lty=2)
X2<-rev(X0.diff)
par(mar=c(4,4,4,0))
acf(X2,lag.max=100,main=expression(paste(O[2],' isotope ratio first differences acf ')))
@
This plot indicates that there is again seasonality, but also possibly that there are occasional outlying observations.
<<Sel6,comment='+', fig.width=7.25, fig.height=4.0, fig.align='center'>>=
acf(diff(X1,lag=16),lag.max=100)
@

\end{document}
