Search This Blog

Wednesday, April 18, 2012

R: FORECASTING AND TIME SERIES # 5




# Example 5.2.
# Random walk and WN (difference) processes with ACFs
# Page 115
wn <- rnorm(150,0,1)
rw <- wn*0
for(i in 1:length(wn)){rw[i]<-sum(wn[1:i])}
par(mfrow=c(2,2))
plot(rw,ylab="",xlab="Time",type="o")
acf(rw,main="Sample ACF")
plot(diff(rw),ylab="",xlab="Time",type="o")
acf(diff(rw),main="Sample ACF")

# Example 5.2.
# Ventilation data and first differences
# Page 117
ventilation <- ts(read.table(file = "C:\\Users\\Tebbs\\My Documents\\texfiles\\Classes\\USC\\stat520\\f11\\data\\ventilation.txt"))
plot(ventilation,ylab="Ventilation (L/min)",xlab="Observation time",type="o")
acf(ventilation,main="Sample ACF")
plot(diff(ventilation),ylab="First differences",xlab="Time",type="o")
acf(diff(ventilation),main="Sample ACF: 1st differences")

# Example 5.3.
# ARI(1,1) simulation
# Page 120
par(mfrow=c(2,2))
ari11.sim <- arima.sim(list(order = c(1,1,0), ar = c(0.7)), n = 150)
plot(ari11.sim,ylab=expression(Y[t]),xlab="Time",type="o")
acf(ari11.sim,main="Sample ACF: ARI(1,1)")
plot(diff(ari11.sim),ylab=expression(Y[t]-Y[t-1]),xlab="Time",type="o")
acf(diff(ari11.sim),main="Sample ACF: 1st differences")

# Example 5.3.
# IMA(1,1) simulation
# Page 122
par(mfrow=c(2,2))
ima11.sim <- arima.sim(list(order = c(0,1,1), ma = c(-0.5)), n = 150)
plot(ima11.sim,ylab=expression(Y[t]),xlab="Time",type="o")
acf(ima11.sim,main="Sample ACF: IMA(1,1)")
plot(diff(ima11.sim),ylab=expression(Y[t]-Y[t-1]),xlab="Time",type="o")
acf(diff(ima11.sim),main="Sample ACF: 1st differences")

# Figure 5.5.
# IMA(2,2) simulation
# Page 124
par(mfrow=c(3,2))
ima22.sim <- arima.sim(list(order = c(0,2,2), ma = c(-0.3,0.3)), n=150)
plot(ima22.sim,ylab=expression(Y[t]),xlab="Time",type="o")
acf(ima22.sim,main="Sample ACF: IMA(2,2)")
plot(diff(ima22.sim),ylab="First differences",xlab="Time",type="o")
acf(diff(ima22.sim),main="Sample ACF: 1st differences")
plot(diff(diff(ima22.sim)),ylab="2nd differences",xlab="Time",type="o")
acf(diff(diff(ima22.sim)),main="Sample ACF: 2nd differences")

# Figure 5.6.
# ARIMA(1,1,1) simulation
# Page 126
par(mfrow=c(2,2))
arima111.sim <- arima.sim(list(order = c(1,1,1), ar = c(0.5), ma = c(0.5)), n = 150)
plot(arima111.sim,ylab=expression(Y[t]),xlab="Time",type="o")
acf(arima111.sim,main="Sample ACF: ARIMA(1,1,1)")
plot(diff(arima111.sim),ylab="First differences",xlab="Time",type="o")
acf(diff(arima111.sim),main="Sample ACF: 1st differences")

# Example 5.4.
# Electricity data
# Page 130
data(electricity)
plot(electricity,ylab="Electricity usage",xlab="Time",type="o")
# Page 133
# Profile log-likelihood of lambda (for Box Cox transformation)
# This can be computationally intense (time consuming)
BoxCox.ar(electricity)

# Example 5.4.
# Electricity data
# Time series plot of the log-transformed data
# Page 134
data(electricity)
plot(log(electricity),ylab="(Log) electricity usage",xlab="Time",type="o")

# Example 5.4.
# Electricity data
# First differences of log-transformed data and sample ACF
# Plots were constructed separately
# Page 135
data(electricity)
plot(diff(log(electricity)),ylab="First differences of log(Electricity)",xlab="Time",type="o")
acf(as.vector(diff(log(electricity))),main="",ylab="ACF of the 1st differences of the logged series",lag.max=36)

No comments:

Post a Comment

Thank you