{"id":927,"date":"2014-12-02T12:34:17","date_gmt":"2014-12-02T12:34:17","guid":{"rendered":"http:\/\/opisthokonta.net\/?p=927"},"modified":"2015-08-22T18:19:02","modified_gmt":"2015-08-22T18:19:02","slug":"the-dixon-coles-model-for-predicting-football-matches-in-r-part-3","status":"publish","type":"post","link":"https:\/\/opisthokonta.net\/?p=927","title":{"rendered":"The Dixon-Coles model for predicting football matches in R (part 3)"},"content":{"rendered":"<p>About a moth ago Martin Eastwood of the <a href=\"http:\/\/pena.lt\/y\/2014\/11\/02\/predicting-football-using-r\/\">pena.lt\/y blog<\/a> put up some slides from a talk he gave about predicting football results in R. He presented in detail the independent Poisson regression model, and how to implement it. He also briefly mentioned and showed the bivariate adjustments in the Dixon-Coles model. I was curious about how he had implemented it since I had just finished my own implementation. In the comments he said that he used a two-stage approach, first estimating the attack and defense parameters using the independent Poisson model, and then estimating the rho parameter by it self. <\/p>\n<p>This method may be less accurate than fitting the complete model, but it will probably be more accurate than the independent Poisson model. It is without a doubt faster and easier to implement.<\/p>\n<p>We start with loading the data, and then making a new <em>data.frame <\/em>that contains two rows per match, as described in my <a href=\"https:\/\/opisthokonta.net\/?p=276\">post about the independent Poisson model<\/a>. <\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\ndta &lt;- read.csv('FAPL1112.csv')\r\n\r\n# Data formated for the independent model\r\n# Store in new variable, we need the data in original format later\r\ndta.indep &lt;- data.frame(Team=as.factor(c(as.character(dta$HomeTeam),\r\n                            as.character(dta$AwayTeam))),\r\n                            Opponent=as.factor(c(as.character(dta$AwayTeam),\r\n                            as.character(dta$HomeTeam))),\r\n                            Goals=c(dta$FTHG, dta$FTAG),\r\n                            Home=c(rep(1, dim(dta)[1]), rep(0, dim(dta)[1])))\r\n\r\n<\/pre>\n<p>Now fit the model:<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nm &lt;- glm(Goals ~ Home + Team + Opponent, data=dta.indep, family=poisson())\r\n<\/pre>\n<p>Since we now have estimated the attack, defense and home parameters we can use the built-in functions in R to calculate the expected home and away scores (lambda and mu). <\/p>\n<p>To calculate lambda and mu, we use the <em>fitted<\/em> function. I organized the data so that all the rows with the goals scored by the home team comes before all the rows with the goals by the away teams. Whats more, the match in the first row in the home team part corresponds to the match in the first row in the away team part, so it is easy to get the corresponding expectations correct. <\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nexpected &lt;- fitted(m)\r\nhome.expected &lt;- expected[1:nrow(dta)]\r\naway.expected &lt;- expected[(nrow(dta)+1):(nrow(dta)*2)]\r\n<\/pre>\n<p>To estimate the rho parameter we can use the <em>tau<\/em> and <em>DClogLik<\/em> function we defined in <a href=\"https:\/\/opisthokonta.net\/?p=890\">part 1<\/a>. We just wrap it inside a function we pass to the built in optimizer in R:<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nDCoptimRhoFn &lt;- function(par){\r\n  rho &lt;- par[1]\r\n  DClogLik(dta$FTHG, dta$FTAG, home.expected, away.expected, rho)\r\n}\r\n\r\nres &lt;- optim(par=c(0.1), fn=DCoptimRhoFn, control=list(fnscale=-1), method='BFGS')\r\n<\/pre>\n<p>The optimization finishes in an instant. As before we get the parameter values by looking at res$par. The estimated rho parameter is -0.126, which is reassuringly not that different from the -0.134 we got from the full model. This is is also about the same values Justin Worrall gets at his <a href=\"http:\/\/www.sportshacker.net\/posts\/dixon_coles_adjustment.html\">sportshacker.net<\/a> blog. <\/p>\n<p>To make predictions we can reuse most of the code from part 2. The only substantial difference is how we calculate the expected goals, which is a bit simpler this time:<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Expected goals home\r\nlambda &lt;- predict(m, data.frame(Home=1, Team='Bolton', Opponent='Blackburn'), type='response')\r\n\r\n# Expected goals away\r\nmu &lt;- predict(m, data.frame(Home=0, Team='Blackburn', Opponent='Bolton'), type='response')\r\n<\/pre>\n<p>This two-stage approach is much faster and simpler. We don&#8217;t have to manually create the design matrices and use matrix algebra to calculate the expected scores. We also don&#8217;t have to write as much code to keep track of all the parameters. I haven&#8217;t really compared all the different models against each other, so I can&#8217;t say which one makes the best predictions, but my guess is that this two-stage approach gives results similar to the fully specified Dixon-Coles model.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>About a moth ago Martin Eastwood of the pena.lt\/y blog put up some slides from a talk he gave about predicting football results in R. He presented in detail the independent Poisson regression model, and how to implement it. He &hellip; <a href=\"https:\/\/opisthokonta.net\/?p=927\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48,5,6],"tags":[],"class_list":["post-927","post","type-post","status-publish","format-standard","hentry","category-dixon-coles-model","category-r","category-soccer"],"_links":{"self":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts\/927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=927"}],"version-history":[{"count":15,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts\/927\/revisions"}],"predecessor-version":[{"id":955,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=\/wp\/v2\/posts\/927\/revisions\/955"}],"wp:attachment":[{"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opisthokonta.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}