Adapted from Chapter 1. Introductory to Time Series in R. Cowpertwait et al.
Sample time series data - the number of air passengers booking for the period 1949-1960.
AP <- AirPassengers
AP
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 1949 112 118 132 129 121 135 148 148 136 119 104 118
## 1950 115 126 141 135 125 149 170 170 158 133 114 140
## 1951 145 150 178 163 172 178 199 199 184 162 146 166
## 1952 171 180 193 181 183 218 230 242 209 191 172 194
## 1953 196 196 236 235 229 243 264 272 237 211 180 201
## 1954 204 188 235 227 234 264 302 293 259 229 203 229
## 1955 242 233 267 269 270 315 364 347 312 274 237 278
## 1956 284 277 317 313 318 374 413 405 355 306 271 306
## 1957 315 301 356 348 355 422 465 467 404 347 305 336
## 1958 340 318 362 348 363 435 491 505 404 359 310 337
## 1959 360 342 406 396 420 472 548 559 463 407 362 405
## 1960 417 391 419 461 472 535 622 606 508 461 390 432
cl = class(AP)
st = start(AP)
e = end(AP)
fr = frequency(AP)
Class = ts; start date = 1949, 1; end date = 1960, 12; frequency = 12
Preliminary plot
plot(AP, ylab = "Passengers (1000's)")
Reduce seasonal effect by aggregating data to the annual level.
layout(matrix(c(1,2), ncol=1)) # Layout divides the device up into as many rows and columns as there are in matrix
plot(aggregate(AP), ylab = "Aggregated annually")
boxplot(AP ~ cycle(AP))
From boxplot - most people travel from June-September
Maine.month <- read.table("Maine.dat", header=TRUE)
attach(Maine.month)
class(Maine.month)
## [1] "data.frame"
head(Maine.month)
## unemploy
## 1 6.7
## 2 6.7
## 3 6.4
## 4 5.9
## 5 5.2
## 6 4.8
Use ts to convert to time series
Maine.month.ts <- ts(unemploy, start = c(1996, 1), freq = 12)
Calculate the annual mean
Calculate precise percentage for unemployment
Maine.Feb <- window(Maine.month.ts, start = c(1996,2), freq = TRUE)
Maine.Aug <- window(Maine.month.ts, start = c(1996,8), freq = TRUE)
Feb.ratio <- mean(Maine.Feb) / mean(Maine.month.ts)
Aug.ratio <- mean(Maine.Aug) / mean(Maine.month.ts)
February ration is 1.222529 and August ration is 0.8163732
Three time series
## choc beer elec
## 1 1451 96.3 1497
## 2 2037 84.4 1463
## 3 2477 91.2 1648
## 4 2785 81.9 1595
## 5 2994 80.5 1777
## 6 2681 70.4 1824
Create a time series for each column:
Plot all three:
Use intersect function and plot:
AP.elec <- ts.intersect(AP, Elec.ts)
AP <- AP.elec[,1]
Elec <- AP.elec[,2]
layout(1:3)
plot(AP, main = "", ylab = "Air passengers / 1000's")
plot(Elec, main = "", ylab = "Electricity production / MkWh")
plot(as.vector(AP), as.vector(Elec), xlab = "Air passengers / 1000's", ylab = "Electricity production / MWh")
abline(reg = lm(Elec ~ AP))
plot(stl()) - produces a single figure showing the original series xt and the decomposed series m_t, s_t, and z_t
#plot(decompose(Elec.ts))
Elec.decom <- decompose(Elec.ts, type = "additive")
plot(Elec.decom)
Elec.decom <- decompose(Elec.ts, type = "mult")
plot(Elec.decom)
Trend <- Elec.decom$trend
Seasonal <- Elec.decom$seasonal
ts.plot(cbind(Trend, Trend * Seasonal), lty = 1:2)
The multiplicative model is more appropriate than the additive model because the variance of the original series and trend increase with time.
Decompose a time series into seasonal, trend and irregular components using loess, acronym STL.
plot(stl(Elec.ts,s.window = 7, t.jump=1))
plot(stl(Elec.ts,s.window = 7))
#plot(stl(Elec.ts,"per"))