<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-3/"></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/25/forecasting-for-small-business-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-for-small-business-exercises-part-3"> 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://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/time_series_exercises_part3-1.png"><img sizes="(max-width: 150px) 100vw, 150px" alt="" class="wp-image-5402" src="https://i2.wp.com/www.r-exercises.com/wp-content/uploads/2017/04/time_series_exercises_part3-1-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 last set of exercises, we’ve seen how to make predictions on a random walk by isolating the white noise components via differentiation of the term of the time series. But this approach is valid only if the random components of the time series follow a normal distribution of constant mean and variance and if those components are added together in each iteration to create the new observations.</p>
<p>Today, we’ll see some transformations we can apply on the time series make them stationary, especially how to stabilise variance and how to detect and remove seasonality in a time series.</p>
<p>To be able to do theses exercise, you have to have installed the packages <code>forecast</code> and <code>tseries</code>.</p>
<p>Answers to the exercises are available <a target="_blank" rel="nofollow" href="http://r-exercises.com/2017/04/25/forecasting-for-small-business-solutions-part-3/">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Use the <code>data()</code> function to load the <code>EuStockMarkets</code> dataset from the R library. Then use the <code>diff()</code> function on <code>EuStockMarkets[,1]</code> to isolate the random component and plot them. This differentiation is the most used transformation with time series.</p>
<p>We can see that the mean of the random component of the time series seems to stay constant over time, but the variance seems to get bigger near 1997.<br>
<strong>Exercise 2</strong><br>
Apply a the <code>log()</code> function on <code>EuStockMarkets[,1]</code> and repeat the step of exercise 1. The logarithmic transformation is often used to stabilise non constant variance.</p>
<p><strong>Exercise 3</strong><br>
Use the <code>adf.test()</code> function from the <code>tseries</code> package to test if the time series you obtain in the last exercise is stationary. Use a lag of 1.</p>
<p><strong>Exercise 4</strong><br>
Load and plot the <code>co2</code> dataset from the R library <code>dataset</code>. Use the <code>lowess()</code> function to create a trend line and add it to the plot of the time series.</p>
<p>By looking at the last plot, we can see that the time series oscillate from one side to the other of the trend line with a constant period. That characteristic is called seasonally and is often observed in time series. Just think about temperature, which change predictably from season to season.<br>
<strong>Exercise 5</strong><br>
To eliminate the upward trend in the data use the <code>diff()</code> function and save the resulting time series in a variable called <code>diff.co2</code>. Plot the autocorrelation plot of <code>diff.co2</code>.</p>
<p><strong>Exercise 6</strong><br>
This last autocorrelation plot has years for unit which is not really intuitive in our scenario. Make another autocorrelation plot where the x axis has months as units. By looking at this plot, can you tell what is the seasonal period of this time series?</p>
<p>Another way to verify if the time series show seasonnality is to use the <code>tbats</code> function from the forecast package. As his named says, this function fits a tbats model on the time series and return a smooth curve that fit the data. We’ll learn more about that model in a future post.<br>
<strong>Exercise 7</strong><br>
Use the <code>tbats</code> function on the <code>co2</code> time series and store the result in a variable called <code>tbats.model</code>. If the time series show sign of seasonality, the <code>$seasonal.periods</code> value of <code>tbats.model</code> will store the period value, else the result will be null.</p>
<p><strong>Exercise 8</strong><br>
Use the <code>diff()</code> function with the appropriate lag to remove the seasonality of the <code>co2</code> time series, then use it again to remove the trend. Plot the resulting random component and the autocorrelation plot.</p>
<p><strong>Exercise 9</strong><br>
Apply the adf test, the kpss test and the Ljung-Box test on the result of the last exercise to make sure that the random component is stationary.</p>
<p><strong>Exercise 10</strong><br>
An interesting way to analyse a time series is to use the <code>decompose()</code> function which uses a moving average to estimate the seasonal, random and trend component of a time series. With that in mind, use this function and plot each component of the <code>co2</code> time series.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<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: 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="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 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/25/forecasting-for-small-business-exercises-part-3/?utm_source=rss&utm_medium=rss&utm_campaign=forecasting-for-small-business-exercises-part-3"> 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/mIPE4H1wj70"></a></div>