Direkt zum Hauptbereich

Posts

Es werden Posts vom Juni, 2014 angezeigt.

#NRW - Tweets aus Nordrhein-Westfalen in #R saugen

In diesem Post zeige ich ein kurzes Beispiel, wie man das streamR-Package nutzen kann, um Tweets aus NRW zu speichern. Voraussetzung ist, dass man ein consumer secret und einen consumerkey von Twitter hat. Das Beispiel aus der streamR-Doku ist im Prinzip richtig. Nur muss man inzwischen https anstelle von http verwenden. Hier also "stuff that works", wie Guy Clark sagen würde. Wichtig ist, dass man eine gültige cacert.pem Datei im Arbeitsverzeichnis hat. Eine entsprechende Datei findet sich hier .  #load libraries library(streamR) library(ROAuth) library(twitteR) requestURL <- "https://api.twitter.com/oauth/request_token" accessURL <- "https://api.twitter.com/oauth/access_token" authURL <- "https://api.twitter.com/oauth/authorize" #own consumerKey and Secret is needed! consumerKey <- "xxxxxyyyyyzzzzzz" consumerSecret <- "xxxxxxyyyyyzzzzzzz111111222222" Cred <- OAuthFactory$new(consumerKey=consumerKe

Love your #nearest neighbor

In this post the k-nearest neighbors algorithm is used to classify Twitter-data. We have 1000 tweets about fracking which are labeld "c" = contra fracking, "p" = pro fracking, "n" = neutral to fracking, "nr" = not related to fracking. The data can be found here . The data is already cleaned: Missing values have been coded as -9999, than all numeric variables have been normalized with this R-function: normalize = function(x) (x-min(x))/(max(x)-min(x)) Dates are transformed with, as can be seen here . The k-nearest-neighbors algorithm is a "lazzy" learner. It does not make any assumtpions about any distributions (non-parametric approach!). It simply calculates the distance in the feature space. Therefore, it does not "learn" anything because it won't come up with any abstraction. We want to test, if k-nearest-neighbors can identify the tweets just by the meta-data (without looking at the text...). Here is the R-scr

#Datamining-Seminar Projekte

Liebe Studierende, bitte überlegt euch für Montag ein Projekt, das ihr gerne mit Data-Mining bearbeiten wollt. Außerdem solltet ihr euch schon einmal mit möglichen Daten vertraut machen. Hier ein paar Anregungen, wo Sozialwissenschaftler Datensätze herbekommen können: http://www.policyagendas.org/ Hervorragende Datensammlung zu US-Budgets und politischen Aufmerksamkeitsvariablen. Ähnliche Daten für andere Länder gibt es hier: http://www.comparativeagendas.info/ Daten der Industrienationen hat die OECD: http://stats.oecd.org/ Europadaten gibt's bei Eurostat: http://epp.eurostat.ec.europa.eu/portal/page/portal/statistics/search_database Deutschlanddaten bei destatis: https://www-genesis.destatis.de/genesis/online Regionale Daten aus Deutschland: https://www.regionalstatistik.de/genesis/online/logon Die US-Regierung hat eine open-data-Initiative: http://data.gov/ und Deutschland auch (allerdings mit viel weniger Daten): https://govdata.de/ Daten zur ameri

Group Games Predictions

And the graph for the group games:

New Graphs Soccer Prediction

Here are some updated graphs of the random forest prediction: The lines show which teams will win more than 50 per cent (33 per cent) of their matches. Of course, this means a lot of randomness. As we have seen, even Spain does not win every match... and if you loose the wrong match...

Rank-Order of Soccer World Cup Predictions

In this post I start to present some simple graphs to visualize the results from the random forest prediction. Here, we see the mean of the winning probability of all teams in increasing order:

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

Spain wins the World Cup… probably

For all possible combinations with two teams I calculated the probability for each team to win. The model is a tuned random forest for classification of “win”, “draw”, or “loose”. The test data is the whole data from codecentrics (more or less). The plot shows the mean winning probability for each team but also shows the variance in the predictions. For the second plot only the games of the group-phase were used. So this plot shows the group-winners.

DataMining the Soccer World Cup 2014

To predict the results of sport events is always fun - and an enormous challenge. Especially in a tournament like the soccer world cup: Teams compete who play very rarely against each other. In soccer, luck is one of the most important factors (which means most of the data available represents noise…) - especially in a knock out tournament… So what could be more hopeless and more fun than using data-mining to predict the results. The codecentric guys did an excellent job in providing data on past tournaments. But matchday is coming soon and we need a dataset with the actual tournament to apply a model. And this is what I did. The R-script creates a new dataset with all possible combinations of teams playing against each other in the 2014 world cup. And it reconstructs the actual values of the codecentric-features for these matches. So, if you build a model on the codecentric data, you can easily apply this new dataset to predict new values. In the following posts, I will demons

#Fraccidents

Analyse der Twittermeldungen zum Thema „Fracking“ In einem interdisziplinären Team am FoKoS von Informatikern, Politikwissenschaftlern und Ökonometrikern, das ich mit Jun. Prof. Marcin Grzegorzek   leite, untersuchen wir Twitter-Meldungen zum Thema Fracking. Die Projektgruppe arbeitet daran, diese Tweets automatisch zu klassifizieren: Wer vertritt eine Pro-Fracking-Position, wer ist gegen Fracking? Wenn es gelingt einen Algorithmus zu entwickeln, der diese Frage automatisch beantworten kann, ließen sich daraus viele Erkenntnisse ableiten, die ansonsten verborgen blieben. Denn die Datenmenge übersteigt längst das, was ein Mensch lesen kann:   Jede Woche fallen ca. 100.000 Tweets an, die sich mit Fracking auseinandersetzen. Die meisten davon in den USA, aber auch zunehmend in Europa. Interessanterweise lässt sich die Einstellung der Twitter-User nicht über herkömmliche Textanalyseverfahren (Text-Mining und Sentiment-Analyse) ermitteln. Mehr dazu hier .  Denn dieser Ansatz b

Lösungen zu Übungen I

... und hier ist das Übungsscript mit den entsprechenden Lösungen eingefügt: library(MASS) df = Boston ?Boston summary(df) # Aufgabe: Fitte eine lineare Regression in der die Kriminalitätsrate # (crim) vom Anteil der schwarzen Bevölkerung (black) abhängt! fit = lm(crim~black, data=df) summary(fit) # Gibt es einen Zusammenhang? # Wie stark ist der Zusammenhang und ist er signifikant? # Erstelle einen Scatterplot und füge eine Regressionsgrade hinzu! plot(df$black, df$crim) abline(fit) # Wie interpretierst du den Plot? # Multi-Linear-Regression # Fitte ein Modell für crim mit allen möglichen Predictoren! fit2 = lm(crim~., data=df) summary(fit2) # Erstelle Diagnoseplots und interpretiere sie! par(mfrow=c(2,2)) plot(fit2) # Lade die Leaps-Bibliothek, führe die folgenden Befehle aus und # identifiziere das beste Modell nach dem Cp-Wert! # requires "leaps" library(leaps) X = df[,-1] y = df[,1] out = summary(regsubsets(X, y, nbest=2, nvmax=ncol(X)))