J James Reade
28/01/2015
\[ \newcommand{\N}[1]{\mathsf{N}\left(#1\right)} \newcommand{\E}[1]{\mathsf{E}\left(#1\right)} \newcommand{\V}[1]{\mathsf{Var}\left(#1\right)} \newcommand{\ols}[1]{\widehat{#1}} \newcommand{\cond}[1]{\left|#1\right.} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\pred}[3]{\widehat{#1}_{#2\left|#3\right.}} \]
library(Quandl)
## Loading required package: xts
library(quantmod)
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
Quandl.auth("y8y3ezt48WZeqUqq1yQd") #this is my authentication code - please sign up at www.quandl.com for your own!
#tourists_AU <- Quandl("EUROSTAT/TOUR_OCC_NIM_1")
tourists_US <- Quandl("BTS/AIRPASS")
tourists_US.t <- ts(tourists_US$Total[order(tourists_US$Date)],start=c(2000,1),freq=12)
vehicle_sales <- Quandl("FRED/TOTALNSA")
vehicle_sales.t <- ts(vehicle_sales$Value[order(vehicle_sales$Date)],start=c(1976,1),freq=12)
UKcc <- Quandl("UKONS/LMS_BCJB_M")
UKcc.eng.t <- ts(UKcc$Value[order(UKcc$Year)],start=c(1983,6),frequency=12)
Alternative representation: Error correction form.
plot(UKcc.eng.t,main="Claimant Count in England",ylab="Claimant Count",xlab="Date")
fit1 <- ses(UKcc.eng.t, alpha=0.2, initial="simple", h=3)
fit2 <- ses(UKcc.eng.t, alpha=0.6, initial="simple", h=3)
fit3 <- ses(UKcc.eng.t, h=3)
plot(fit1, plot.conf=FALSE, ylab="Claimant Count",
xlab="Date", main="", fcol="white", type="o",include=30)
lines(fitted(fit1), col="blue", type="o")
lines(fitted(fit2), col="red", type="o")
lines(fitted(fit3), col="green", type="o")
lines(fit1$mean, col="blue", type="o")
lines(fit2$mean, col="red", type="o")
lines(fit3$mean, col="green", type="o")
legend("bottomleft",lty=1, col=c(1,"blue","red","green"),
c("data", expression(alpha == 0.2), expression(alpha == 0.6),
expression(alpha == 0.9999)),pch=1,bty="n")
plot(vehicle_sales.t,main="US Vehicle Sales",ylab="Sales",xlab="Date")
fit1 <- ses(vehicle_sales.t, alpha=0.2, initial="simple", h=3)
fit2 <- ses(vehicle_sales.t, alpha=0.6, initial="simple", h=3)
fit3 <- ses(vehicle_sales.t, h=3)
plot(fit1, plot.conf=FALSE, ylab="Sales",
xlab="Date", main="", fcol="white", type="o",include=50)
lines(fitted(fit1), col="blue", type="o")
lines(fitted(fit2), col="red", type="o")
lines(fitted(fit3), col="green", type="o")
lines(fit1$mean, col="blue", type="o")
lines(fit2$mean, col="red", type="o")
lines(fit3$mean, col="green", type="o")
legend("bottomleft",lty=1, col=c(1,"blue","red","green"),
c("data", expression(alpha == 0.2), expression(alpha == 0.6),
expression(alpha == 0.5003)),pch=1,bty="n")
plot(tourists_US.t,main="Total Air Passengers in the US",ylab="Total",xlab="Date")
fit1 <- ses(tourists_US.t, alpha=0.2, initial="simple", h=3)
fit2 <- ses(tourists_US.t, alpha=0.6, initial="simple", h=3)
fit3 <- ses(tourists_US.t, h=3)
plot(fit1, plot.conf=FALSE, ylab="Total",
xlab="Date", main="", fcol="white", type="o",include=50)
lines(fitted(fit1), col="blue", type="o")
lines(fitted(fit2), col="red", type="o")
lines(fitted(fit3), col="green", type="o")
lines(fit1$mean, col="blue", type="o")
lines(fit2$mean, col="red", type="o")
lines(fit3$mean, col="green", type="o")
legend("bottomleft",lty=1, col=c(1,"blue","red","green"),
c("data", expression(alpha == 0.2), expression(alpha == 0.6),
expression(alpha == 0.6263)),pch=1,bty="n")
wd <- "/home/readejj/Dropbox/Teaching/Reading/ec313/Resources/Datasets/"
gdp_file <- paste(wd,"gdp_1_2000.csv",sep="")
gdp_all <- read.csv(gdp_file)
uk.gdp.t <- ts(gdp_all$England.GB.UK[19:nrow(gdp_all)],start=1800,frequency=1)
uk.gdp.t0 <- window(uk.gdp.t,end=2008)
uk.gdp.t1 <- window(uk.gdp.t,start=2009)
uk.gdp.t.aan <- ets(uk.gdp.t0,model="AAN")
uk.gdp.t.aan.fit <- forecast(uk.gdp.t.aan,h=5)
plot(window(uk.gdp.t,start=1950),ylim=range(min(window(uk.gdp.t,start=1950)),max(uk.gdp.t.aan.fit$mean)))
lines(uk.gdp.t.aan$fitted,col=2)
lines(uk.gdp.t.aan.fit$mean,col=3)
legend("topleft",lty=1,col=c(1,2,3),legend=c("Actual","Fitted","Forecast"),bty="n")
uk.gdp.t.mmn <- ets(uk.gdp.t0,model="MMN")
uk.gdp.t.mmn.fit <- forecast(uk.gdp.t.mmn,h=5)
plot(window(uk.gdp.t,start=1950),ylim=range(min(window(uk.gdp.t,start=1950)),
max(uk.gdp.t.mmn.fit$mean)),
xlim=range(1950,2015))
lines(uk.gdp.t.mmn$fitted,col=2)
lines(uk.gdp.t.mmn.fit$mean,col=2,lty=2)
lines(uk.gdp.t.aan.fit$mean,col=3)
legend("topleft",lty=c(1,1,2,1),col=c(1,2,2,3),
legend=c("Actual","Fitted","Mult Forecast","Add Forecast"),bty="n")
uk.gdp.t.aand <- ets(uk.gdp.t0,model="AAN",damped=TRUE)
uk.gdp.t.aand.fit <- forecast(uk.gdp.t.aand,h=5)
plot(window(uk.gdp.t,start=1950),ylim=range(min(window(uk.gdp.t,start=1950)),
max(uk.gdp.t.aan.fit$mean)))
lines(uk.gdp.t.aand$fitted,col=2)
lines(uk.gdp.t.aand.fit$mean,col=2,lty=2)
lines(uk.gdp.t.mmn.fit$mean,col=3)
lines(uk.gdp.t.aan.fit$mean,col=4)
legend("topleft",lty=c(1,1,2,1,1),col=c(1,2,2,3,4),
legend=c("Actual","Fitted","Additive Damped Forecast","Multiplicative Forecast","Additive Forecast"),bty="n")
uk.gdp.t.mmnd <- ets(uk.gdp.t0,model="MMN",damped=TRUE)
uk.gdp.t.mmnd.fit <- forecast(uk.gdp.t.mmnd,h=5)
plot(window(uk.gdp.t,start=1950),ylim=range(min(window(uk.gdp.t,start=1950)),
max(uk.gdp.t.mmn.fit$mean)),
xlim=range(1950,2015))
lines(uk.gdp.t.mmnd$fitted,col=2)
lines(uk.gdp.t.mmnd.fit$mean,col=2,lty=2)
lines(uk.gdp.t.aand.fit$mean,col=3)
lines(uk.gdp.t.mmn.fit$mean,col=4)
lines(uk.gdp.t.aan.fit$mean,col=5)
legend("topleft",lty=c(1,1,2,1,1,1),col=c(1,2,2,3,4,5),
legend=c("Actual","Fitted","Multiplicative Damped Forecast",
"Additive Damped Forecast","Multiplicative Forecast","Additive Forecast"),bty="n")
\[ \begin{align} y_{t+h\cond{t}} &= \ell_{t} + hb_{t} + s_{t-m+h_m^+} \\ \ell_{t} &= \alpha(y_{t} - s_{t-m}) + (1 - \alpha)(\ell_{t-1} + b_{t-1})\\ b_{t} &= \beta^*(\ell_{t} - \ell_{t-1}) + (1 - \beta^*)b_{t-1}\\ s_{t} &= \gamma (y_{t}-\ell_{t-1}-b_{t-1}) + (1-\gamma)s_{t-m}, \end{align} \] - Here, \(h_m^+=\lfloor(h-1)\mod~m\rfloor+1\). + Notation \(\lfloor u \rfloor\) means largest integer not greater than \(u\).} + Ensures seasonal component for forecast comes only from final year of sample. - Error correction form: \[ \begin{align} \ell_{t} &= \ell_{t-1} + b_{t-1}+\alpha e_{t}\\ b_{t} &= b_{t-1} + \alpha\beta^*e_{t}\\ s_{t} &= s_{t-m}+\gamma e_t, \end{align} \] - Here, \(e_t=y_{t} - (\ell_{t-1} + b_{t-1}+s_{t-m})=y_{t} - \pred{y}{t}{t-1}\).
tourists_US.t0 <- window(tourists_US.t,end=c(2011,12))
tourists_US.t1 <- window(tourists_US.t,start=c(2012,1))
tourists_US.t.aaa <- ets(tourists_US.t0,model="AAA")
tourists_US.t.aaa.fit <- forecast(tourists_US.t.aaa,h=NROW(tourists_US.t1))
plot(tourists_US.t,pch=2,type="o",main="US Air Passengers")
lines(tourists_US.t.aaa$fitted,col=2,type="o")
lines(tourists_US.t.aaa.fit$mean,col="darkred",lty=2,lwd=2,type="o")
legend("topleft",lty=c(1,1,2),col=c(1,2,"darkred"),legend=c("Actual","ETS(A,A,A)","Forecast"),pch=c(2,1,1),bty="n")
tourists_US.t.mam <- ets(tourists_US.t0,model="MAM")
tourists_US.t.mam.fit <- forecast(tourists_US.t.mam,h=3)
plot(tourists_US.t,type="b",main="Total Passengers in the US")
lines(tourists_US.t.mam$fitted,type="b",col=2,pch=2)
lines(tourists_US.t.mam.fit$mean,type="b",col=2,lty=2,pch=3)
lines(tourists_US.t.aaa$fitted,type="b",col=3,pch=2)
lines(tourists_US.t.aaa.fit$mean,type="b",col=3,lty=2,pch=3)
legend("topleft",lty=c(1,2,2,3,3),col=c(1,2,2,3,3),pch=c(1,2,3,2,3),
legend=c("Actual","ETS(M,A,M)","ETS(M,A,M) Forecast",
"ETS(A,A,A)","ETS(A,A,A) Forecast"))
getSymbols('XTEXVA01JPM664N',src='FRED',return.class="ts")
## As of 0.4-0, 'getSymbols' uses env=parent.frame() and
## auto.assign=TRUE by default.
##
## This behavior will be phased out in 0.5-0 when the call will
## default to use auto.assign=FALSE. getOption("getSymbols.env") and
## getOptions("getSymbols.auto.assign") are now checked for alternate defaults
##
## This message is shown once per session and may be disabled by setting
## options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
## [1] "XTEXVA01JPM664N"
exp.t.ets.mnn.fit <- forecast(ets(XTEXVA01JPM664N,model="MNN"),100)
plot(exp.t.ets.mnn.fit,include=500)
plot(UKcc.eng.t,main="Claimant Count in England",ylab="Claimant Count",xlab="Date")
fit.aaa <- ets(UKcc.eng.t,model="AAA")
plot(fit.aaa)
fit.mmm <- ets(UKcc.eng.t,model="MMM")
plot(fit.mmm)
plot(window(UKcc.eng.t,start=c(2005,1)), ylab="Claimant Count",xlab="Date", main="")
lines(fit.aaa$fitted, col="blue", type="l")
lines(fit.mmm$fitted, col="red", type="l")
legend("bottomleft",lty=1, col=c(1,"blue","red","green"),
c("data", expression(alpha == 0.2), expression(alpha == 0.6),
expression(alpha == 0.9999)),pch=1,bty="n")
plot(vehicle_sales.t,main="US Vehicle Sales",ylab="Sales",xlab="Date")
fit1 <- ses(vehicle_sales.t, alpha=0.2, initial="simple", h=3)
fit2 <- ses(vehicle_sales.t, alpha=0.6, initial="simple", h=3)
fit3 <- ses(vehicle_sales.t, h=3)
plot(fit1, plot.conf=FALSE, ylab="Sales",
xlab="Date", main="", fcol="white", type="o",include=50)
lines(fitted(fit1), col="blue", type="o")
lines(fitted(fit2), col="red", type="o")
lines(fitted(fit3), col="green", type="o")
lines(fit1$mean, col="blue", type="o")
lines(fit2$mean, col="red", type="o")
lines(fit3$mean, col="green", type="o")
legend("bottomleft",lty=1, col=c(1,"blue","red","green"),
c("data", expression(alpha == 0.2), expression(alpha == 0.6),
expression(alpha == 0.5003)),pch=1,bty="n")
plot(tourists_US.t,main="Total Air Passengers in the US",ylab="Total",xlab="Date")
fit1 <- ses(tourists_US.t, alpha=0.2, initial="simple", h=3)
fit2 <- ses(tourists_US.t, alpha=0.6, initial="simple", h=3)
fit3 <- ses(tourists_US.t, h=3)
plot(fit1, plot.conf=FALSE, ylab="Total",
xlab="Date", main="", fcol="white", type="o",include=50)
lines(fitted(fit1), col="blue", type="o")
lines(fitted(fit2), col="red", type="o")
lines(fitted(fit3), col="green", type="o")
lines(fit1$mean, col="blue", type="o")
lines(fit2$mean, col="red", type="o")
lines(fit3$mean, col="green", type="o")
legend("bottomleft",lty=1, col=c(1,"blue","red","green"),
c("data", expression(alpha == 0.2), expression(alpha == 0.6),
expression(alpha == 0.6263)),pch=1,bty="n")
getSymbols('XTEXVA01JPM664N',src='FRED',return.class="zoo")
## [1] "XTEXVA01JPM664N"
jap.exp.t <- ts(XTEXVA01JPM664N,start=c(1955,1),freq=12)
jap.exp.t0 <- window(jap.exp.t,end=c(2007,12))
jap.exp.t1 <- window(jap.exp.t,start=c(2008,1))
exp.t.ets.mnn.fit <- forecast(ets(jap.exp.t0,model="MNN"),100)
exp.t.ets.ann.fit <- forecast(ets(jap.exp.t0,model="ANN"),100)
exp.t.ets.aan.fit <- forecast(ets(jap.exp.t0,model="AAN"),100)
exp.t.ets.man.fit <- forecast(ets(jap.exp.t0,model="MAN"),100)
#exp.t.ets.amn.fit <- forecast(ets(jap.exp.t0,model="AMN"),100)
exp.t.ets.mmn.fit <- forecast(ets(jap.exp.t0,model="MMN"),100)
plot(window(jap.exp.t,start=c(2000,1)),main="Japanese Exports, non-seasonal forecasts",ylab="Exports",xlab="Date")
lines(exp.t.ets.mnn.fit$mean,col=2,type="l")
lines(exp.t.ets.ann.fit$mean,col=3,type="l")
lines(exp.t.ets.aan.fit$mean,col=4,type="l")
lines(exp.t.ets.man.fit$mean,col=5,type="l")
lines(exp.t.ets.mmn.fit$mean,col=6,type="l")
legend("topleft",lty=1,bty="n",col=c(2:6),legend=c("MNN","ANN","AAN","MAN","MMN"))
exp.t.ets.mmm.fit <- forecast(ets(jap.exp.t0,model="MMM"),100)
#exp.t.ets.amm.fit <- forecast(ets(jap.exp.t0,model="AMM"),100)
#exp.t.ets.aam.fit <- forecast(ets(jap.exp.t0,model="AAM"),100)
#exp.t.ets.mma.fit <- forecast(ets(jap.exp.t0,model="MMA"),100)
#exp.t.ets.ama.fit <- forecast(ets(jap.exp.t0,model="AMA"),100)
exp.t.ets.maa.fit <- forecast(ets(jap.exp.t0,model="MAA"),100)
exp.t.ets.aaa.fit <- forecast(ets(jap.exp.t0,model="AAA"),100)
plot(window(jap.exp.t,start=c(2000,1)),main="Japanese Exports, seasonal forecasts",ylab="Exports",xlab="Date",
ylim=range(c(window(jap.exp.t,start=c(2000,1)),
exp.t.ets.mmm.fit$mean,
exp.t.ets.maa.fit$mean,
exp.t.ets.aaa.fit$mean)))
lines(exp.t.ets.mmm.fit$mean,col=2,type="l")
lines(exp.t.ets.maa.fit$mean,col=3,type="l")
lines(exp.t.ets.aaa.fit$mean,col=4,type="l")
legend("topleft",lty=1,col=c(2,3,4),legend=c("MMM","MAA","AAA"),bty="n")