Direkt zum Hauptbereich

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-script:
#load the data (of course it has to be in the working directory...)
dfn=read.csv("FrackTweets.csv")

#remove the text-column
dfn=dfn[,-1]

#create 500 random numbers to allocate test and training set
samp=sample(1000,500)

#and now run the k-nearest-neigbors
library(class)
bof=knn(train=dfn[samp,-dim(dfn)[2]], test=dfn[-samp,-dim(dfn)[2]], cl=dfn$codes[samp], k=32)
We used half of the data for training and the other half for testing. 
How good is this classification?
The package gmodels has a nice function for cross-tables:
library(gmodels)
CrossTable(x=dfn$codes[-samp], y=bof, prop.chisq=F)

Total Observations in Table:  500 

 
                 | bof 
dfn$codes[-samp] |         c |         n |        nr | Row Total | 
-----------------|-----------|-----------|-----------|-----------|
               c |       216 |        11 |         3 |       230 | 
                 |     0.939 |     0.048 |     0.013 |     0.460 | 
                 |     0.457 |     0.550 |     0.429 |           | 
                 |     0.432 |     0.022 |     0.006 |           | 
-----------------|-----------|-----------|-----------|-----------|
               n |       114 |         5 |         0 |       119 | 
                 |     0.958 |     0.042 |     0.000 |     0.238 | 
                 |     0.241 |     0.250 |     0.000 |           | 
                 |     0.228 |     0.010 |     0.000 |           | 
-----------------|-----------|-----------|-----------|-----------|
              nr |        97 |         4 |         3 |       104 | 
                 |     0.933 |     0.038 |     0.029 |     0.208 | 
                 |     0.205 |     0.200 |     0.429 |           | 
                 |     0.194 |     0.008 |     0.006 |           | 
-----------------|-----------|-----------|-----------|-----------|
               p |        46 |         0 |         1 |        47 | 
                 |     0.979 |     0.000 |     0.021 |     0.094 | 
                 |     0.097 |     0.000 |     0.143 |           | 
                 |     0.092 |     0.000 |     0.002 |           | 
-----------------|-----------|-----------|-----------|-----------|
    Column Total |       473 |        20 |         7 |       500 | 
                 |     0.946 |     0.040 |     0.014 |           | 
-----------------|-----------|-----------|-----------|-----------|
 

What do think? Is it a good classifyer?

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

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

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