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

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...

Der Nutzerismus: Eine Ideologie mit totalitärem Potential

Ich glaube, dass wir derzeit den Aufstieg einer Ideologie erleben, die ich Nutzerismus nennen möchte. Hannah Arendt hat darauf hingewiesen, dass jede Ideologie zu einem totalitaristischen Regime führen kann und es gibt ernste Anzeichen, dass dies auch für den Nutzerismus gilt.  Was ist der Nutzerismus? Wie bei jeder Ideologie ist der Kerngedanke sehr einfach: Im Prinzip gibt es für alle gesellschaftlichen Probleme eine technische Lösung. Leider wenden die Menschen die richtigen Technologien nicht an. Sie nehmen ihre Rolle als Nutzer nicht wahr. Es geht dem Nutzerismus also um das Zusammenspiel von Mensch und Technik, allerdings immer wieder aus der gleichen Perspektive. Die Technik kommt vor als potentielle Lösung eines gesellschaftlichen Problems. Eventuell fehlt die perfekte Lösung noch, aber das ist dann als Auftrag an die Wissenschaft und die Ingenieure zu verstehen. Dieser Technikglaube hat etwas sehr Naives. Er abstrahiert zum Beispiel von allen Interessen, für die Technolog...

#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-...