From Data Smart by John Foreman
http://www.wiley.com/WileyCDA/WileyTitle/productCd-111866146X.html.
This is beautifully simple.
repo <- 'http://cran.us.r-project.org'
if('forecast' %in% rownames(installed.packages()) == FALSE) {
install.packages('forecast', repos=repo, dependencies=TRUE)
}
#
# if('ROCR' %in% rownames(installed.packages()) == FALSE) {
# install.packages('ROCR', repos=repo, dependencies=TRUE)
# }
#
#
library(forecast)
## Warning: package 'forecast' was built under R version 3.1.3
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.1.3
##
## Attaching package: 'zoo'
##
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Loading required package: timeDate
## Warning: package 'timeDate' was built under R version 3.1.2
## This is forecast 5.9
# library(ROCR)
sword <- read.csv('data/SwordDemand.csv')
head(sword)
## SwordDemand
## 1 165
## 2 171
## 3 147
## 4 143
## 5 164
## 6 160
str(sword)
## 'data.frame': 36 obs. of 1 variable:
## $ SwordDemand: int 165 171 147 143 164 160 152 150 159 169 ...
sword
## SwordDemand
## 1 165
## 2 171
## 3 147
## 4 143
## 5 164
## 6 160
## 7 152
## 8 150
## 9 159
## 10 169
## 11 173
## 12 203
## 13 169
## 14 166
## 15 162
## 16 147
## 17 188
## 18 161
## 19 162
## 20 169
## 21 185
## 22 188
## 23 200
## 24 229
## 25 189
## 26 218
## 27 185
## 28 199
## 29 210
## 30 193
## 31 211
## 32 208
## 33 216
## 34 218
## 35 264
## 36 304
First things first, tell R this is time series data.
# 12 observations per year
sword.ts <- ts(sword, frequency=12, start=c(2010,1))
sword.ts
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2010 165 171 147 143 164 160 152 150 159 169 173 203
## 2011 169 166 162 147 188 161 162 169 185 188 200 229
## 2012 189 218 185 199 210 193 211 208 216 218 264 304
plot(sword.ts)
Build the forecast now.
sword.forecast <- forecast(sword.ts)
head(sword.forecast)
## $model
## ETS(M,M,M)
##
## Call:
## ets(y = object, lambda = lambda)
##
## Smoothing parameters:
## alpha = 0.1214
## beta = 0.1214
## gamma = 1e-04
##
## Initial states:
## l = 161.5431
## b = 0.9996
## s=1.223 1.0843 1.009 0.9832 0.9163 0.9444
## 0.9392 1.0283 0.9047 0.9305 1.0355 1.0017
##
## sigma: 0.0393
##
## AIC AICc BIC
## 302.8388 331.4704 328.1751
##
## $mean
## Jan Feb Mar Apr May Jun Jul
## 2013 242.6378 256.2353 235.2032 233.6134 271.2415 253.0880 259.9610
## 2014 313.3843 330.9465 303.7821 301.7287 350.3282 326.8816 335.7585
## Aug Sep Oct Nov Dec
## 2013 257.6620 282.4400 296.1021 325.0422 374.5360
## 2014 332.7892 364.7917 382.4374 419.8157 483.7405
##
## $level
## [1] 80 95
##
## $x
## Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
## 2010 165 171 147 143 164 160 152 150 159 169 173 203
## 2011 169 166 162 147 188 161 162 169 185 188 200 229
## 2012 189 218 185 199 210 193 211 208 216 218 264 304
##
## $lower
## 10% 2.5%
## Jan 2013 230.3977 224.3139
## Feb 2013 242.8593 235.3235
## Mar 2013 222.5062 215.4269
## Apr 2013 219.4844 212.8845
## May 2013 252.9041 244.3434
## Jun 2013 233.2020 224.1851
## Jul 2013 238.1864 226.6787
## Aug 2013 233.1642 220.2350
## Sep 2013 251.6349 234.9534
## Oct 2013 259.7296 242.5367
## Nov 2013 279.6285 257.8512
## Dec 2013 317.5545 291.2836
## Jan 2014 260.9898 237.2761
## Feb 2014 269.8001 242.8920
## Mar 2014 242.6259 215.7451
## Apr 2014 236.3148 207.4536
## May 2014 267.7351 232.7634
## Jun 2014 245.5135 210.5799
## Jul 2014 245.8780 209.5092
## Aug 2014 238.4877 200.7887
## Sep 2014 253.2507 212.3311
## Oct 2014 259.3886 214.3535
## Nov 2014 278.8031 224.3925
## Dec 2014 311.8349 248.5048
##
## $upper
## 90% 97.5%
## Jan 2013 254.7923 260.9715
## Feb 2013 269.4530 276.2059
## Mar 2013 248.1033 254.6881
## Apr 2013 247.2641 255.0854
## May 2013 289.3467 299.5108
## Jun 2013 272.8619 283.2804
## Jul 2013 282.4541 296.4209
## Aug 2013 283.4397 297.7614
## Sep 2013 315.6356 334.2150
## Oct 2013 335.3138 358.3853
## Nov 2013 374.4637 403.1583
## Dec 2013 439.0163 475.1028
## Jan 2014 374.4268 409.5532
## Feb 2014 402.6267 444.6619
## Mar 2014 377.0689 419.3428
## Apr 2014 380.7130 428.7047
## May 2014 452.7548 512.6525
## Jun 2014 431.3003 496.4851
## Jul 2014 453.1920 524.7636
## Aug 2014 459.0212 540.1867
## Sep 2014 514.7884 616.2160
## Oct 2014 554.8254 662.9037
## Nov 2014 623.6753 759.9715
## Dec 2014 736.5476 906.2272
sword.forecast
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## Jan 2013 242.6378 230.3977 254.7923 224.3139 260.9715
## Feb 2013 256.2353 242.8593 269.4530 235.3235 276.2059
## Mar 2013 235.2032 222.5062 248.1033 215.4269 254.6881
## Apr 2013 233.6134 219.4844 247.2641 212.8845 255.0854
## May 2013 271.2415 252.9041 289.3467 244.3434 299.5108
## Jun 2013 253.0880 233.2020 272.8619 224.1851 283.2804
## Jul 2013 259.9610 238.1864 282.4541 226.6787 296.4209
## Aug 2013 257.6620 233.1642 283.4397 220.2350 297.7614
## Sep 2013 282.4400 251.6349 315.6356 234.9534 334.2150
## Oct 2013 296.1021 259.7296 335.3138 242.5367 358.3853
## Nov 2013 325.0422 279.6285 374.4637 257.8512 403.1583
## Dec 2013 374.5360 317.5545 439.0163 291.2836 475.1028
## Jan 2014 313.3843 260.9898 374.4268 237.2761 409.5532
## Feb 2014 330.9465 269.8001 402.6267 242.8920 444.6619
## Mar 2014 303.7821 242.6259 377.0689 215.7451 419.3428
## Apr 2014 301.7287 236.3148 380.7130 207.4536 428.7047
## May 2014 350.3282 267.7351 452.7548 232.7634 512.6525
## Jun 2014 326.8816 245.5135 431.3003 210.5799 496.4851
## Jul 2014 335.7585 245.8780 453.1920 209.5092 524.7636
## Aug 2014 332.7892 238.4877 459.0212 200.7887 540.1867
## Sep 2014 364.7917 253.2507 514.7884 212.3311 616.2160
## Oct 2014 382.4374 259.3886 554.8254 214.3535 662.9037
## Nov 2014 419.8157 278.8031 623.6753 224.3925 759.9715
## Dec 2014 483.7405 311.8349 736.5476 248.5048 906.2272
sword.forecast$method
## [1] "ETS(M,M,M)"
plot(sword.forecast)