O'Reilly logo

Practical Predictive Analytics by Ralph Winters

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Computing the SMA using a function

To compute a rolling five-period moving average for our data, we will use the simple moving average (SMA) function from the TTR package, and then display the first few rows:

#install.packages("TTR")library(TTR) MA <- SMA(x, n = 5) cbind(head(x, 14), head(MA, 14)) >            [,1]      [,2]>  [1,] 0.1541247        NA>  [2,] 0.1574131        NA>  [3,] 0.1629424        NA>  [4,] 0.1609860        NA>  [5,] 0.1485338 0.1568000>  [6,] 0.1474000 0.1554551>  [7,] 0.1523271 0.1544379>  [8,] 0.1464598 0.1511414>  [9,] 0.1433967 0.1476235> [10,] 0.1455136 0.1470194> [11,] 0.1391090 0.1453612> [12,] 0.1347953 0.1418549> [13,] 0.1308892 0.1387408> [14,] 0.1362041 0.1373023 

There are many ways in which you can plot the moving average with the original data. Using base ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required