<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-4/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/05/02/forecasting-for-small-business-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-for-small-business-exercises-part-4"> 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://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/DAX.png"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-4994" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/DAX-150x150.png?resize=150%2C150" data-recalc-dims="1"></a></p>
<p>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 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 the previous post we have seen the fundamental models of time series. We’ve seen what’s a white noise, what’s a random walk, what is seasonality and which transformation to use to make a time series stationary. Today, we’ll see some basic methods for predicting the values of a time series and how to select which method to use.</p>
<p>To be able to do theses exercise, you have to have installed the packages <code>forecast</code> and <code>smooth</code>. Also, when you generate a random number or process, set the seed to 42 to have results identical to those on the solution set.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/05/02/forecasting-for-small-business-solutions-part-4/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Create an artificial time series, which we’ll use to test the different forecasting methods we’ll use today. This random walk should have a white noise component with a mean of 0.5 and a standard deviation of 1, should have 365 observations with a frequency and deltat of 1.</p>
<p><strong>Exercise 2</strong><br>
The basic way to predict the future value of a time series is to suppose that the next value will be the same as the last. Just imagine you are the manager of a grocery store, that you have to order bread for the next week and that you don’t have the time to do some fancy R programming. Using the bread sale of last week as a reference for your order could be quite enough. This method is called the naive method, for good reason! Use this method to do predict the value of observations from t=331 to t=365, calculate the forecasting error and save the results in a <code>naive.forecast</code> data frame.</p>
<p><strong>Exercise 3</strong><br>
The <code>forecast</code> package provides the <code>naive()</code> function to use this method. Use this function to make prediction on the time windows t=331 to t=365 with a confidence interval of 90% and 95% then plot the forecast.</p>
<p>Looking back at the grocery store manager example, using the naive method can make you order too much or too little bread if last week’s sales were unusual. It gets worst for food which price can change drastically depending on the period. For example, lobster gets cheap during fishing season and using this method would under estimate the sales in the beginning of the fishing season and over estimate the sale during the week following this period. The naive method gives good result only when the time series is stable, so only when forecasting is not really useful!<br>
<strong>Exercise 4</strong><br>
Calculate three averages: the average of all the observations, of the observations 115 to 330 and of observations 329 to 330. Use those three values as predictions of value from 331 to 365 and calculate the estimation error. Save the result in 3 different data frames.</p>
<p>By computing the mean of the whole data set and using this value to do the forecast, we get a bigger estimation error than with the naive method, but the less observations we use to compute the mean the better the forecast become. Using the mean of the time series as forecast value is called the mean method and is useful for white noise process, but it’s not precise for non stationary time series, since his mean change over time. We can better better estimate that changing mean value by using a small number of observations in our computation. This method is called moving average and can be used to smooth the time series and easily make a prediction.<br>
<strong>Exercise 5</strong><br>
Use the mean method with the <code>meanf()</code> function to predict the last 35 observations of the time series with an confidence interval of 90 and 95 percent.</p>
<p><strong>Exercise 6</strong><br>
Use the <code>sma()</code> function from the <code>smooth</code> library to predict the last 35 of the time series by the moving average method. This function will use different size of n-day sample to compute the mean, select the model that best fit the data and make the prediction.</p>
<p><strong>Exercise 7</strong><br>
The moving average can also be used to remove the randomness of the time series curve to better see the trend in the data. Plot the time series and add two red lines: one line made by a moving average made with a n-day sample of 150 and another made by a n-day sample of 5. You can use the <code>ma()</code> function to compute the moving average.</p>
<p><strong>Exercise 8</strong><br>
With the rwf() function makes predictions for the observations between t=331 and t=365.</p>
<p><strong>Exercise 9</strong><br>
Calculate the RMSE of the prediction you made with each model and make a bar plot of the result. Which would be the best model to use to make forecasts of this time series?</p>
<p>In practice, when deciding which model to use to make a prediction, you should follow the process used in this exercise set: separate your historic data in a training and a test set, apply the algorithm on the training set, make prediction on the test set, calculate the prediction error, choose a metric like RMSE or MASE to evaluate the models and select the model which is the more effective to predict your data.</p>
<p>Note that in practice the distribution of your time series will change over time, so you must repeat this process periodically to make sure that you make prediction with the model best suited to the new data.<br>
<strong>Exercise 10</strong><br>
Follow the steps of the last exercise, but this time use only the observations</p>
<ul>
<li>between t=331 and t=335</li>
<li>between t=361 and t=365</li>
</ul>
<p>Does the prediction are more precise on the short or long term?</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Forecasting: Linear Trend and ARIMA Models Exercises (Part-2)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/15/forecasting-linear-models-exercises-part-2/">Forecasting: Linear Trend and ARIMA Models Exercises (Part-2) </a></li>
<li><a title="Forecasting: Exponential Smoothing Exercises (Part-3)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/17/forecasting-exponential-smoothing-exercises-part-3/">Forecasting: Exponential Smoothing Exercises (Part-3) </a></li>
<li><a title="Forecasting for small business Exercises (Part-3)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/25/forecasting-for-small-business-exercises-part-3/">Forecasting for small business Exercises (Part-3) </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/05/02/forecasting-for-small-business-exercises-part-4/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-for-small-business-exercises-part-4"> 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/LWTvip8SFt0"></a></div>