<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/some-insights-in-soccer-transfers-using-market-basket-analysis/"></div></div><div><a data-url="https://www.r-bloggers.com/some-insights-in-soccer-transfers-using-market-basket-analysis/" data-counturl="https://www.r-bloggers.com/some-insights-in-soccer-transfers-using-market-basket-analysis/" data-count="horizontal" data-text="Some insights in soccer transfers using Market Basket Analysis" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://longhowlam.wordpress.com/2016/09/12/some-insights-in-soccer-transfers-using-market-basket-analysis/"> R – Longhow Lam's Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img alt="linkedinfront" class="wp-image-1996" src="https://i1.wp.com/longhowlam.files.wordpress.com/2016/09/linkedinfront.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Introduction</h2>
<p>Although more than 20 years old, Market Basket Analysis (MBA) (or association rules mining) can still be a very useful technique to gain insights in large transactional data sets. The classical example is transactional data in a supermarket. For each customer we know what the individual products (items) are that he has put in his basket and bought. Other use cases for MBA could be web click data, log files, and even questionnaires.</p>
<p>With market basket analysis we can identify items that are frequently bought together. Usually the results of an MBA are presented in the form of <em><strong>rules</strong></em>. The rules can be as simple as {<em>A</em> ==> <em>B</em>}, when a customer buys item <em>A</em> then it is (very) likely that the customer buys item <em>B</em>. More complex rules are also possible {<em>A</em>, <em>B</em> ==> <em>D</em>, <em>F</em>}, when a customer buys items <em>A</em> and <em>B</em> then it is likely that he buys items <em>D</em> and <em>F</em>.</p>
<p><img alt="winkelmandje" class="wp-image-1999" src="https://i1.wp.com/longhowlam.files.wordpress.com/2016/09/winkelmandje.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Soccer transactional data</h2>
<p>To perform MBA you need of course data, but I don’t have real transactional data from a retailer that I can present here. So I am using soccer data instead<img alt="🙂" class="wp-smiley" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"> From the <a target="_blank" href="http://www.kaggle.com/hugomathien/soccer">Kaggle</a> site you can download some soccer data, thanks to <a target="_blank" href="http://www.kaggle.com/hugomathien">Hugo Mathien</a>. The data contains around 25.000 matches from eleven European soccer leagues starting from season 2008/2009 until season 2015/2016. After some data wrangling I was able to generate a transactional data set suitable for market basket analysis. The data structure is very simple, some records are given in the figure below:</p>
<p><img src="https://i1.wp.com/media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAghAAAAJDNlYmQ4MDIyLWMxMTAtNDIyOC05ZDc0LTA5NGEwY2IxZDYwZQ.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So we do not have customers but soccer players, and we do not have products but soccer clubs. In total, my soccer transactional data set contains around 18.000 records. Obviously, these records do not only include the <strong>multi-million transfers</strong> covered in the media, but also all the transfers of players nobody has ever heard of<img alt="🙂" class="wp-smiley" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3>Market basket results</h3>
<p>In R you can use the <strong><em>arules</em></strong> package for MBA / association rules mining. Alternatively, when the order of the transactions is important, like my soccer transfers, you should use the <strong><em>arulesSequences</em></strong> package. After running the algorithm I got some interesting results. The figure below shows the most frequently occurring transfers between clubs:</p>
<p><img src="https://i0.wp.com/media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAhLAAAAJDAwZDcyMzM0LWY5MDAtNDNlNi1hYTdlLWQzODk0NTM0M2NiYg.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>So in this data set the most frequently occurring transfer is from Fiorentina to Genoa (12 transfers in total). I have published the entire table with the rules on <a target="_blank" href="http://rpubs.com/longhowlam/soccertransfers2">RPubs</a>, where you can look up the transfer activity of your favorite soccer club.</p>
<h3>Network graph visualization</h3>
<p>All the rules that we get from the association rules mining form a <strong>network graph</strong>. The individual soccer clubs are the nodes of the graph and each rule “from ==> to” is an edge of the graph. In R, network graphs can be visualized nicely by means of the <em>visNetwork</em> package. The network is shown in the picture below.</p>
<p><img src="https://i0.wp.com/media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAkIAAAAJGNlZjQwZGJjLTA4YWItNGJmMi1iNGNlLTFhYzI2MjkyNDFjNQ.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>An interactive version can be found on <a target="_blank" href="http://rpubs.com/longhowlam/soccertransfers">RPubs</a>. The different colors represent the different soccer leagues. There are eleven leagues in this data, there are more leagues in Europe, but in this data we see that the <strong>Polish league is quite isolated</strong> from the rest. Almost blended in each other are the German, Spanish, English and French leagues. Less connected are the Scottish and Portuguese leagues, but also in the big English Premier and German leagues you will find less connected clubs like <strong>Bournemouth, Reading or Arminia Bielefeld</strong>.</p>
<p>The size of a node in the above graph represents it’s <strong>betweenness centrality</strong>, it is an indicator of a node’s <a target="_blank" href="http://en.wikipedia.org/wiki/Betweenness_centrality">centrality</a> in a network. It is equal to the number of shortest paths from all vertices to all others that pass through that node. In R betweenness measures can be calculated with the <strong>igraph package</strong>. The most central clubs in the transfers of players are <a target="_blank" href="http://www.sporting.pt/en">Sporting CP</a>, <a target="_blank" href="http://www.lechia.pl/">Lechia Gdansk</a>, <a target="_blank" href="http://www.safc.com/">Sunderland</a>, <a target="_blank" href="http://www.fcporto.pt/en/Pages/fc-porto.aspx">FC Porto</a> and <a target="_blank" href="http://www.psv.nl/">PSV Eindhoven</a>.</p>
<h2>Virtual Items</h2>
<p>An old trick among marketeers is to use <em>virtual items</em> in a market basket analysis. Besides the ‘physical’ items that a customer has in his basket, a marketeer can add extra virtual items in the basket. These could be for example customer characteristic like age-class, sex, but also things like day of week, region etc. The transactional data with virtual items might look like:</p>
<p><img src="https://i1.wp.com/media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAjwAAAAJDVlNzM3MmVhLTMzNDktNDM1NS04NGU1LWE3OTA5MmQyODVlYw.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>If you run a MBA on the transactional data with virtual items, interesting rules might appear. For example:</p>
<ul>
<li>{Chocolate, Female ==> Eggs}</li>
<li>{Chocolate, Male ==> Apples}</li>
<li>{Beer, Friday, Male, Age[18:23] ==> sausages}.</li>
</ul>
<p>Virtual items that I can add to my soccer transactional data are: <strong>age-class</strong>, four classes: 1: players younger than 25, 2: [25, 29), 3: [29, 33) and 4: the players that are 33 or older. <strong>Preferred foot</strong>, two classes: left or right. <strong>Height class</strong>, four classes: 1; players smaller than 178 cm, 2: [178, 183), 3: [183, 186), and 4: players taller than 186 cm.</p>
<p>After running the algorithm again the results allow you to find out the frequently occuring transfers of let-footers. I can see 4 left footers that transferred from Roma to Sampdoria, more rules can be seen on my <a target="_blank" href="http://rpubs.com/longhowlam/soccertransfers3">RPubs </a>site.</p>
<p><img src="https://i2.wp.com/media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAkvAAAAJGE3ZTU3NGZmLTdiMzktNGM3MS05YWRkLWVjMDUyMzhiODJjMQ.jpg?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Conclusion</h2>
<p>When you have transactional data, even as small as the soccer transfers, market basket analysis is definitely one of the techniques you should try to get some first insights. Feel free to look at my <strong>R code</strong> on <a target="_blank" href="http://github.com/longhowlam/soccertransfers">GitHub</a> to experiment with the soccer transfers data.</p>
<p><strong><em>Cheers Longhow.</em></strong></p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/longhowlam.wordpress.com/1995/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/longhowlam.wordpress.com/1995/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/some-insights-in-soccer-transfers-using-market-basket-analysis/"></div></div><div><a data-url="https://www.r-bloggers.com/some-insights-in-soccer-transfers-using-market-basket-analysis/" data-counturl="https://www.r-bloggers.com/some-insights-in-soccer-transfers-using-market-basket-analysis/" data-count="vertical" data-text="Some insights in soccer transfers using Market Basket Analysis" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://longhowlam.wordpress.com/2016/09/12/some-insights-in-soccer-transfers-using-market-basket-analysis/"> R – Longhow Lam's Blog</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/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=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/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/-WyT8QFvclU"></a></div>