<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/but-can-ravens-forecast/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://thinkr.biz/2018/07/29/sales-forecast/"> R – thinkr</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>Why forecast sales?</h2>
<p>Humans have the magical ability to plan for future events, for future gain. It’s <a rel="nofollow" href="https://www.newscientist.com/article/2140668-ravens-can-plan-for-future-as-well-as-4-year-old-children-can/" target="_blank">not quite a uniquely human trait</a>. Because apparently ravens can match a 4-year-old.</p>
<p>An abundance of data, and some very nice R packages, make our ability to plan all the more powerful.</p>
<p>A couple of months ago we looked at sales from an historical perspective in <a rel="nofollow" href="https://thinkr.biz/2018/04/02/digital-marketplace-six-months-later/" target="_blank">Digital Marketplace. Six months later.</a> In this post, we’ll use the sales data to March 31st to model a time-series forecast for the next two years. The techniques apply to any time series with characteristics of trend, seasonality or longer-term cycles.<span></span></p>
<p>Why forecast sales? Business plans require a budget, e.g. for resources, marketing and office space. A good projection of revenue provides the foundation for the budget. And, for an established business, with historical data, time-series forecasting is one way to deliver a robust projection.</p>
<p>The forecast assumes one continues to do what one’s doing. So, it provides a good starting-point. Then one might, for example, add assumptions about new products or services.</p>
<h2>The power of iteration</h2>
<p>Businesses typically deal with many product / service lines. So the ability to iteratively forecast multiple time series is very powerful.</p>
<p>We’ll deal first with each government framework: <em>G-Cloud</em> (cloud services), and <em>DOS</em> (Digital Outcomes &amp; Specialists). Then we’ll iterate through G-Cloud’s lot structure: Cloud Hosting, Cloud Software and Cloud Support. Only three child levels, but the principle is easily scaled up.</p>
<h2>Cleaning data</h2>
<p>G-Cloud suppliers are contractually-obliged under the government’s framework to report monthly on their buyer invoicing. So, if some months were missed, then there would be a one-time catch-up in a later month. This could result in the odd outlier in the <a rel="nofollow" href="https://www.gov.uk/government/collections/digital-marketplace-sales" target="_blank">Digital Marketplace sales data</a>. However, <a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/07/DM_Forecast_2.html" target="_blank">as revealed in the more detailed analysis (with code)</a>, none was discovered.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i0.wp.com/thinkr.biz/wp-content/uploads/2018/07/plot-cleaning-1.png?w=450&ssl=1" alt="" class="wp-image-969" data-recalc-dims="1"></p>
<h2>Seasonal decomposition</h2>
<p>By decomposing the historical data we can tease out the underlying trend and seasonality:</p>
<ul>
<li>
<ul>
<li><span><strong>Trend:</strong>  </span>G-Cloud sales have grown over time as more suppliers have added their services to the government frameworks. And more Public Sector organizations have found the benefits of purchasing Cloud services this way. Why? Because it’s a faster, simpler, more transparent and competitive contracting vehicle.</li>
<li><span><strong>Seasonality:</strong>  </span>Suppliers often manage their sales and financials based on a quarterly cycle. There’s a particular emphasis on a strong close to the financial year (often December 31st for commercial enterprises). And government buyers may want to make optimal use of their budgets at the close of their financial year (March 31st). Consequently, we see quarterly seasonality with an extra spike in March, and a secondary peak in December.</li>
</ul>
</li>
</ul>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i0.wp.com/thinkr.biz/wp-content/uploads/2018/07/plot-decomposition-1.png?w=450&ssl=1" alt="" class="wp-image-938" data-recalc-dims="1"></p>
<h2>Forecasting sales for each framework</h2>
<p>Using <a rel="nofollow" href="https://en.wikipedia.org/wiki/Autoregressive_integrated_moving_average" target="_blank">AutoRegressive Integrated Moving Average</a> (ARIMA) modelling, we can select from close to 100 models to describe the autocorrelations in the data. Then we can use the generated model to forecast future sales.</p>
<p>In the plot below, we project two years ahead with 80% and 95% prediction intervals. This means the darker-shaded 80% range should include the future sales value with an 80% probability. Likewise with a 95% probability when adding the wider and lighter-shaded area.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i0.wp.com/thinkr.biz/wp-content/uploads/2018/07/Auto-1.png?w=450&ssl=1" alt="" class="wp-image-936" data-recalc-dims="1"></p>
<p>The DOS framework (for project-related services) was launched more recently in June 2016. It exhibits different time-series characteristics. Hence a different ARIMA model.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2018/07/plot-dos-1.png?w=450&ssl=1" alt="" class="wp-image-939" data-recalc-dims="1"></p>
<h2>Forecasting sales for the component lots</h2>
<p>The G-Cloud framework comprises three lots. There are different ways of forecasting multiple time series. We will do so in one shot, with the best model tailored to each lot. The possible approaches, and code used, are detailed <a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/07/DM_Forecast_2.html" target="_blank">here</a>.</p>
<p><img sizes="(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px" src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2018/07/lots-1.png?w=450&ssl=1" alt="" class="wp-image-937" data-recalc-dims="1"></p>
<p>Ravens aren’t yet ready for forecasting with R. But then neither are 4-year-olds, are they?</p>
<h2>R toolkit</h2>
<p>R packages and functions (excluding base) used in this analysis.</p>
<div>
<table>
<thead>
<tr>
<th> </th>
<th><strong>Packages</strong></th>
<th><strong>Functions</strong></th>
</tr>
</thead>
<tbody>
<tr>
<td><img src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2017/10/Program-e1509478672361.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-135" data-recalc-dims="1" height="100"></td>
<td>purrr</td>
<td>map[6]; map2_df[1]; possibly[1]; set_names[1]; simplify[1]; some[1]; when[1]</td>
</tr>
<tr>
<td><img src="https://i0.wp.com/thinkr.biz/wp-content/uploads/2017/10/Import-e1509478685299.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-134" data-recalc-dims="1" height="100"></td>
<td>readr</td>
<td>guess_encoding[3]; locale[2]; read_csv[2]; parse_number[1]</td>
</tr>
<tr>
<td rowspan="9"><img src="https://i2.wp.com/thinkr.biz/wp-content/uploads/2017/10/Transform-e1509478657749.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-136" data-recalc-dims="1" height="100"></td>
<td>dplyr</td>
<td>mutate[12]; if_else[8]; filter[6]; group_by[6]; desc[3]; first[3]; select[3]; summarise[3]; summarize[2]; arrange[1]; as_tibble[1]; between[1]; bind_rows[1]; collapse[1]; data_frame[1]; n[1]</td>
</tr>
<tr>
<td>tibble</td>
<td>str_c[7]; fixed[2]; str_remove[2]; str_count[1]; str_detect[1]; str_extract[1]; str_replace[1]</td>
</tr>
<tr>
<td>stringr</td>
<td>str_c[7]; fixed[2]; str_remove[2]; str_count[1]; str_detect[1]; str_extract[1]; str_replace[1]</td>
</tr>
<tr>
<td>rebus</td>
<td>or[4]; alpha[1]; literal[1]; whole_word[1]</td>
</tr>
<tr>
<td>lubridate</td>
<td>month[11]; year[4]; ceiling_date[1]; date[1]; days_in_month[1]; myd[1]; parse_date_time[1]; tz[1]; ymd[1]</td>
</tr>
<tr>
<td>timetk</td>
<td>tk_ts[3]</td>
</tr>
<tr>
<td>sweep</td>
<td>str_c[7]; fixed[2]; str_remove[2]; str_count[1]; str_detect[1]; str_extract[1]; str_replace[1]</td>
</tr>
<tr>
<td>tidyr</td>
<td>str_c[7]; fixed[2]; str_remove[2]; str_count[1]; str_detect[1]; str_extract[1]; str_replace[1]</td>
</tr>
<tr>
<td>forecast</td>
<td>forecast[14]; auto.arima[8]; autoplot[7]; BoxCox[4]; mstl[2]; ndiffs[2]; nsdiffs[2]; tsclean[2]; BoxCox.lambda[1]; seasonal[1]</td>
</tr>
<tr>
<td rowspan="6"><img src="https://i1.wp.com/thinkr.biz/wp-content/uploads/2017/10/Visualize-e1509478640644.png?resize=100%2C100&ssl=1" alt="" width="100" class="wp-image-137" data-recalc-dims="1" height="100"></td>
<td>ggplot2</td>
<td>autoplot[7]; xlab[6]; ylab[6]; aes[5]; theme[5]; labs[4]; element_rect[3]; geom_ribbon[2]; unit[2]; alpha[1]; element_line[1]; element_text[1]; facet_wrap[1]; geom_line[1]; geom_path[1]; geom_text[1]; ggplot[1]; margin[1]; scale_x_date[1]</td>
</tr>
<tr>
<td>scales</td>
<td>or[4]; alpha[1]; literal[1]; whole_word[1]</td>
</tr>
<tr>
<td>cowplot</td>
<td>draw_label[1]; plot_grid[1]</td>
</tr>
<tr>
<td>ggthemes</td>
<td>theme_economist[1]</td>
</tr>
<tr>
<td>kableExtra</td>
<td>kable[3]; kable_styling[2]</td>
</tr>
<tr>
<td>knitr</td>
<td>kable[3]; opts_chunk[1]</td>
</tr>
</tbody>
</table>
</div>
<p></p>
<p>View the code <a rel="nofollow" href="https://thinkr.biz/wp-content/uploads/2018/07/DM_Forecast_2.html" target="_blank">here</a>.</p>
<h3>Citations</h3>
<p>R Development Core Team (2008). R: A language and environment for<br>
statistical computing. R Foundation for Statistical Computing,<br>
Vienna, Austria. ISBN 3-900051-07-0, URL <a rel="nofollow" href="http://www.r-project.org/" target="_blank">http://www.R-project.org</a>.</p>
<p>Hyndman R, Athanasopoulos G, Bergmeir C, Caceres G, Chhay L, O’Hara-Wild M, Petropoulos F, Razbash S, Wang E, Yasmeen F (2018). forecast: Forecasting functions for time series and linear models. R package version 8.4, <a rel="nofollow" href="http://pkg.robjhyndman.com/forecast/" target="_blank">http://pkg.robjhyndman.com/forecast</a>.</p>
<p>Hyndman RJ, Khandakar Y (2008). “Automatic time series forecasting: the forecast package for R.” Journal of Statistical Software, 26(3), 1–22. <a rel="nofollow" href="http://www.jstatsoft.org/article/view/v027i03" target="_blank">http://www.jstatsoft.org/article/view/v027i03</a>.</p>
<p>Contains public sector information licensed under the <a rel="nofollow" href="https://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/" target="_blank">Open Government Licence v3.0</a>.</p>
<p>The post <a rel="nofollow" href="https://thinkr.biz/2018/07/29/sales-forecast/" target="_blank">But can ravens forecast?</a> appeared first on <a rel="nofollow" href="https://thinkr.biz/" target="_blank">thinkr</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://thinkr.biz/2018/07/29/sales-forecast/"> R – thinkr</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/C5GKD-pVgcE" height="1"></a></div>