-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorldmapcup2&3&4.Rmd
87 lines (78 loc) · 3.75 KB
/
Worldmapcup2&3&4.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
---
title: "Leitner et al. 2010"
author: "test"
date: "22 July 2018"
output: html_document
---
```{r}
# create list of countries who qualified and by the right name used by the SpatialPolygonsDataFrame
# list of countries who qualified 32 - England = United Kingdom Serbia = Republic of Serbia
qualified <- c('Spain','Nigeria','Egypt','Costa Rica','Japan','Tunisia', 'France','Switzerland','Portugal','Russia','Colombia','Peru','Senegal', 'United Kingdom','Denmark','Brazil','Germany','Iran (Islamic Republic of)','Korea, Republic of', 'Morocco', 'Belgium','Croatia','Saudi Arabia', 'Uruguay', 'Iceland', 'Argentina', 'Panama','Poland','Australia','Mexico','Sweden','Serbia')
# knocked out countries... Korea Republic - South Korea
knocked_out <- c('Saudi Arabia', 'Egypt', 'Iran (Islamic Republic of)', 'Morocco', 'Peru', 'Australia', 'Nigeria', 'Iceland', 'Serbia', 'Costa Rica', 'Korea, Republic of', 'Germany','Tunisia', 'Panama','Senegal','Poland')
```
```{r}
# load library with shapefile
library(maptools)
# load shapefile
data("wrld_simpl")
names(wrld_simpl)
wrld_simpl <- wrld_simpl[,c(5)]
names(wrld_simpl)[1] <- 'Country'
## create 2 df - one with countries still in and knocked out
# apply values to countries in the wc
countries <- wrld_simpl[which(wrld_simpl$Country %in% qualified),]
# 32 countries in the world cup
nrow(countries)
```
```{r}
#Probabilistic forecasts for the 2018 FIFA World Cup based on the bookmaker consensus model - https://econpapers.repec.org/paper/innwpaper/2018-09.html
# read data from: https://eeecon.uibk.ac.at/~zeileis/news/fifa2018/
odds <- read.csv('https://eeecon.uibk.ac.at/~zeileis/assets/posts/2018-05-30-fifa2018/fifa2018.csv',header=F)[-1,] # remove header & delete first row (old header)
# identify what columns to keep
names(odds)
# subset columns - quarter final, semi-final, final,win
odds <- odds[c(1,38,39,40,41)]
# rename columns
names(odds) <- c('Country','Quarter','Semi','Final','Win')
# explore data
head(odds)
# convert to character
odds$Country <- as.character(odds$Country)
# change row values
odds$Country[odds$Country == "England"] <- "United Kingdom"
odds$Country[odds$Country == "South Korea"] <- "Korea, Republic of"
odds$Country[odds$Country == "Iran"] <- "Iran (Islamic Republic of)"
```
```{r}
# create new df with world cup wins
Country <- c('Brazil','Germany','Uruguay','Argentina','United Kingdom','France','Spain','Nigeria','Egypt','Costa Rica','Japan','Tunisia', 'Switzerland','Portugal','Russia','Colombia','Peru','Senegal','Denmark','Iran (Islamic Republic of)','Korea, Republic of', 'Morocco', 'Belgium','Croatia','Saudi Arabia', 'Iceland', 'Panama','Poland','Australia','Mexico','Sweden','Serbia')
# apply wc wins
WC_wins <- c(5,4,2,2,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
# combine country and wc_wins characters to form df
df <- data.frame(Country,WC_wins)
```
```{r}
# merge countries df with probability df
d <- merge(countries,odds,by='Country')
# merge previous df with number of wc wins
d <- merge(d,df,by='Country')
# apply function for making columns numeric
d@data[,c('Quarter','Semi','Final','Win','WC_wins')] <- as.numeric(as.character(unlist(d@data[,c('Quarter','Semi','Final','Win', 'WC_wins')])))
# explore structure
head(d@data)
# number of rows
nrow(d)
#check map projection - mapbox requires lat&lng
proj4string(d)
# package for exporting as shapefile
library(rgdal)
# write spatial data as shapefile
writeOGR(d, ".", "mapbox", driver="ESRI Shapefile") #also you were missing the
# get colours based on the red sequential colour scheme
library(RColorBrewer)
display.brewer.pal(7,"Reds")
# library for defining class intervals for choropleth mapping
library(classInt)
n <- classIntervals(n=7, d$Win, style='quantile')
```