### Statistics 6871 - Time Series
### Lab One
### Introduction to Splus and Time Series data.
###########################################################################################
# Wine Data: We use the Wine Data to examine trends and seasonal
patterns in time series data.
# First we plot the data.
par(mfcol=c(3,2)) # The par command lets us divide the graph sheet
up into 3 rows and 2 columns.
# The rts command tells splus that what is inside the () is regular
time series data.
# The scan command reads data from a file.
# The ts.plot command plots time series data.
Wine <-
rts(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\Wine.txt"), start=1980,
frequency=12, units="months")
ts.plot(Wine, main="The Austrailian Red Wine Sales",
ylab="(thousands)")
# Plot the data again against the index.
Wine <-
rts(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\Wine.txt"))
ts.plot(Wine, main="The Austrailian Red Wine Sales",
ylab="(thousands)")
# To plot the autocorrelation function for the Wine data we use the
acf function.
acf(Wine)
# Note that the Wine data seems to have increasing variance in
time. We apply the log to
# the data to stabilize the variance. Note that the acf looks the
same.
Wine.log <- rts(log(Wine))
ts.plot(Wine.log, main="Log Red Wine Sales",
ylab="log(thousands)",ylim=c(6,8))
acf(Wine.log)
# Note that the Wine data has an increasing trend. To remove the
trend we fit a line to
# the data using the lm (linear model) command.
# fit a line
y <- Wine.log
x <- 1:length(Wine.log)
fit <- lm(y ~ x)
# Plot the data and the line.
ts.plot(Wine.log, main="Log Red Wine Sales",
ylab="log(thousands)", ylim=c(6,8))
par(new=T)
y.hat <- rts(fit$fitted)
ts.plot(y.hat, ylim=c(6,8))
# Next we plot the detrended data, which are the residuals of the
line fit.
par(mfcol=c(3,2))
Wine.log.detrend <- rts(fit$resid)
ts.plot(Wine.log.detrend, main="Detrended Log Austrailian Red
Wine Sales")
# Note that the acf shows a 12 month seasonal pattern.
acf(Wine.log.detrend, 40)
# To remove the seasonal pattern we difference the data with a lag
of 12. Note that the seasonal
# pattern is gone and that there is not pattern in the acf now.
Wine.log.detrend.diff12 <- diff(Wine.log.detrend, 12)
ts.plot(Wine.log.detrend.diff12, main="Differenced 12
Detrended Log Austrailian Red Wine Sales")
acf(Wine.log.detrend.diff12,40)
###########################################################################################
# Accidental Deaths: What patterns do you see in the Accidental
Deaths data?
par(mfcol=c(3,2))
Deaths <-
rts(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\Deaths.txt"),start=1973,frequency=12,units="months")
ts.plot(Deaths, main="Accidental Deaths USA",
ylab="(thousands)")
Deaths <-
rts(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\Deaths.txt"))
ts.plot(Deaths, main="Accidental Deaths USA",
ylab="(thousands)")
acf(Deaths)
Deaths.diff <- diff(Deaths, 12)
tsplot(Deaths.diff)
acf(Deaths.diff)
###########################################################################################
# Lake Huron: What patterns do you see in the Lake Huron data?
par(mfcol=c(3,2))
Lake <-
rts(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\Lake.txt"),start=1975,frequency=12,units="months")
tspar(Lake)
ts.plot(Lake, main="Level of Lake Huron")
Lake <-
rts(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\Lake.txt"))
tspar(Lake)
ts.plot(Lake, main="Level of Lake Huron")
# fit a line
y <- Lake
x <- 1:length(Lake)
fit <- lm(y ~ x)
ts.plot(Lake, main="Level of Lake Huron", ylim=c(6,12))
par(new=T)
y.hat <- rts(fit$fitted)
ts.plot(y.hat, ylim=c(6,12))
Lake.detrend <- rts(fit$resid)
ts.plot(Lake.detrend, main="Detrended Level of Lake
Huron")
acf(Lake.detrend)