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 EMA using a function

The following code will compute an EMA with a ratio of .8, using a lookback period of 5. The ratio of .8 will give the most weight to the most recent period, whilel still allowing the past to influence the prediction.

Then we will use cbind() to display the data point, as well as the simple and exponential moving averages:

ExpMA <- EMA(x, n = 5, ratio = 0.8)cbind(head(x, 15), head(MA, 15), head(ExpMA, 15)) >            [,1]      [,2]      [,3]>  [1,] 0.1541247        NA        NA>  [2,] 0.1574131        NA        NA>  [3,] 0.1629424        NA        NA>  [4,] 0.1609860        NA        NA>  [5,] 0.1485338 0.1568000 0.1568000>  [6,] 0.1474000 0.1554551 0.1492800>  [7,] 0.1523271 0.1544379 0.1517177>  [8,] 0.1464598 0.1511414 0.1475114>  [9,] 0.1433967 0.1476235 0.1442196> [10,] 0.1455136 ...

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