In [1]:
# Load the packages you intend to work with every time you start a new session
library(tseries)
library(TSA)
library(forecast)
In [7]:
NAO <- read.table("http://www.escet.urjc.es/biodiversos/R/NAO.txt", header=T, sep="\t")
In [4]:
NAO.ts <- ts(NAO$NAO, start = min(NAO$year))
In [6]:
# looks stationary; no seasonality/trend in the data
plot(NAO.ts, type='o')
In [8]:
# since there's no trend / seasonality in the data,
# we use single expo. smoothing
In [9]:
alisim1 <- HoltWinters(NAO.ts, gamma=F, beta=F)
In [12]:
class(alisim1)
In [11]:
# alpha was chosen as 0.1020395, i.e. it uses most of the past observations
alisim1
In [14]:
plot(alisim1, ylab="NAO index", main="Simple exponential smoothing")
In [16]:
# available from library 'ts'
str(uspop)
In [18]:
plot(uspop, type='o')
In [19]:
# white noise: only look at last obs
# trend / slope: look at some recent obs (0.77)
aliholt <- HoltWinters(uspop, gamma = F)
In [21]:
# our prediction for the next year is 203.20000 (last obs) + 24.29044 (slope)
aliholt
In [20]:
plot(aliholt)
In [27]:
# n.ahead=5: look five time units ahead (our time unit here is 10 yrs)
pred.aliholt <- predict(aliholt, n.ahead = 5, prediction.interval = T)
In [25]:
plot(aliholt, pred.aliholt, ylab="pop in million", main="US pop")
In [28]:
data(co2, package = "TSA")
In [29]:
str(co2)
In [30]:
plot(co2, main = "monthly CO2 concentration")
In [31]:
aliHW <- HoltWinters(co2)
In [34]:
# we're using all the data for the trend and
# most of the data for seasonality and "noise"
# coefficients s1 .. s12 map to months in the first
# year in the future
aliHW
In [32]:
plot(aliHW)
In [35]:
# here: time unit = month
# n.ahead=120 -> look 10 yrs ahead
pred.aliHW <- predict(aliHW, n.ahead = 120, prediction.interval = T)
In [37]:
plot(aliHW, pred.aliHW, ylab="CO2 in ppm")
labs = c("observed", "predicted", "prediction interval")
legend("topleft", lty=rep(1,3), legend=labs, col=c("black", "red", "blue"))
In [ ]: