Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In part one and part two of Modeling Match Results in La Liga Using a Hierarchical Bayesian Poisson Model I developed a model for the number of goals in football matches from five seasons of La Liga, the premier Spanish football league. I’m now reasonably happy with the model and want to use it to rank the teams in La Liga and to predict the outcome of future matches!

## Ranking the Teams of La Liga

We’ll start by ranking the teams of La Liga using the estimated skill parameters from the 2012/2013 season. The values of the skill parameters are difficult to interpret as they are relative to the skill of the team that had its skill parameter “anchored” at zero. To put them on a more interpretable scale I’ll first zero center the skill parameters by subtracting the mean skill of all teams, I then add the home baseline and exponentiate the resulting values. These rescaled skill parameters are now on the scale of expected number of goals when playing home team. Below is a caterpillar plot of the median of the rescaled skill parameters together with the 68 % and 95 % credible intervals. The plot is ordered according to the median skill and thus also gives the ranking of the teams.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #408080; font-style: italic"># The ranking of the teams for the 2012/13 season.</span>
team_skill <span style="color: #666666"><-</span> ms3[, str_detect(string <span style="color: #666666">=</span> colnames(ms3), <span style="color: #BA2121">"skill\\[5,"</span>)]
team_skill <span style="color: #666666"><-</span> (team_skill <span style="color: #666666">-</span> rowMeans(team_skill)) <span style="color: #666666">+</span> ms3[, <span style="color: #BA2121">"home_baseline[5]"</span>]
team_skill <span style="color: #666666"><-</span> exp(team_skill)
colnames(team_skill) <span style="color: #666666"><-</span> teams
team_skill <span style="color: #666666"><-</span> team_skill[, order(colMeans(team_skill), decreasing <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">T</span>)]
par(mar <span style="color: #666666">=</span> c(<span style="color: #666666">2</span>, <span style="color: #666666">0.7</span>, <span style="color: #666666">0.7</span>, <span style="color: #666666">0.7</span>), xaxs <span style="color: #666666">=</span> <span style="color: #BA2121">"i"</span>)
caterplot(team_skill, labels.loc <span style="color: #666666">=</span> <span style="color: #BA2121">"above"</span>, val.lim <span style="color: #666666">=</span> c(<span style="color: #666666">0.7</span>, <span style="color: #666666">3.8</span>))
</pre></div>

Two teams are clearly ahead of the rest, FC Barcelona and Real Madrid CF. Let’s look at the credible difference between the two teams.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">plotPost(team_skill[, <span style="color: #BA2121">"FC Barcelona"</span>] <span style="color: #666666">-</span> team_skill[, <span style="color: #BA2121">"Real Madrid CF"</span>], compVal <span style="color: #666666">=</span> <span style="color: #666666">0</span>,
xlab <span style="color: #666666">=</span> <span style="color: #BA2121">"← Real Madrid     vs     Barcelona →"</span>)
</pre></div>

FC Barcelona is the better team with a probability of 82 % . Go Barcelona!

## Predicting the End Game of La Liga 2012/2013

In the laliga data set the results of the 50 last games of the 2012/2013 season was missing. Using our model we can now both predict and simulate the outcomes of these 50 games. The R code below calculates a number of measures for each game (both the games with known and unknown outcomes):

• The mode of the simulated number of goals, that is, the most likely number of scored goals. If we were asked to bet on the number of goals in a game this is what we would use.
• The mean of the simulated number of goals, this is our best guess of the average number of goals in a game.
• The most likely match result for each game.
• A random sample from the distributions of credible home scores, away scores and match results. This is how La Liga actually could have played out in an alternative reality…

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">n <span style="color: #666666"><-</span> nrow(ms3)
m3_pred <span style="color: #666666"><-</span> sapply(<span style="color: #666666">1:</span>nrow(laliga), <span style="color: #008000; font-weight: bold">function</span>(i) {
home_team <span style="color: #666666"><-</span> which(teams <span style="color: #666666">==</span> laliga<span style="color: #666666">$</span>HomeTeam[i]) away_team <span style="color: #666666"><-</span> which(teams <span style="color: #666666">==</span> laliga<span style="color: #666666">$</span>AwayTeam[i])
season <span style="color: #666666"><-</span> which(seasons <span style="color: #666666">==</span> laliga<span style="color: #666666">$</span>Season[i]) home_skill <span style="color: #666666"><-</span> ms3[, col_name(<span style="color: #BA2121">"skill"</span>, season, home_team)] away_skill <span style="color: #666666"><-</span> ms3[, col_name(<span style="color: #BA2121">"skill"</span>, season, away_team)] home_baseline <span style="color: #666666"><-</span> ms3[, col_name(<span style="color: #BA2121">"home_baseline"</span>, season)] away_baseline <span style="color: #666666"><-</span> ms3[, col_name(<span style="color: #BA2121">"away_baseline"</span>, season)] home_goals <span style="color: #666666"><-</span> rpois(n, exp(home_baseline <span style="color: #666666">+</span> home_skill <span style="color: #666666">-</span> away_skill)) away_goals <span style="color: #666666"><-</span> rpois(n, exp(away_baseline <span style="color: #666666">+</span> away_skill <span style="color: #666666">-</span> home_skill)) home_goals_table <span style="color: #666666"><-</span> table(home_goals) away_goals_table <span style="color: #666666"><-</span> table(away_goals) match_results <span style="color: #666666"><-</span> sign(home_goals <span style="color: #666666">-</span> away_goals) match_results_table <span style="color: #666666"><-</span> table(match_results) mode_home_goal <span style="color: #666666"><-</span> as.numeric(names(home_goals_table)[ which.max(home_goals_table)]) mode_away_goal <span style="color: #666666"><-</span> as.numeric(names(away_goals_table)[ which.max(away_goals_table)]) match_result <span style="color: #666666"><-</span> as.numeric(names(match_results_table)[which.max(match_results_table)]) rand_i <span style="color: #666666"><-</span> sample(seq_along(home_goals), <span style="color: #666666">1</span>) c(mode_home_goal <span style="color: #666666">=</span> mode_home_goal, mode_away_goal <span style="color: #666666">=</span> mode_away_goal, match_result <span style="color: #666666">=</span> match_result, mean_home_goal <span style="color: #666666">=</span> mean(home_goals), mean_away_goal <span style="color: #666666">=</span> mean(away_goals), rand_home_goal <span style="color: #666666">=</span> home_goals[rand_i], rand_away_goal <span style="color: #666666">=</span> away_goals[rand_i], rand_match_result <span style="color: #666666">=</span> match_results[rand_i]) }) m3_pred <span style="color: #666666"><-</span> t(m3_pred) </pre></div> First lets compare the distribution of the number of goals in the data with the predicted mode, mean and randomized number of goals for all the games (focusing on the number of goals for the home team). First the actual distribution of the number of goals for the home teams. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">hist(laliga<span style="color: #666666">$</span>HomeGoals, breaks <span style="color: #666666">=</span> (<span style="color: #666666">-1:10</span>) <span style="color: #666666">+</span> <span style="color: #666666">0.5</span>, xlim <span style="color: #666666">=</span> c(<span style="color: #666666">-0.5</span>, <span style="color: #666666">10</span>), main <span style="color: #666666">=</span> <span style="color: #BA2121">"Distribution of the number of goals\nscored by a home team in a match."</span>,
xlab <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>)
</pre></div>

This next plot shows the distribution of the modes from the predicted distribution of home goals from each game. That is, what is the most probable outcome, for the home team, in each game.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">hist(m3_pred[, <span style="color: #BA2121">"mode_home_goal"</span>], breaks <span style="color: #666666">=</span> (<span style="color: #666666">-1:10</span>) <span style="color: #666666">+</span> <span style="color: #666666">0.5</span>, xlim <span style="color: #666666">=</span> c(<span style="color: #666666">-0.5</span>, <span style="color: #666666">10</span>),
main <span style="color: #666666">=</span> <span style="color: #BA2121">"Distribution of predicted most\nprobable scoreby a home team in\na match."</span>,
xlab <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>)
</pre></div>

For almost all games the single most likely number of goals is one. Actually, if you know nothing about a La Liga game betting on one goal for the home team is 78 % of the times the best bet.

Lest instead look at the distribution of the predicted mean number of home goals in each game.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">hist(m3_pred[, <span style="color: #BA2121">"mean_home_goal"</span>], breaks <span style="color: #666666">=</span> (<span style="color: #666666">-1:10</span>) <span style="color: #666666">+</span> <span style="color: #666666">0.5</span>, xlim <span style="color: #666666">=</span> c(<span style="color: #666666">-0.5</span>, <span style="color: #666666">10</span>),
main <span style="color: #666666">=</span> <span style="color: #BA2121">"Distribution of predicted mean \n score by a home team in a match."</span>,
xlab <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>)
</pre></div>

For most games the expected number of goals are 2. That is, even if your safest bet is one goal you would expect to see around two goals.

The distribution of the mode and the mean number of goals doesn’t look remotely like the actual number of goals. This was not to be expected, we would however expect the distribution of randomized goals (where for each match the number of goals has been randomly drawn from that match’s predicted home goal distribution) to look similar to the actual number of home goals. Looking at the histogram below, this seems to be the case.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">hist(m3_pred[, <span style="color: #BA2121">"rand_home_goal"</span>], breaks <span style="color: #666666">=</span> (<span style="color: #666666">-1:10</span>) <span style="color: #666666">+</span> <span style="color: #666666">0.5</span>, xlim <span style="color: #666666">=</span> c(<span style="color: #666666">-0.5</span>, <span style="color: #666666">10</span>),
main <span style="color: #666666">=</span> <span style="color: #BA2121">"Distribution of randomly draw \n score by a home team in a match."</span>,
xlab <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>)
</pre></div>

We can also look at how well the model predicts the data. This should probably be done using cross validation, but as the number of effective parameters are much smaller than the number of data points a direct comparison should at least give an estimated prediction accuracy in the right ballpark.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">mean(laliga<span style="color: #666666">$</span>HomeGoals <span style="color: #666666">==</span> m3_pred[, <span style="color: #BA2121">"mode_home_goal"</span>], na.rm <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">T</span>) </pre></div> <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #408080; font-style: italic">## [1] 0.3351</span> </pre></div> <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">mean((laliga<span style="color: #666666">$</span>HomeGoals <span style="color: #666666">-</span> m3_pred[, <span style="color: #BA2121">"mean_home_goal"</span>])<span style="color: #666666">^2</span>, na.rm <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">T</span>)
</pre></div>

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #408080; font-style: italic">## [1] 1.452</span>
</pre></div>

So on average the model predicts the correct number of home goals 34 % of the time and guesses the average number of goals with a mean squared error of 1.45 . Now we’ll look at the actual and predicted match outcomes. The graph below shows the match outcomes in the data with 1 being a home win, 0 being a draw and -1 being a win for the away team.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">hist(laliga<span style="color: #666666">$</span>MatchResult, breaks <span style="color: #666666">=</span> (<span style="color: #666666">-2:1</span>) <span style="color: #666666">+</span> <span style="color: #666666">0.5</span>, main <span style="color: #666666">=</span> <span style="color: #BA2121">"Actual match results."</span>, xlab <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>) </pre></div> Now looking at the most probable outcomes of the matches according to the model. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">hist(m3_pred[, <span style="color: #BA2121">"match_result"</span>], breaks <span style="color: #666666">=</span> (<span style="color: #666666">-2:1</span>) <span style="color: #666666">+</span> <span style="color: #666666">0.5</span>, main <span style="color: #666666">=</span> <span style="color: #BA2121">"Predicted match results."</span>, xlab <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>) </pre></div> For almost all matches the safest bet is to bet on the home team. While draws are not uncommon it is never the safest bet. As in the case with the number of home goals, the randomized match outcomes have a distribution similar to the actual match outcomes: <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">hist(m3_pred[, <span style="color: #BA2121">"rand_match_result"</span>], breaks <span style="color: #666666">=</span> (<span style="color: #666666">-2:1</span>) <span style="color: #666666">+</span> <span style="color: #666666">0.5</span>, main <span style="color: #666666">=</span> <span style="color: #BA2121">"Randomized match results."</span>, xlab <span style="color: #666666">=</span> <span style="color: #BA2121">""</span>) </pre></div> <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">mean(laliga<span style="color: #666666">$</span>MatchResult <span style="color: #666666">==</span> m3_pred[, <span style="color: #BA2121">"match_result"</span>], na.rm <span style="color: #666666">=</span> <span style="color: #008000; font-weight: bold">T</span>)
</pre></div>

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #408080; font-style: italic">## [1] 0.5627</span>
</pre></div>

The model predicts the correct match outcome 56 % of the time. Pretty good!

Now that we’ve checked that the model reasonably predicts the La Liga history lets predict the La Liga endgame! The code below displays the predicted mode and mean number of goals for the endgame and the predicted winner of each game.

<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">laliga_forecast <span style="color: #666666"><-</span> laliga[is.na(laliga<span style="color: #666666">$</span>HomeGoals), c(<span style="color: #BA2121">"Season"</span>, <span style="color: #BA2121">"Week"</span>, <span style="color: #BA2121">"HomeTeam"</span>, <span style="color: #BA2121">"AwayTeam"</span>)] m3_forecast <span style="color: #666666"><-</span> m3_pred[is.na(laliga<span style="color: #666666">$</span>HomeGoals), ]
laliga_forecast<span style="color: #666666">$</span>mean_home_goals <span style="color: #666666"><-</span> round(m3_forecast[, <span style="color: #BA2121">"mean_home_goal"</span>], <span style="color: #666666">1</span>) laliga_forecast<span style="color: #666666">$</span>mean_away_goals <span style="color: #666666"><-</span> round(m3_forecast[, <span style="color: #BA2121">"mean_away_goal"</span>], <span style="color: #666666">1</span>)
laliga_forecast<span style="color: #666666">$</span>mode_home_goals <span style="color: #666666"><-</span> m3_forecast[, <span style="color: #BA2121">"mode_home_goal"</span>] laliga_forecast<span style="color: #666666">$</span>mode_away_goals <span style="color: #666666"><-</span> m3_forecast[, <span style="color: #BA2121">"mode_away_goal"</span>]
laliga_forecast<span style="color: #666666">$</span>predicted_winner <span style="color: #666666"><-</span> ifelse(m3_forecast[, <span style="color: #BA2121">"match_result"</span>] <span style="color: #666666">==</span> <span style="color: #666666">1</span>, laliga_forecast<span style="color: #666666">$</span>HomeTeam, ifelse(m3_forecast[, <span style="color: #BA2121">"match_result"</span>] <span style="color: #666666">==</span> <span style="color: #666666">-1</span>,
laliga_forecast<span style="color: #666666"></span>AwayTeam, <span style="color: #BA2121">"Draw"</span>)) rownames(laliga_forecast) <span style="color: #666666"><-</span> <span style="color: #008000; font-weight: bold">NULL</span> print(xtable(laliga_forecast, align <span style="color: #666666">=</span> <span style="color: #BA2121">"cccccccccc"</span>), type <span style="color: #666666">=</span> <span style="color: #BA2121">"html"</span>) </pre></div> While these predictions are good if you want to bet on the likely winner they do not reflect how the actual endgame will play out, e.g., there is not a single draw in the predicted_winner column. So at last lets look at a possible version of the La Liga endgame by displaying the simulated match results calculated earlier. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">laliga_sim <span style="color: #666666"><-</span> laliga[is.na(laliga<span style="color: #666666"></span>HomeGoals), c(<span style="color: #BA2121">"Season"</span>, <span style="color: #BA2121">"Week"</span>, <span style="color: #BA2121">"HomeTeam"</span>,
<span style="color: #BA2121">"AwayTeam"</span>)]
laliga_sim<span style="color: #666666">$</span>home_goals <span style="color: #666666"><-</span> m3_forecast[, <span style="color: #BA2121">"rand_home_goal"</span>] laliga_sim<span style="color: #666666">$</span>away_goals <span style="color: #666666"><-</span> m3_forecast[, <span style="color: #BA2121">"rand_away_goal"</span>]
laliga_sim<span style="color: #666666">$</span>winner <span style="color: #666666"><-</span> ifelse(m3_forecast[, <span style="color: #BA2121">"rand_match_result"</span>] <span style="color: #666666">==</span> <span style="color: #666666">1</span>, laliga_forecast<span style="color: #666666">$</span>HomeTeam,