Decomposition methods for time series

Following the previous example on monthly milk production (per cow) in the US for the 1994-2005 period, we want to make a time series decomposition using two different methods.


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

In [5]:
head(milk)


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

We will need to transform this into a time series before further processing.


In [10]:
# one data point / month, beginning in January 1994
milk.ts <- ts(data=milk$milk, start=c(1994, 1), frequency=12)

We can see that milk production shows an upward trend and is seasonal.


In [11]:
plot(milk.ts, ylab = "milk production / cow", main="US monthly milk production")


Task 1: Decompose by moving averages


In [13]:
milk.ma <- decompose(milk.ts)

In [16]:
plot(milk.ma)


Interpret the results of the model

We see an upward trend and very predictable seasonalilty, but the random part doesn't look random.

  • trend: slight dip in 2000 / 2001
  • random: local maxima in 1996 and 2000; local minima in 1999 and 2005

In [22]:
str(milk.ma)


List of 6
 $ x       : Time-Series [1:144] from 1994 to 2006: 1343 1236 1401 1396 1457 1388 1389 1369 1318 1354 ...
 $ seasonal: Time-Series [1:144] from 1994 to 2006: 25.2 -82.9 75.6 45.7 97.3 ...
 $ trend   : Time-Series [1:144] from 1994 to 2006: NA NA NA NA NA ...
 $ random  : Time-Series [1:144] from 1994 to 2006: NA NA NA NA NA ...
 $ figure  : num [1:12] 25.2 -82.9 75.6 45.7 97.3 ...
 $ type    : chr "additive"
 - attr(*, "class")= chr "decomposed.ts"

In [23]:
milk.ma$trend


          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
1994       NA       NA       NA       NA       NA       NA 1363.625 1368.625
1995 1384.042 1385.333 1386.125 1387.083 1387.833 1388.583 1389.458 1392.125
1996 1393.917 1393.625 1394.375 1395.958 1398.333 1401.167 1403.500 1403.417
1997 1421.208 1426.917 1431.042 1433.542 1435.375 1437.000 1439.208 1441.375
1998 1448.208 1447.875 1448.125 1449.750 1452.583 1456.292 1460.583 1465.125
1999 1486.750 1489.833 1494.125 1498.458 1502.500 1506.708 1511.292 1518.083
2000 1536.875 1541.083 1543.458 1545.083 1545.917 1545.125 1543.083 1538.708
2001 1530.958 1530.375 1530.500 1531.292 1532.833 1535.167 1538.167 1541.625
2002 1559.667 1562.833 1565.958 1567.708 1568.875 1570.417 1572.500 1574.208
2003 1577.375 1578.208 1578.833 1580.333 1582.125 1583.875 1585.125 1587.250
2004 1593.083 1595.417 1597.583 1599.417 1600.917 1602.083 1603.458 1603.875
2005 1626.917 1632.333 1638.000 1643.000 1647.792 1652.542       NA       NA
          Sep      Oct      Nov      Dec
1994 1373.250 1376.708 1379.125 1381.625
1995 1395.000 1396.250 1396.625 1395.458
1996 1403.875 1406.542 1410.000 1415.125
1997 1442.792 1444.542 1446.792 1448.167
1998 1470.417 1475.542 1480.000 1483.875
1999 1524.292 1527.875 1530.583 1532.958
2000 1534.208 1531.917 1530.833 1531.000
2001 1545.625 1550.000 1554.000 1557.250
2002 1575.417 1576.167 1576.125 1576.208
2003 1588.792 1588.958 1590.208 1591.542
2004 1605.417 1609.583 1614.667 1620.958
2005       NA       NA       NA       NA

Task 2: Decompose by local polynomial regression (LOESS)


In [18]:
milk.loess <- stl(milk.ts, s.window = "periodic")

In [19]:
plot(milk.loess)


Interpret and compare the results with those from the other model

We see an upward trend and very predictable seasonalilty, but the remainder part has outliers:

  • trend: slight dip in 2000 / 2001
  • remainder: local maxima in 1996 and 2000; local minima in 1999 and 2005

In [ ]: