<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-linear-trend-and-arima-models-exercises-part-2/"></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/15/forecasting-linear-models-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-linear-models-exercises-part-2"> 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/forecasting-part-2-main.png"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-5027" src="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/forecasting-part-2-main-300x194.png?resize=300%2C194" data-recalc-dims="1"></a></p>
<p>There are two main approaches to time series forecasting. One of them is to find persistent patterns in a time series itself, and extrapolate those patterns. Another approach is to discover how a series depend on other variables, which serve as predictors.<br>
This set of exercises focuses on the first approach, while the second one will be considered in a later set. The present set allows to practice in applying three forecasting models:<br>
– a naive model, which provides probably the simplest forecasting technique, but still can be useful as a benchmark for evaluating other methods,<br>
– a linear trend model (based on a simple linear regression),<br>
– the ARIMA model, a more sophisticated and popular model, which assumes a linear dependence of a time series on its past values and random shocks.<br>
The exercises do not require a deep understanding of underlying theories, and make use of automatic model estimation functions included in the <code>forecast</code> package. The set also help to practice in retrieving useful data from forecasts (confidence intervals, forecast errors), and comparing predictive accuracy of different models. The exercises are based on data on e-commerce retail sales in the USA for 1999-2016 retrieved from FRED, the Federal Reserve Bank of St. Louis database (<a target="_blank" rel="nofollow" href="http://www.r-exercises.com/wp-content/uploads/2017/04/ecommerce.csv">download here</a>). The data represent quarterly sales volumes in millions of dollars.</p>
<p>For other parts of the series follow the tag <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/tags/forecasting">forecasting</a></p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/15/forecasting-linear-models-part-2-solutions/">here</a></p>
<p><strong>Exercise 1</strong><br>
Read the data from the file, and transform it into a time series (<code>ts</code>) object (given that the data is quarterly and the starting period is the fourth quarter of 1999).<br>
Plot the obtained series.</p>
<p><strong>Exercise 2</strong><br>
Make a naive forecast for the next 8 periods using the appropriate function from the <code>forecast</code> package (i.e. create an object of the class <code>forecast</code> using the function that implements the naive method of forecasting) (Note that this method sets all forecast values equal to the last known time series value).</p>
<p><strong>Exercise 3</strong><br>
Plot the forecast values.</p>
<p><strong>Exercise 4</strong><br>
Make a forecast for the next 8 periods based on a linear model in two steps:<br>
(1) create a linear regression model for the forecast using the <code>tslm</code> function from the <code>forecast</code> package (use the series as the dependent variable, trend and season as independent variables),<br>
(2) make a forecast based on the model using the <code>forecast</code> function from the same package.<br>
Plot the forecast.</p>
<p><strong>Exercise 5</strong><br>
Retrieve forecast errors (residuals) from the linear model based forecast and save them as a separate variable.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/04/15/forecasting-linear-models-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-linear-models-exercises-part-2"></aside>
<div><strong>Learn more</strong> about Forecasting in the online course <a target="_blank" rel="nofollow" href="http://www.r-exercises.com/product/time-series-analysis-and-forecasting-in-r/">Time Series Analysis and Forecasting in R</a>. In this course you will learn how to:
<ul>
<li>A complete introduction on Forecasting</li>
<li>Work thru an exponentional smoothing instruction</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 6</strong><br>
Make a forecast for the next 8 periods based on the ARIMA model in two steps:<br>
(1) create a model using the <code>auto.arima</code> function from the <code>forecast</code> package,<br>
(2) make a forecast based on the model using the <code>forecast</code> function from the same package.<br>
Plot the forecast.</p>
<p><strong>Exercise 7</strong><br>
Print the summary of the forecast based on the ARIMA model.</p>
<p><strong>Exercise 8</strong><br>
Explore the structure the forecast summary. Find the forecast value for the last period, and its 5% confidence interval values.</p>
<p><strong>Exercise 9</strong><br>
Retrieve forecast errors (residuals) from the ARIMA based forecast. </p>
<p><strong>Exercise 10</strong><br>
Use the errors from the ARIMA based forecast and the errors from the linear model based forecast to compare predictive accuracy of the two models with the Diebold-Mariano test (implemented as a function in the <code>forecast</code> package). Test the hypothesis that the ARIMA based forecast is more accurate than the linear model based forecast.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Multiple Regression (Part 3) Diagnostics" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/03/11/multiple-regression-part-3-diagnostics/">Multiple Regression (Part 3) Diagnostics </a></li>
<li><a title="Forecasting for small business Exercises (Part-1)" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2017/04/13/forecasting-for-small-business-exercises-part-1/">Forecasting for small business Exercises (Part-1) </a></li>
<li><a title="3D plotting exercises" target="_blank" rel="nofollow" href="http://www.r-exercises.com/2016/04/06/3d-plotting-exercises/">3D plotting exercises </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/15/forecasting-linear-models-exercises-part-2/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-linear-models-exercises-part-2"> 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/iiW0BaQXY0Q"></a></div>