<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/forecasting-for-small-business-exercises-part-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/04/13/forecasting-for-small-business-exercises-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-for-small-business-exercises-part-1"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a target="_blank" rel="nofollow" href="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/time-series-exercises-3.png"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-4976" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/time-series-exercises-3-150x150.png?resize=150%2C150" data-recalc-dims="1"></a><br>
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.</p>
<p>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.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/13/forecasting-for-small-business-solutions-part-1/">here</a>.</p>
<p>To be able to do theses exercises, you have to have installed the R packages <code>forecast</code> and <code>tseries</code>. </p>
<p><strong>Exercise 1</strong><br>
Use the <code>data</code> function to load the <code>treering</code> 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 <code>ts</code> object and use another function to find the number of observations in the dataset.</p>
<p><strong>Exercise 2</strong><br>
Use the function <code>window(ts, start, end)</code> to select the observations in the treering dataset from the year 1500 to the year 2000. </p>
<p><strong>Exercise 3</strong><br>
Do a basic plot of the treering dataset and use the <code>abline</code> function to add an horizontal red line representing the mean of the dataset.</p>
<p>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.</p>
<p>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.</p>
<p><strong>Exercise 4</strong><br>
Load the <code>forecast</code> package and use the <code>Acf</code> function to draw the autocorrelation plot of the time series.</p>
<p><strong>Exercise 5</strong><br>
Use the <code>Box.test</code> 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.</p>
<p><strong>Exercise 6</strong><br>
Load the <code>tseries</code> package and apply the Kwiatkowski-Phillips-Schmidt-Shin test by using the <code>kpss.test</code> function on the data.</p>
<p><strong>Exercise 7</strong><br>
Use the <code>adf.test</code> function to apply the Augmented Dickey–Fuller t-statistic test on the dataset. Set the argument <code>alternative</code> to “stationary”.</p>
<p><strong>Exercise 8</strong><br>
Use the Holt-Winters Filtering method to apply an exponential smoothing on the time series. Use the function <code>HoltWinters</code> with the parameters <code>beta</code> and <code>gamma</code> 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 <code>HW</code>.</p>
<p><strong>Exercise 9</strong><br>
With the <code>forecast.HoltWinters</code> function make predictions for the next 5 years, store the results in a variable named <code>prediction</code> and print it to the screen. </p>
<p><strong>Exercise 10</strong><br>
Use the <code>plot.forecast</code> function from the <code>forecast</code> package to plot your predictions.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Forecasting: Time Series Exploration Exercises (Part-1)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/10/forecasting-time-series-exploration-exercises-part-1/">Forecasting: Time Series Exploration Exercises (Part-1) </a></li>
<li><a title="Stock Prices Analysis part 3 – Exercises" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2016/08/09/stock-prices-analysis-part-3-exercises/">Stock Prices Analysis part 3 – Exercises </a></li>
<li><a title="Lattice exercises – part 1" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2016/04/30/lattice-exercises-part-1/">Lattice exercises – part 1 </a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://www.r-exercises.com/r-courses/">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/04/13/forecasting-for-small-business-exercises-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-for-small-business-exercises-part-1"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/vdri5ESKFNM"></a></div>