FiveThirtyEight predicted who would win the world cup daily https://github.com/fivethirtyeight/data. I wanted to know who favourites changed over time. This is talking FiveThirtyEight’s predicitions on whether each team would win at each point of the competition.

setwd("~/Dropbox/Personal/fun with code/football_predictions")
library(WDI)
## Loading required package: RJSONIO
library(rCharts)
library(plyr)
library(tables)
## Loading required package: Hmisc
## Loading required package: grid
## Loading required package: lattice
## Loading required package: survival
## Loading required package: splines
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## 
## The following objects are masked from 'package:plyr':
## 
##     is.discrete, summarize
## 
## The following objects are masked from 'package:base':
## 
##     format.pval, round.POSIXt, trunc.POSIXt, units
# empty dataset
  data <- NULL
# Get list of files into a vector
  files <- list.files(paste0(getwd(),"/world-cup-predictions"))
# Set up loop to make the data
  for (i in 1:length(files)) {  
    # Move it into a data file
      data_temp <- read.csv(paste0(getwd(), 
                                   "/world-cup-predictions/",
                                   files[i]), 
                            header = TRUE)
    # Clean data file
      data_temp$date <- unlist(strsplit(files[i], split='-', fixed=TRUE))[2]
      data_temp$date <- as.Date(data_temp$date, "%Y%m%d")
      data <- rbind(data, data_temp)
    }

# Win!
  data$win <- round(data$win*100,1)
# Days before the final
  data$days <- as.numeric(data$date - 16265)
# Just needed data
  data_raw <- data
  data <- subset(data, select=c("country","country_id","win","days"))

Plot_all <- nPlot(win ~ days, 
  data = data, 
  group = "country",
  type = "lineChart")

# germany or brazil as defaults

  data$bra_or_ger <- as.logical(data$country_id != "BRA")
  data$bra_or_ger[data$country_id == "GER"] <- FALSE
  bra_or_ger <- data[1:length(unique(data$country)),"bra_or_ger"] 
  
  Plot_all$yAxis(axisLabel = "Chance of winning (%)", width = 62)
  
  Plot_all$xAxis(axisLabel = "Days before the 2014 World Cup final")
  
  Plot_all$set(disabled = bra_or_ger)
  
  Plot_all$chart(tooltipContent = "#! function(key, x, y){
          return '<h3>' + key + '</h3>' + 
          '<p>' + y + '% chance ' + x + ' days before the final' + '</p>'
          } !#")
  Plot_all$save(destfile = "plot.html", cdn = T)
tabular((country + 1) ~ (n=1) + Format(digits=2)*(win)*(max + median+ mean), data=data )
##                                                  
##                              win                 
##  country                n    max    median mean  
##  Algeria                  84  0.300  0.000  0.038
##  Argentina                84 39.000 15.000 16.568
##  Australia                84  0.000  0.000  0.000
##  Belgium                  84  3.200  1.800  1.754
##  Bosnia and Herzegovina   84  1.000  0.000  0.385
##  Brazil                   84 55.700 39.700 37.767
##  Cameroon                 84  0.100  0.000  0.006
##  Chile                    84  7.200  4.100  3.363
##  Colombia                 84  8.900  5.250  4.664
##  Costa Rica               84  1.400  0.600  0.658
##  Croatia                  84  0.600  0.150  0.182
##  Ecuador                  84  1.000  0.300  0.319
##  England                  84  1.800  0.000  0.345
##  France                   84  8.400  5.300  4.987
##  Germany                  84 63.900 11.500 15.705
##  Ghana                    84  0.400  0.100  0.137
##  Greece                   84  0.600  0.100  0.140
##  Honduras                 84  0.000  0.000  0.000
##  Iran                     84  0.000  0.000  0.000
##  Italy                    84  1.100  0.400  0.408
##  Ivory Coast              84  1.500  0.700  0.627
##  Japan                    84  0.100  0.000  0.015
##  Mexico                   84  1.400  0.500  0.482
##  Netherlands              84 19.500  7.100  8.026
##  Nigeria                  84  0.500  0.100  0.162
##  Portugal                 84  1.100  0.300  0.337
##  Russia                   84  0.600  0.350  0.277
##  South Korea              84  0.100  0.100  0.051
##  Spain                    84  7.700  0.000  0.814
##  Switzerland              84  1.200  0.700  0.598
##  Uruguay                  84  2.000  0.300  0.555
##  USA                      84  1.200  0.600  0.575
##  All                    2688 63.900  0.200  3.123