### Statistics 6871 - Time Series

### Lab Two

### More Time Series Data, Nonstationarity and the CCF.

###########################################################################################

# Johnson and Johnson Quarterly Earnings (4th Quarter, 1970 to 1st Quarter, 1980)

# Plot the data.

par(mfcol=c(2,1))

JJ <- rts(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_s\\jj.txt"))

ts.plot(JJ, main="Johnson and Johnson Quarterly Earnings", ylab="(thousands)")

JJ.log <- rts(log(JJ))

ts.plot(JJ.log, main="Log Johnson and Johnson Quarterly Earnings", ylab="log")

par(mfcol=c(2,2))

acf(JJ.log)

lag.plot(JJ.log, lags=16, layout=c(4,4))

###########################################################################################

# Wholesale U.S. gas and oil prices over 180 months (July, 1973 to December, 1987

# How do oil prices influence changes in gas prices?

# Plot the data.

par(mfcol=c(2,2))

OilGas <- matrix(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_s\\oil.txt"), ncol=2, byrow=F)

Oil <- rts(OilGas[,1])

Gas <- rts(OilGas[,2])

ts.plot(Oil, main="Oil")

ts.plot(Gas, main="Gas")

ts.plot(Oil, Gas, lty = 1, main="Oil and Gas")

Gas.lag <- lag(Gas, 2)

ts.plot(Oil, Gas.lag, lty=1, main="Oil and Gas.lag2")

#acf(ts.union(Oil,Gas))

# Difference the data to try and achieve stationarity.

# Note that the first difference produces the price changes.

Oil.diff <- diff(Oil, 1)

Gas.diff <- diff(Gas, 1)

ts.plot(Oil.diff, main="Oil.diff")

ts.plot(Gas.diff, main="Gas.diff")

# Suggest a transformation that might stabalize the Oil and Gas time series.

ts.plot(Oil.diff, Gas.diff, lty = 1, main="Oil.diff and Gas.diff")

acf(ts.union(Oil.diff,Gas.diff))

lag.plot(Oil.diff, lags=16, layout=c(4,4))

lag.plot(Gas.diff, lags=16, layout=c(4,4))

###########################################################################################

# Systolic and diastolic blood presure readings for a mild hypertensive.

par(mfcol=c(3,1))

SysDia <- matrix(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_s\\Blood.txt"), ncol=2, byrow=T)

Systolic <- rts(SysDia[,1])

Diastolic <- rts(SysDia[,2])

ts.plot(Systolic, main="Systolic")

ts.plot(Diastolic, main="Diastolic")

ts.plot(Systolic, Diastolic, lty = 1, main="Oil and Gas")

par(mfcol=c(1,1))

acf(ts.union(Systolic,Diastolic))

lag.plot(Systolic, lags=16, layout=c(4,4))

lag.plot(Diastolic, lags=16, layout=c(4,4))

###########################################################################################

# Sunspots.

DateSunSpots <- matrix(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_e\\Wsunspots.txt"), ncol=2, byrow=T)

SunSpots <- rts(DateSunSpots[,2], start=1749, frequency=12, units="years")

ts.plot(SunSpots, main="Wolfer's Sunspot Data")

###########################################################################################

# The Dow-Jones Index and Australian All-ordinaries Index.

par(mfcol=c(3,1))

DJAO <- matrix(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\Djao2.txt"), ncol=2, byrow=T)

DJ <- rts(DJAO[,1])

AO <- rts(DJAO[,2])

ts.plot(DJ, main="DJ")

ts.plot(AO, main="AO")

ts.plot(DJ, AO, lty = 1, main="DJ and AO")

par(mfcol=c(1,1))

acf(ts.union(DJ,AO))

# Re-express the data as percntage relative price changes. (See pp. 219-220)

par(mfcol=c(3,1))

DJAOPC <- matrix(scan("I:\\Courswrk\\Stat\\esuess\\Stat6871\\Data_bd\\DjaoPC2.txt"), ncol=2, byrow=T)

DJPC <- rts(DJAOPC[,1])

AOPC <- rts(DJAOPC[,2])

ts.plot(DJPC, main="DJPC")

ts.plot(AOPC, main="AOPC")

ts.plot(DJPC, AOPC, lty = 1, main="DJPC and AOPC")

par(mfcol=c(1,1))

acf(ts.union(DJPC,AOPC))

###########################################################################################

# Simulated data example: AR(1)

par(mfcol=c(2,2))

ar.sim.pos <- rts(arima.sim(list(order=c(1,0,0), ar = 0.7), n=200))

ts.plot(ar.sim.pos, main="AR(1) phi(1) = 0.7")

acf(ar.sim.pos)

ar.sim.neg <- rts(arima.sim(list(order=c(1,0,0), ar = -0.7), n=200))

ts.plot(ar.sim.neg, main="AR(1) phi(1) = -0.7")

acf(ar.sim.neg)

###########################################################################################

# Simulated data example: MA(1)

# Note: Splus define the Moving Average model as X_t = Z_t - theta*Z_{t-1}

# The difference is that there is a minus sign in their definition.

par(mfcol=c(2,2))

ma.sim.pos <- rts(arima.sim(list(order=c(0,0,1), ma = -0.5), n=200))

ts.plot(ma.sim.pos, main="MA(1) theta(1) = 0.5")

acf(ma.sim.pos)

ma.sim.neg <- rts(arima.sim(list(order=c(0,0,1), ma = 0.5), n=200))

ts.plot(ma.sim.neg, main="MA(1) theta(1) = -0.5")

acf(ma.sim.neg)

###########################################################################################

# Simulated random walk.

par(mfcol=c(2,3))

z <- rts(rnorm(200, 0, 1))

ts.plot(z, main="white noise")

acf(z)

s <- rts(cumsum(z))

ts.plot(s, main="random walk")

acf(s)

s.diff <- diff(s, 1)

ts.plot(s.diff, main="diff1 random walk")

#p <- locator()

#p

acf(s.diff)