R-help to extra exercise on time series
# Read the data and inform R that (the log-transformed) pasenger numbers are time series data with monthly observations:
airline<-matrix(scan("http://www.math.uio.no/avdc/kurs/STK4900/data/airlines"), byrow=T)
airlinets<-ts(log(airline), start=1947, frequency=12)
# Before we look at the questions in the exercise, we will make some plots to get an overview of the data:
# Plot of the data (cf page 106 in BS)
ts.plot(airlinets,ylab="log(passenger)")
# Decompose the time series in trend, sesonal variation and residuals and plot the components:
# (It is beyond the scope of our presentation to describe how this is done. See help(stl) for details.)
airlinets.stl<-stl(airlinets[,1],"periodic")
plot(airlinets.stl)
# Then we turn to the questions in the exercise
# QUESTOINS a&b)
# Define dummy variables for each of the months:
m1<-c(1,rep(0,11))
m2<-c(0,1,rep(0,10))
m3<-c(rep(0,2),1,rep(0,9))
m4<-c(rep(0,3),1,rep(0,8))
m5<-c(rep(0,4),1,rep(0,7))
m6<-c(rep(0,5),1,rep(0,6))
m7<-c(rep(0,6),1,rep(0,5))
m8<-c(rep(0,7),1,rep(0,4))
m9<-c(rep(0,8),1,rep(0,3))
m10<-c(rep(0,9),1,rep(0,2))
m11<-c(rep(0,10),1,0)
m12<-c(rep(0,11),1)
m1<-rep(m1,12)
m2<-rep(m2,12)
m3<-rep(m3,12)
m4<-rep(m4,12)
m5<-rep(m5,12)
m6<-rep(m6,12)
m7<-rep(m7,12)
m8<-rep(m8,12)
m9<-rep(m9,12)
m10<-rep(m10,12)
m11<-rep(m11,12)
m12<-rep(m12,12)
# Check that these commands generate dummy variables as described in the exercise.
# Define a variable for time (i.e. a number for the months)
time<-seq(1,144)
# Fit a linear model without intercept and inspect the results:
mod.a<-lm(log(airline)~m1+m2+m3+m4+m5+m6+m7+m8+m9+m10+m11+m12+time-1)
summary(mod.a)
# Interpret the fitted model and explain why we do not include an intercept term.
# Comment:
# A simpler way to fit the model is by the commands:
month<-rep(1:12,12)
mod.a2<-lm(log(airline)~factor(month)+time-1)
summary(mod.a2)
# Check that these commands give the same results as above and explain why they do so.
# QUESTION c)
# Define centred dummy variables:
mm1<-m1-1/12
mm2<-m2-1/12
mm3<-m3-1/12
mm4<-m4-1/12
mm5<-m5-1/12
mm6<-m6-1/12
mm7<-m7-1/12
mm8<-m8-1/12
mm9<-m9-1/12
mm10<-m10-1/12
mm11<-m11-1/12
# Check that these commands define centred dummy variables as described in the exercise
# Fit a linear model and inspect the results:
mod.c<-lm(log(airline)~mm1+mm2+mm3+mm4+mm5+mm6+mm7+mm8+mm9+mm10+mm11+time)
summary(mod.c)
# Interpret the fitted model.
# Remember to answer the questions in the exercise!
# QUESTION d)
# Plot (the log-transformed) observations and the corresponding fitted values
plot(time, log(airline))
lines(time, fitted(mod.c))
# What does the plot tell you?
# QUESTION e)
# Plot the autocorrelation of the residuals:
acf(ts(resid(mod.c)))
# What does the plot tell you?