In [1]:
# Load the packages you intend to work with every time you start a new session
library(tseries)
library(TSA)
library(forecast)


Loading required package: leaps
Loading required package: locfit
locfit 1.5-9.1 	 2013-03-22
Loading required package: mgcv
Loading required package: nlme
This is mgcv 1.8-12. For overview type 'help("mgcv-package")'.

Attaching package: ‘TSA’

The following objects are masked from ‘package:stats’:

    acf, arima

The following object is masked from ‘package:utils’:

    tar

Loading required package: zoo

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Loading required package: timeDate

Attaching package: ‘timeDate’

The following objects are masked from ‘package:TSA’:

    kurtosis, skewness

This is forecast 7.1 


Attaching package: ‘forecast’

The following objects are masked from ‘package:TSA’:

    fitted.Arima, plot.Arima

The following object is masked from ‘package:nlme’:

    getResponse


In [5]:
milk <- read.csv("http://www.escet.urjc.es/biodiversos/R/milk.csv", header=T, sep=",")

In [6]:
str(milk)


'data.frame':	144 obs. of  3 variables:
 $ year : int  1994 1994 1994 1994 1994 1994 1994 1994 1994 1994 ...
 $ month: Factor w/ 12 levels "April","August",..: 5 4 8 1 9 7 6 2 12 11 ...
 $ milk : int  1343 1236 1401 1396 1457 1388 1389 1369 1318 1354 ...

In [7]:
head(milk)


yearmonthmilk
11994 January1343
21994 February1236
31994 March1401
41994 April1396
51994May 1457
61994June1388

In [8]:
milk


yearmonthmilk
11994 January1343
21994 February1236
31994 March1401
41994 April1396
51994May 1457
61994June1388
71994July1389
81994 August1369
91994 September1318
101994 October1354
111994 November1312
121994 December1370
131995 January1404
141995 February1295
151995 March1453
161995 April1427
171995May 1484
181995June1421
191995July1414
201995 August1375
211995 September1331
221995 October1364
231995 November1320
241995 December1380
251996 January1415
261996 February1348
271996 March1469
281996 April1441
291996May 1479
301996June1398
1152003July1597
1162003 August1571
1172003 September1511
1182003 October1561
1192003 November1517
1202003 December1596
1212004 January1624
1222004 February1531
1232004 March1661
1242004 April1636
1252004May 1692
1262004June1607
1272004July1623
1282004 August1601
1292004 September1533
1302004 October1583
1312004 November1531
1322004 December1610
1332005 January1643
1342005 February1522
1352005 March1707
1362005 April1690
1372005May 1760
1382005June1690
1392005July1683
1402005 August1671
1412005 September1599
1422005 October1637
1432005 November1592
1442005 December1663

In [11]:
milk.ts <- ts(data=milk$milk, start=c(1994, 1), end=c(2005,12), frequency = 12)

In [18]:
# has a trend and seasonality
plot(milk.ts, ylab = "milk production / cow", main="US monthly milk production")



In [14]:
alimilk <- HoltWinters(milk.ts)

In [23]:
# we use all the data for the trend (i.e. the trend is constant),
# but only half of the seasonality data
# and little of the white noise
alimilk


Holt-Winters exponential smoothing with trend and additive seasonal component.

Call:
HoltWinters(x = milk.ts)

Smoothing parameters:
 alpha: 0.8467204
 beta : 0
 gamma: 0.5552876

Coefficients:
           [,1]
a   1666.598837
b      2.141171
s1    21.089301
s2   -91.438172
s3    67.437048
s4    36.111285
s5    90.300812
s6    16.743017
s7    19.100623
s8    -3.367374
s9   -62.829218
s10  -19.659715
s11  -67.724830
s12   -3.981659

In [22]:
plot(alimilk, ylab = "milk production / cow", main="US monthly milk production")



In [20]:
# n.ahead=60: predict monthly for 5 yrs
pred.alimilk <- predict(alimilk, n.ahead = 60, prediction.interval = T)

In [24]:
plot(alimilk, pred.alimilk)
labs = c("observed", "predicted", "prediction interval")
legend("topleft", lty=rep(1,3), legend=labs, col=c("black", "red", "blue"))