Skip to Content
Practical Predictive Analytics
book

Practical Predictive Analytics

by Ralph Winters
June 2017
Beginner to intermediate
576 pages
15h 22m
English
Packt Publishing
Content preview from Practical Predictive Analytics

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Data Superstream: Analytics Engineering

Data Superstream: Analytics Engineering

Alistair Croll, Anna Filippova, Emilie Schario, Lewis Davies, Jacob Frackson, Benn Stancil, Nick Acosta, Elizabeth Caley
R: Predictive Analysis

R: Predictive Analysis

Tony Fischetti, Eric Mayor, Rui Miguel Forte
Python: Advanced Predictive Analytics

Python: Advanced Predictive Analytics

Ashish Kumar, Joseph Babcock

Publisher Resources

ISBN: 9781785886188Supplemental Content