Direkt zum Hauptbereich

RandomForest to predict the Soccer World Cup

I now present the R-code for the random forest prediction (the group-variable was added here):
df=read.csv("WorldCup2014Test.csv")
WC=read.csv("WorldCup2014.csv")

#I am setting all NAs to 0. This might be a bad idea, but it works.
df[is.na(df)]=0

#We want to run a randomforest as classifier
#First, we code a response variable Y with "w" (win), "d" (draw)
#and "l" (loss)
df$Y=ifelse(df[,4]-df[,3]>0,"w",ifelse(df[,4]-df[,3]==0,"d","l"))
df$Y=as.factor(df$Y)

library(randomForest)
#We want to auto tune the random forest: This requires
#response and predictors to be in a matrix
y=as.matrix(df$Y)
x=as.matrix(df[,-c(1,2,3,4,5,6,76)])
rf.tune = tuneRF(x=x,y=as.factor(y), type="pob", doBest=T)
#The random forest includes a confusion matrix
rf.tune$confusion
#The model is poor in draws, but quite good in predicting wins

#Let's predict the World Cup!
WC[is.na(WC)]=0
xte=as.matrix(WC[,-c(1,2,3,4,5,6,76,77)])
pred.rf= predict(rf.tune, xte)

#We can also get the probabilities...
WC.rf=predict(rf.tune, xte, type="prob")

#Now we can build a new data-frame with Teams, Group and
#probabilities:
WCres.rf=cbind(WC[,c(1,2,76,77)],WC.rf)
View(WCres.rf)

#Transform the teams to factors
WCres.rf[,1]=as.factor(WCres.rf[,1])
WCres.rf[,2]=as.factor(WCres.rf[,2])

#In the data home or away matters. But this is not meaningfull in the WC.
#The next data.frame tries to work around this problem:
WCres1 = WCres.rf[,c(1,2,3,7)]
colnames(WCres1)=c("Team1", "Team2","Group", "Prob")
WCres2 = WCres.rf[,c(1,2,4,6)]
colnames(WCres2)=c("Team1", "Team2", "Group", "Prob")
WCres=rbind(WCres1,WCres2)
write.csv(WCres,"WCWinningProbs.csv", row.names=F)
#And this gives us a plot of the winning probabilities for all teams!
png("WCrfBP.png", 1400,600)
plot(WCres$Prob~WCres$Team)
dev.off()

# And if we take only games from teams out of the same group
# we have the winning probabilities for the group-phase.
WCresGroups =subset(WCres.rf, Group1==Group2)
WCres1 = WCresGroups[,c(1,3,7)]
colnames(WCres1)=c("Team", "Group", "Prob")
WCres2 = WCresGroup[,c(2,4,6)]
colnames(WCres2)=c("Team", "Group", "Prob")
WCres=rbind(WCres1,WCres2)
png("WCrfGroupBP.png", 1400,600)
plot(WCres$Prob~WCres$Team)
dev.off()

Kommentare

Beliebte Posts aus diesem Blog

#RTutorial: Using R to Harvest the Twitter STREAM API

Initializing the Twitter API In this tutorial, the so called STREAMING-API from Twitter is used. This API provides real-time access to Twitter, so the results are dependent from what is actually going on, right now. Before we start, we have to initialize the Twitter-API. To use the Twitter API, a consumer key and consumer secret is required. Therefore, you have to register as a developer who is creating a Twitter app. Create a Twitter account and then sign in at https://apps.twitter.com/. The account has to be verified with a phone number. This can be done on the Twitter webpage in the account settings. Fill in name, description and any valid URL with leading “http://”. It is important NOT to provide any call-back URL, because otherwise the registration from R will not function. After this, you can see a summary of your newly created app with a link to “manage keys and access tokens”. The consumer key and consumer secret that can be found there have to be copied into the following R-...

Deep-Dive Impfeffektivität: Eine kritische Datenanalyse der RKI-Berechnungen / Teil 1: Die Methode

Die Einschätzung, wie effektiv die COVID-Impfung ist, ist eine der politisch relevantesten Kennzahlen derzeit. Insbesondere für die Einschätzungen der Angemessenheit einr Impfpflicht ist diese Zahl extrem wichtig. In der Vergangenheit hat sich immer wieder gezeigt, dass die Berechnungen des RKI nicht in jeder Hinsicht eindeutig sind, sondern auf vielen Annahmen beruhen, die man auch kritisch hinterfragen kann und muss. Für die politische Datenwissenschaft ist es daher essenziell, diese Berechnungen nachvollziehbar zu machen. In diesem Beitrag wird das methodische Vorgehen des RKI zur Berechnung der Impfeffektivität analysiert. Die Informationen dazu entstammen den RKI-Wochenberichten .  In einem zweiten Teil habe ich die konkreten Berechnungen des RKI so weit wie möglich rekonstruiert und kann daher zeigen, wie stark die Ergebnisse schwanken, wenn Annahmen leicht verändert werden. Meine Erkenntnisse aus der folgenden Analyse: Das RKI verwendet zur Berechnung der Impfeffektivität di...

The Social Science One Facebook Cooperation: A Systemic Failure

This blogpost is a detailed background statement to this comment published in Nature at https://www.nature.com/articles/d41586-020-00828-5 . Fast-Read The following text argues that the Social Science One initiative that is celebrated by Facebook as success is an systemic failure. Privacy concerns are instrumentalized to withhold data from independent researchers while similar data is still available to private companies. The hurdles for researchers at Social Science One are set in a way that it is nearly impossible to study the effect of social media on elections and democracy. This fits to the strategic interest of Facebook to play cooperative but to control the outcomes of research. Social Science One is structured in a way that it is easy for Facebook to instrumentalize this organization. We end with policy recommendations. An appendix shows that the dataset delivered by Facebook is more or less useless. Background Facebook has announced the release of a dataset that is shar...