<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/euro-2016-analytics-whos-playing-the-toughest-game/"></div></div><div><a data-url="http://www.r-bloggers.com/euro-2016-analytics-whos-playing-the-toughest-game/" data-counturl="http://www.r-bloggers.com/euro-2016-analytics-whos-playing-the-toughest-game/" data-count="horizontal" data-text="Euro 2016 analytics: Who’s playing the toughest game?" 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="http://datascienceplus.com/euro-2016-analytics-whos-playing-the-toughest-game/"> DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I am really enjoying Uefa Euro 2016 Footbal Competition, even because our national team has done pretty well so far. That’s why after  browsing for a while <a target="_blank" rel="nofollow" href="http://www.uefa.com/uefaeuro/season=2016/statistics/index.html">statistics section of official EURO 2016 website</a> I decided to do some analysis on the data they share</p>
<p>Just to be clear from the beginning: we are not talking of anything too rigourus, but just about some interesting questions with related answers gathered mainly through data visualisation.</p>
<p>We can divide following analyses into two main parts: a first part were we analyse distribution of fouls and their incidence on matches outcome ( data as at the 21th of June) and a second part where ball possession is analysed, once again looking at relationship between this stat and matches outcome (data as at 28th june). Let’s start with some specs on data import.</p>
<h2>Data import and treatment</h2>
<p>As usual we first need  to load required packages and our data into the R environment. We perform the task running the following lines of code:</p>
<pre>
# Load required packages
library(rio)
library(plyr)
library(dplyr)
library(choroplethr)
library(choroplethrMaps)
library(ggplot2)
library(dummies)

#data from http://www.uefa.com/uefaeuro/season=2016/statistics/index.html
players_stat &lt;- read.csv('players_stats.csv',sep = &quot;;&quot;)
players_stat$Team &lt;- tolower(players_stat$Team)
team_stat &lt;- read.csv('team_stats.csv' ,sep = &quot;;&quot;)
team_stat$Team &lt;- tolower(team_stat$Team)
possession_stat &lt;- read.csv('possession.csv', sep = &quot;;&quot;)
possession_stat$team &lt;- tolower(possession_stat$team)
</pre>
<p>I am not going to spend too much time on this chunk but I would like to highlight you the use of rio package by <a target="_blank" rel="nofollow" href="http://thomasleeper.com/">Thomas J. Leeper</a>, since it is a really powerfull package that can make your life really easy when talking about data import in R.</p>
<p>You can find referred .csv file within the rstudio project <a target="_blank" rel="nofollow" href="https://github.com/AndreaCirilloAC/euro_2016_stat">Rstudio project I have uploaded on Github.</a></p>
<h2>Which team committed the  greatest number of fouls?</h2>
<p>Here we are with the first question. And here it is the answer:</p>
<p><a title="" data-rel="lightbox-0" target="_blank" href="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls-490x336.png?resize=456%2C313 490w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls-226x156.png 226w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls-600x412.png 600w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls-300x206.png 300w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls.png 691w" alt="fouls" class="wp-image-4240" src="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls-490x336.png?resize=456%2C313" data-recalc-dims="1"></a></p>
<p>We obtained the plot aggregating fouls data by team and leveraging <code>country_choropleth()</code> function from <code>choroplethr</code> package</p>
<pre>
#sum up fouls data from player view to team view
by_team &lt;- group_by(players_stat,Team)
team_sums &lt;- summarise(by_team, sum(Yellow.Cards),
sum(Red.Cards),
sum(Fouls.Committed),
sum(Fouls.Suffered))

# subset columns to plot only the number of fouls committed
fouls_data &lt;- data.frame(&quot;region&quot; = team_sums$Team,&quot;value&quot; = team_sums$`sum(Fouls.Committed)`)
# plot
fouls_plot &lt;- country_choropleth(fouls_data,
title = &quot;number of Fouls Committed by region&quot;,
legend = &quot;# fouls&quot;,
num_colors = 1) +
xlim(-31.266001, 39.869301) +
ylim(27.636311, 81.008797) +
coord_map(&quot;lambert&quot;, lat0 = 27.636311, lat1 = 81.008797)
fouls_plot
</pre>
<p>Given that black countries are countries not playing the competition it seams Romania committed quite a great number of fouls. You may wonder: were them serious fouls? Let’s have a look to the number of yellow and red cards to answer that legitimate question. Let’s start from yellow cards, plotting <code>sum(Yellow.Cards)</code> against each team with standard barplot:</p>
<pre>
ggplot(team_sums,aes(x = Team,y = `sum(Yellow.Cards)`, fill = `sum(Yellow.Cards)` )) +
  geom_bar(stat = 'identity') +
  coord_flip()
</pre>
<p>Here it is the plot:<br>
<a title="" data-rel="lightbox-1" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/07/yellow_cards.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/07/yellow_cards-490x325.png?resize=456%2C302 490w, http://datascienceplus.com/wp-content/uploads/2016/07/yellow_cards-600x398.png 600w, http://datascienceplus.com/wp-content/uploads/2016/07/yellow_cards-300x199.png 300w, http://datascienceplus.com/wp-content/uploads/2016/07/yellow_cards.png 730w" alt="yellow_cards" class="wp-image-4241" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/07/yellow_cards-490x325.png?resize=456%2C302" data-recalc-dims="1"></a></p>
<p>Well, Romania togheter with Albania is standing at the top of yellow cards ranking as well. Getting to red cards we have to underline that we dont’ have a lot of data about them, since just two of them were assigned. Guess to whom? Albania and  Austria. All that summed up we can definitely say Albania am Romania played toughest  matches overall. but was that worthing? Uhm I guess no, since both of them didn’t get trough staging phase. And this raise another interesting question:</p>
<h2>Is there a correlation between number of fouls and number of wins?</h2>
<p>To answer this technically silly question we can try to plot total number of fouls against number of wins by country team. Let’s have a look to the plot:</p>
<pre>
#plot fouls committed against number of wins by country
ggplot(total_stats,aes(x = total_stats$`sum(Fouls.Committed)`,y = total_stats$Wins, label = Team)) + 
  geom_point() +
  geom_text(nudge_y = 0.2) +
  geom_smooth(method = 'lm', formula = y~x)
</pre>
<p>Here it is the plot:<br>
<a title="" data-rel="lightbox-2" target="_blank" href="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_wins.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_wins-490x261.png?resize=456%2C243 490w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_wins-600x319.png 600w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_wins-300x160.png 300w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_wins.png 737w" alt="fouls_vs_wins" class="wp-image-4242" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_wins-490x261.png?resize=456%2C243" data-recalc-dims="1"></a></p>
<p>We added a linear regression model to the scatter plot to investigate the hypothesis of linear relationship but nothing happened, so no, committing more fouls is not a winning strategy. Let us ask one last reasonable question about fouls:</p>
<h2>Is committing more fouls at least a good strategy in order to reduce goals against?</h2>
<p>Once again let’s the data speak out, plotting number of goal against togheter with number of fouls committed:</p>
<pre>
ggplot(total_stats,aes(x = total_stats$`sum(Fouls.Committed)`,
                       y = total_stats$Total.goals.against, label = Team)) +
  geom_point() +
  geom_text(nudge_y = 0.2) +
  geom_smooth(method = 'lm', formula = y~x)
</pre>
<p>Here is the plot:<br>
<a title="" data-rel="lightbox-3" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_goals_against.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_goals_against-490x329.png?resize=456%2C306 490w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_goals_against-600x402.png 600w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_goals_against-300x201.png 300w, http://datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_goals_against.png 695w" alt="fouls_vs_goals_against" class="wp-image-4243" src="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/fouls_vs_goals_against-490x329.png?resize=456%2C306" data-recalc-dims="1"></a></p>
<p>Ok, it could seems we can answer a sound yes, but I get a bit disturbed by that 52 romanian fouls so I am going to try to remove them and plot again:</p>
<pre>
total_stats_no_romania &lt;- total_stats[-16,]
ggplot(total_stats_no_romania,aes(x = total_stats_no_romania$`sum(Fouls.Committed)`,
                                  y = total_stats_no_romania$Total.goals.against, label = Team)) +
  geom_point() +
  geom_text(nudge_y = 0.2) +
  geom_smooth(method = 'lm',formula = y~x)
</pre>
<p>Here is the plot:<br>
<a title="" data-rel="lightbox-4" target="_blank" href="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/plot_fouls_goals_no_rom.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/plot_fouls_goals_no_rom-490x260.png?resize=456%2C242 490w, http://datascienceplus.com/wp-content/uploads/2016/07/plot_fouls_goals_no_rom-768x407.png 768w, http://datascienceplus.com/wp-content/uploads/2016/07/plot_fouls_goals_no_rom-600x318.png 600w, http://datascienceplus.com/wp-content/uploads/2016/07/plot_fouls_goals_no_rom-300x159.png 300w, http://datascienceplus.com/wp-content/uploads/2016/07/plot_fouls_goals_no_rom.png 975w" alt="plot_fouls_goals_no_rom" class="wp-image-4244" src="http://i1.wp.com/datascienceplus.com/wp-content/uploads/2016/07/plot_fouls_goals_no_rom-490x260.png?resize=456%2C242" data-recalc-dims="1"></a></p>
<p>Our first guess seems to be confirmed: number  of fouls committed is negatively correlated to the number of goals against suffered, which appears to be reasonable since we may assume a great number of fouls are committed to stop rivals from dangerous game actions.</p>
<h2>Who’s keeping the ball more? ball possession stats</h2>
<p>Ball possession is a key start when analysing a football match, so let’s start answering this introductory question: who’s scoring the highest possession stat within this competition? Once again we can visualise it on a europe map:<br>
<a title="" data-rel="lightbox-5" target="_blank" href="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/07/ball_possession_map.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/ball_possession_map-490x251.png?resize=456%2C234 490w, http://datascienceplus.com/wp-content/uploads/2016/07/ball_possession_map-600x308.png 600w, http://datascienceplus.com/wp-content/uploads/2016/07/ball_possession_map-300x154.png 300w, http://datascienceplus.com/wp-content/uploads/2016/07/ball_possession_map.png 688w" alt="ball_possession_map" class="wp-image-4245" src="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/ball_possession_map-490x251.png?resize=456%2C234" data-recalc-dims="1"></a></p>
<p>Before actually producing this plot we had to treat quite heavily raw data since they where produced in untidy format, meaning each row stored more than one observation of the attribute we are investigating: ball possession. For isntance first record was related to Northern Ireland vs Germany match played on 21/06/2016, showing a possession possession stat related to Northern Ireland on a column, and another possession stat related to Germany in a second column. We then needed to split our dataset into two separate data frames, each one storing one of two possession stat columns, and then binding those data frames togheter merging possession stats columns:</p>
<pre>
possession_a &lt;- possession_stat[,-c(4,8)]
possession_a$win  possession_a$score_
possession_b &lt;- possession_stat[,-c(3,7)]
possession_b$win &lt;- possession_b$score &lt; possession_b$score_
possession_a &lt;- possession_a[,-5]
possession_b &lt;- possession_b[,-4]
colnames(possession_b) &lt;- colnames(possession_a)
possession_tidy &lt;- rbind(possession_a,possession_b)
</pre>
<p>Once our data are ready, we can easily produce above showed mpa leveraging once again <code>country_choropleth()</code> function:</p>
<pre>
by_team_pos &lt;- group_by(possession_tidy,team)
team_means &lt;- summarise(by_team_pos, mean(possession))
colnames(team_means) &lt;- c(&quot;region&quot;,&quot;value&quot;)

possession_plot &lt;- country_choropleth(team_means,
                                 title = &quot;mean ball possession&quot;,
                                 legend=&quot;% possession&quot;,
                                 num_colors=1)+
  xlim(-31.266001, 39.869301)+
  ylim(27.636311, 81.008797) +
  coord_map(&quot;lambert&quot;, lat0=27.636311, lat1=81.008797)
possession_plot
</pre>
<p>Table: Giving a closer look to first five teams by ball possession we find a prevalence of north western europe countries:</p>
<table>
<tbody>
<tr>
<td><b>Country</b></td>
<td><b>Mean Possession</b></td>
</tr>
<tr>
<td>Germany</td>
<td>71%</td>
</tr>
<tr>
<td>ukraine</td>
<td>65%</td>
</tr>
<tr>
<td>portugal</td>
<td>62%</td>
</tr>
<tr>
<td>spain</td>
<td>62%</td>
</tr>
</tbody>
</table>
<p>so here’s come the question that has always stimulated my curiosity: ball possession is always considered a relevant topic, but is it correlated with matches victory? in other words:</p>
<h2>Increasing ball possession increases winning likelihood?</h2>
<p>we are going to tackle this visually, plotting percentage of ball possession against a <code>win_dummy</code> variable, scoring 1 for win and 0 for defeat (ties were considered as defeats) , which can be easily done leveraging <em>dummies</em> and <em>ggplot</em> packages :</p>
<pre>
dummy_win &lt;- as.data.frame(dummy(possession_tidy$win))
possession_tidy$win_dummy &lt;- dummy_win[,2]
ggplot(data = possession_tidy,aes(x = possession)) +
  geom_point(aes(y = win_dummy)) +
  stat_smooth(aes(y = win_dummy),method = &quot;glm&quot;)
</pre>
<p>Here is the plot:<br>
<a title="" data-rel="lightbox-6" target="_blank" href="http://i0.wp.com/datascienceplus.com/wp-content/uploads/2016/07/possession_truth.png"><img sizes="(max-width: 490px) 100vw, 490px" srcset="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/07/possession_truth-490x361.png?resize=456%2C336 490w, http://datascienceplus.com/wp-content/uploads/2016/07/possession_truth-600x442.png 600w, http://datascienceplus.com/wp-content/uploads/2016/07/possession_truth-300x221.png 300w, http://datascienceplus.com/wp-content/uploads/2016/07/possession_truth.png 656w" alt="possession_truth" class="wp-image-4247" src="http://i2.wp.com/datascienceplus.com/wp-content/uploads/2016/07/possession_truth-490x361.png?resize=456%2C336" data-recalc-dims="1"></a></p>
<p>As you can see we have also added an estimated logistic regression which visually enforces the truth already visible from raw data: there is no positive correlation, or at least not a strong one, between ball possession and matches outcome. This seems to me a non obvious result, since a great number successful of football coaches have founded their success on possession ball. It is actually quite a debated topic as you can see simply googling “correlation between ball possession win”. </p>
<h2>Conclusions</h2>
<p>Within this post we tried to answer some questions about matches occured so far during UEFA EURO 2016 and we definitely found that being tough is not a good tactic to win your matches but at least can help you avoid suffering a great number of goals. Moreover we understood that ball possession is only weakly related to matches outcome. If you are interested in reperforming showed analyses or even further explore related raw data you can have a look to the <a target="_blank" rel="nofollow" href="https://github.com/AndreaCirilloAC/euro_2016_stat">Rstudio project I have uploaded on Github.</a></p>
<p>That said: may the best win!</p>
<p>Let me know if you have any comment or question.</p>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a title="Permanent Link toIntegrating R with Apache Hadoop" target="_blank" rel="bookmark" href="http://datascienceplus.com/integrating-r-with-apache-hadoop/">Integrating R with Apache Hadoop</a></li>
<li><a title="Permanent Link toPlotting App for ggplot2" target="_blank" rel="bookmark" href="http://datascienceplus.com/plotting-app-for-ggplot2/">Plotting App for ggplot2</a></li>
<li><a title="Permanent Link toPerforming SQL selects on R data frames" target="_blank" rel="bookmark" href="http://datascienceplus.com/performing-sql-selects-on-r-data-frames/">Performing SQL selects on R data frames</a></li>
<li><a title="Permanent Link toDeveloping an R Tutorial shiny dashboard app" target="_blank" rel="bookmark" href="http://datascienceplus.com/developing-a-r-tutorial-shiny-dashboard-app/">Developing an R Tutorial shiny dashboard app</a></li>
<li><a title="Permanent Link toStrategies to Speedup R Code" target="_blank" rel="bookmark" href="http://datascienceplus.com/strategies-to-speedup-r-code/">Strategies to Speedup R Code</a></li>
</ol></strong>
<div>
<a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=80tqHBvEaBE:qRRWzMadncA:D7DqB2pKExk"><img src="http://feeds.feedburner.com/~ff/datascienceplus?i=80tqHBvEaBE:qRRWzMadncA:D7DqB2pKExk" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=80tqHBvEaBE:qRRWzMadncA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=qj6IDK7rITs" border="0"></a> <a target="_blank" href="http://feeds.feedburner.com/~ff/datascienceplus?a=80tqHBvEaBE:qRRWzMadncA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/datascienceplus?d=yIl2AUoC8zA" border="0"></a>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/euro-2016-analytics-whos-playing-the-toughest-game/"></div></div><div><a data-url="http://www.r-bloggers.com/euro-2016-analytics-whos-playing-the-toughest-game/" data-counturl="http://www.r-bloggers.com/euro-2016-analytics-whos-playing-the-toughest-game/" data-count="vertical" data-text="Euro 2016 analytics: Who’s playing the toughest game?" 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="http://datascienceplus.com/euro-2016-analytics-whos-playing-the-toughest-game/"> DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/n2Rj0EHzcG4"></a></div>