Handout 2
Review of Regression
Simple linear regression
Body Fat data: For a random sample of 18 individuals, we have records of measured body fat (Y ,
in percent) and measured dietary fat (X, in percent). Here Y =dependent variable and X=independent
vari

x = 1:5
# Calculate the fast fourier transform of x
fft(x)
# R does not multiply by n^(-1/2) in fft
dft = fft(x)/sqrt(5)
# take the inverse of dft to get back the data
fft(dft, inverse=T)
# because dft was mulitplied by n^(-1/2), so should the inverse
fft

library(astsa)
plot(soi)
?soi
#the spec.pgram will remove a linear trend first before
#calculating the periodogram
# soi is a ts opject with frequency (the period) = 12
# the spec.pgram function will take this into account
# when there is a seasonal compo

library(datasets)
library(tseries)
library(forecast)
x = as.vector(LakeHuron)
n = length(x)
t = 1:n
plot(t,x, type="l")
#the data is clearly not stationary
#this can be verified with the adf.test or kpss.test
adf.test(x)
#not a significant p-value so not

#simulate data from an ARMA(2,3) model
set.seed(20)
x = arima.sim(n=100, model = list(ar = c(0.85, -0.74), ma=c(0.87, 0.43, -0.9)
require(forecast)
#select a model using the H-K algorithm
fit = auto.arima(x,allowmean = F)
#check to see if the residuals fo

require(astsa)
data(cmort)
#use only years with a complete cycle
cmort.part = window(cmort, start=c(1970,1), end=c(1978,52)
ts.plot(cmort.part)
x = as.vector(cmort.part)
n = length(x)
t = 1:n
#remove the trend
trend.fit = lm(x~t)
y.trend = resid(trend.fit

# Remove the last year.
# We will forecast this last year and see how well we did
x = window(AirPassengers, start=c(1949,1), end=c(1959,12)
x = as.vector(x)
n = length(x)
t = 1:n
plot(t,x, type="l", main="Number of Int. Airline Passengers (1949-1959)",
yl

# Remove the last year.
# We will forecast this last year and see how well we did
x = window(AirPassengers, start=c(1949,1), end=c(1959,12)
x = as.vector(x)
n = length(x)
t = 1:n
plot(t,x, type="l", main="Number of Int. Airline Passengers (1949-1959)",
yl

# read data straight from website
www = "http:/staff.elena.aut.ac.nz/Paul-Cowpertwait/ts/global.dat"
temps = scan(www)
#construct data as a time series object
temps = ts(temps, st=c(1856,1), end=c(2005,12), fr=12)
plot(temps)
#subset the data. We only wan

library(astsa)
# for an AR(1) model with phi = 0.9
arma.spec(ar=0.9, log="no")
# What happens when phi is negative?
arma.spec(ar=-0.9, log="no")
x1 = arima.sim(1000, model = list(ar=c(0.9)
x2 = arima.sim(1000, model = list(ar=c(-0.9)
par(mfrow=c(2,1)
ts.p

library(astsa)
par(mfrow=c(2,1)
ts.plot(soi)
y = diff(soi)
ts.plot(y)
spec.pgram(soi, log="no")
spec.pgram(y, log="no")
#monthly data, so do a order 12 moving average
mav.even <- function(x,d=12)cfw_filter(x,c(0.5/d,rep(1/d,(d-1),0.5/d), sides=2)
y = mav.

Handout 10
[Chapters 2.5.2 and 3.3 in Brockwell and Davis].
Some methods for modeling time series in the presence of trend and/or
sesasonality.
We have employed the following methods for modeling a time series fYt g when either trend or seasanality
(or bo

Handout 12
Spectral Analysis
Analysis of time series has often two aspects
a) model tting and forecasting, b) understanding hidden periodicities (spectral analysis).
We have discussed the rst aspect. Now we are about to begin the second. Connection of the

Handout 3
Model Selection
Model building/selection is an indispensable part of any statistical analysis of data. We will briey discuss
a few methods which are useful in regression and time series modeling. We will write these down in the
regression contex

Handout 11
Chapters 2.2, 2.3, 3.1 and 3.3 in Brockwell and Davis.
Important Technical issues:
This handout is devoted to addressing important technical issues such as: stationarity, identiability, nonredundancy of time series models etc. There are many (a

Handout 8
[Chapters 2.1-2.3, 3.1, 3.3 and 5.1.1 in Brockwell and Davis.]
In this handout we will be mostly concerned with autoregressive models. In handout 6 concepts of
AR(1), AR(2), M A(1) and M A(2) models were introduced. We can have autoregressive mo

Handout 9
Partial autocorrelation function (PACF)
Partial autocorrelations play an important role in time series analysis along with autocorrelations (ACF).
The concept of partial autocorrelation is the same as that of partial correlation in regression. W

Handout 7
[Chapter 2.4 in Brockwell and Davis.]
Estimation of mean, autocovariances and autocorrelations
Recall that a stationary process with mean can be described by two properties
a) E(Xt ) = , for all t,
b) Cov(Xt , Xt+j ) is the same for all t (for a

Solution: Sample MIDTERM
1. (a) Note that R2 = 1
SSE=SST O = 0:7839:
About 78:4% percent of the variability in sale price (Y ) can be explained by its regression on area (X1 ),
elevation (X2 ) and slope (X3 ).
Here
M SE = SSE=(n
4) = 5:903=16 = 0:36894;
M

Sample MIDTERM
Statistics 137
1. A land developer was interested in creating a model to use for estimating the selling price of beach
lots on the Oregon coast. To do so, he recorded the following items for each of 20 beach lots recently sold:
Y =sale pric

Exam 2 TH
STA 137 - Patrick
Midterm 2 Take Home
For each part, work may be typed or handwritten, however, it must be neat and organized.
Points will be deducted for those that are not so.
All R code you use must be included. If you are handwriting you exa

Interval Forecasts for MA and AR Models:II
Recall
1. If yt is a covariance stationary process
whose innovations, t, are normally
distributed, then
T h ,T 2 h
y
is an approximate 95-percent forecast
interval for yT+h, where
T h.T E ( yT h yT , yT 1 ,.)
y

Final TH
STA 137 - Patrick
Final Exam Take Home
You are a statistical consultant in which a client has come to you with a data set for which
they want you to analyze.
The data set consists of monthly CO2 levels at Alert, Northwest Territories, Canada. The

a.
la
all
STA137Homework3
Question1.
Weknowthatthetrueautocorre
tioniszerowhenlagisgreater
thanorequalto2,howeverthis
simulationshowssomelagval
uesthatarenotwithin+
1.96/sqrt(100)afterlag2,even
thewayuptolag20.
b.
ForMA(2)theautocorrelationfunctionis
thes

Handout 4
Time series with trend
(From Sections 1.32,1.3.3 and 1.5 in the text)
Consider the temperature data (years 1850-2012) from Handout 1. The temperature series can be described
as
obs = smooth + rough;
where the smooth part is called the trend and

Handout 6
[Chapters 1.4 and 1.6 in Brockwell and Davis.]
You will also nd below plot of temp and loess t (window q = 20, span = :25), plot of the rough
^
^ t against X
^ t+1 and the plot of the autocorrelations obtained from
Xt = Yt m
^ t against time, pl

Handout 9
Partial autocorrelation function (PACF)
Partial autocorrelations play an important role in time series analysis along with autocorrelations (ACF).
The concept of partial autocorrelation is the same as that of partial correlation in regression. W

Handout 8
[Chapters 2.1-2.3, 3.1, 3.3 and 5.1.1 in Brockwell and Davis.]
In this handout we will be mostly concerned with autoregressive models. In handout 6 concepts of
AR(1), AR(2), M A(1) and M A(2) models were introduced. We can have autoregressive mo

Handout 5
Estimation of trend and seasonality.[Chapter 1.5 in Brockwell and Davis]
Consider the data Electricity Sales to the Residential Sector, Jan 1990 - Dec 2012. The data has a
total of n = 276 months of observations. We will denote the electricty sa

Handout 7
[Chapter 2.4 in Brockwell and Davis.]
Estimation of mean, autocovariances and autocorrelations
Recall that a stationary process with mean
a) E(Xt ) =
can be described by two properties
, for all t,
b) Cov(Xt ; Xt+j ) is the same for all t (for a