<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-time-series-exploration-exercises-part-1/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r-exercises.com/2017/04/10/forecasting-time-series-exploration-exercises-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-time-series-exploration-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="http://r-exercises.com/wp-content/uploads/2017/04/forecasting-part-1-main-2.png?x61433"><img sizes="(max-width: 300px) 100vw, 300px" alt="" class="wp-image-5003" src="https://i0.wp.com/r-exercises.com/wp-content/uploads/2017/04/forecasting-part-1-main-2-300x148.png?resize=300%2C148" data-recalc-dims="1"></a></p>
<p>R provides powerful tools for forecasting time series data such as sales volumes, population sizes, and earthquake frequencies. A number of those tools are also simple enough to be used without mastering sophisticated underlying theories. This set of exercises is the first in a series offering a possibility to practice in the use of such tools, which include the ARIMA model, exponential smoothing models, and others.<br>
The first set provides a training in exploration of regularly spaced time series data (such as weekly, monthly, and quarterly), which may be useful for selection of a predictive model. The set covers:<br>
– visual inspection of time series,<br>
– estimation of trend and seasonal patterns,<br>
– finding whether a series is stationary (i.e. whether it has a constant mean and variance),<br>
– examination of correlation between lagged values of a time series (autocorrelation).<br>
The exercises make use of functions from the packages <code>forecast</code>, and <code>tseries</code>. Exercises are based on a dataset on retail sales volume by US clothing and clothing accessory stores for 1992-2016 retrieved from FRED, the Federal Reserve Bank of St. Louis database (<a target="_blank" rel="nofollow" href="http://r-exercises.com/wp-content/uploads/2017/04/sales.csv">download here</a>). The data represent monthly sales in millions of dollars.<br>
For other parts of the series follow the tag <a target="_blank" rel="nofollow" href="http://r-exercises.com/tags/forecasting">forecasting</a><br>
Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/04/10/forecasting-time-series-exploration-part-1-solutions/">here</a></p>
<p><strong>Exercise 1</strong><br>
Read the data from the file <code>sales.csv</code>.</p>
<p><strong>Exercise 2</strong><br>
Transform the data into a time series object of the <code>ts</code> type (indicate that the data is monthly, and the starting period is January 1992).<br>
Print the data.</p>
<p><strong>Exercise 3</strong><br>
Plot the time series. Ensure that the <code>y</code> axis starts from zero.</p>
<p><strong>Exercise 4</strong><br>
Use the <code>gghistogram</code> function from the <code>forecast</code> package to visually inspect the distribution of time series values. Add a kernel density estimate and a normal density function to the plot.</p>
<p><strong>Exercise 5</strong><br>
Use the <code>decompose</code> function to break the series into seasonal, trend, and irregular components (apply multiplicative decomposition).<br>
Plot the decomposed series.</p>
<p><strong>Exercise 6</strong><br>
Explore the structure of the decomposed object, and find seasonal coefficients (multiples). Identify the three months with the greatest coefficients, and the three months with the smallest coefficients. (Note that the coefficients are equal in different years).</p>
<p><strong>Exercise 7</strong><br>
Check whether the time series is trend-stationary (i.e. its mean and variance are constant with respect to a trend) using the function <code>kpss.test</code> from the <code>tseries</code> package. (Note that the null hypothesis of the test is that the series is trend-stationary).</p>
<p><strong>Exercise 8</strong><br>
Use the <code>diff</code> function to create a differenced time series (i.e. a series that includes differences between the values of the original series), and test it for trend stationarity.</p>
<p><strong>Exercise 9</strong><br>
Plot the differenced time series.</p>
<p><strong>Exercise 10</strong><br>
Use the <code>Acf</code> and <code>Pacf</code> functions from the <code>forecast</code> package to explore autocorrelation of the differenced series. Find at which lags correlation between lagged values is statistically significant at 5% level.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a title="Stock Prices Analysis part 3 – Exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/08/09/stock-prices-analysis-part-3-exercises/">Stock Prices Analysis part 3 – Exercises </a></li>
<li><a title="zoo time series exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2016/05/30/zoo-time-series-exercises/">zoo time series exercises </a></li>
<li><a title="Correlation and Correlogram Exercises" target="_blank" rel="nofollow" href="http://r-exercises.com/2017/04/08/correlation-and-correlogram-exercises/">Correlation and Correlogram Exercises </a></li>
<li><a target="_blank" rel="nofollow" href="http://r-exercises.com/start-here-to-learn-r/">Explore all our (>1000) R exercises</a></li>
<li><a target="_blank" rel="nofollow" href="http://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://r-exercises.com/2017/04/10/forecasting-time-series-exploration-exercises-part-1/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-time-series-exploration-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/lkPgz8bvpQY"></a></div>