Forecasting for small business Exercises (Part-1)

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Uncertainty is the biggest enemy of a profitable business. That is especially true of small business who don’t have enough resources to survive an unexpected diminution of revenue or to capitalize on a sudden increase of the demand. In this context, it is especially important to be able to predict accurately the change in the markets to be able to make better decision and stay competitive.

This series of posts will teach you how to use data to make sound prediction. In this first set of exercises, you’ll learn the essential concepts we’ll use throughout the series: how to use the fundamental R tools for time series analysis, how to verify if a time series is stationary and how to make prediction in that context.

Answers to the exercises are available here.

To be able to do theses exercises, you have to have installed the R packages forecast and tseries.

Exercise 1
Use the data function to load the treering dataset from the R library. This dataset is loaded as an R time series object which is a vector whose value are ordered chronologically. Look at the structure of this ts object and use another function to find the number of observations in the dataset.

Exercise 2
Use the function window(ts, start, end) to select the observations in the treering dataset from the year 1500 to the year 2000.

Exercise 3
Do a basic plot of the treering dataset and use the abline function to add an horizontal red line representing the mean of the dataset.

Looking at the previous plot, we get the sense that these values are randomly distributed around the red line who represent the mean of the dataset. Also the magnitude of the random fluctuation of the points seems to stay stable over time. Such a time series is called “stationary” and it is a propriety we prefer to observe in a time series when we want to make predictions.

To make sure that the time series is stationary, we’ll draw the autocorrelation plot and run the Box-Ljung test, Kwiatkowski-Phillips-Schmidt-Shin test and the Augmented Dickey–Fuller test on the dataset.

Exercise 4
Load the forecast package and use the Acf function to draw the autocorrelation plot of the time series.

Exercise 5
Use the Box.test function to apply the Box-Ljung test on the data set. Set the parameter lag to the maximum value of Lag in the previous plot.

Exercise 6
Load the tseries package and apply the Kwiatkowski-Phillips-Schmidt-Shin test by using the kpss.test function on the data.

Exercise 7
Use the adf.test function to apply the Augmented Dickey–Fuller t-statistic test on the dataset. Set the argument alternative to “stationary”.

Exercise 8
Use the Holt-Winters Filtering method to apply an exponential smoothing on the time series. Use the function HoltWinters with the parameters beta and gamma set to “FALSE” to select the exponential smoothing and start the function at the first observation of the dataset. Store the result in a variable named HW.

Exercise 9
With the forecast.HoltWinters function make predictions for the next 5 years, store the results in a variable named prediction and print it to the screen.

Exercise 10
Use the plot.forecast function from the forecast package to plot your predictions.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)