O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

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

How to do it...

To apply the HoltWinters method for exponential smoothing and forecasting, follow these steps:

  1. Read the data. The file has monthly stock prices from Yahoo! Finance for Infosys between March 1999 and January 2015:
> infy <- read.csv("infy-monthly.csv") 
  1. Create the time series object:
> infy.ts <- ts(infy$Adj.Close, start = c(1999,3),     frequency = 12) 
  1. Perform Holt-Winters exponential smoothing:
> infy.hw <- HoltWinters(infy.ts)
  1. Plot the results:
> plot(infy.hw, col = "blue", col.predicted = "red") 

The plotted result can be seen as follows:

  1. Examine the results:
> # See the squared errors > infy.hw$SSE [1] 1446.232 > ...

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