Direkt zum Hauptbereich

#k-means #clustering



Hier kommt das versprochene Beispiel zu k-means Clustering. Angelehnt ist es an Ledolter, Johannes. 2013. Business analytics and data mining with R. Hoboken, NewJersey: Wiley.


df = read.csv("http://www.biz.uiowa.edu/faculty/jledolter/DataMining/protein.csv")
View(df)
#what is the right number of clusters?
#One idea is to look at the sum squared error (SSE) for 
#each possible number of clusters.
 
#Formula to calculate SSE:
wss <- (nrow(df)-1)*sum(apply(df[,-1],2,var))
 
for (i in 2:20) wss[i] <- sum(kmeans(df[,-1],centers=i)$withinss)
plot(1:20, wss, type="b", xlab="Number of Clusters",
     ylab="Within groups sum of squares") 
 #The "elbow" now indicates the optimal number of clusters. The
#idea is that at a certain point additional clusters do not 
#reduce the SSE much...
#In this case, we might decide to take 5 clusters.
 
#But there are many more and more sophisticated ways:
#The NbClust package provides 30 indices to determine the number 
#of clusters in a dataset.
 
#requires :: NbClust
library(NbClust)
set.seed(123)
nb <- NbClust(df[,-1], method = "kmeans")
#Warning messages show that Beale-Test failed. 
#Too few observations?
#NbClust suggests 2 (or 3) Clusters. We can plot the different
#results:
par(mfrow=c(1,1)) #NbClust had changed this parameter, so
#we set it back...
hist(nb$Best.nc[1,], breaks = max(na.omit(nb$Best.nc[1,])))
#Changing set.seed will lead to different resulsts! Still
#some randomness in the method.
 
#kmeans clustering set.seed(1234) clus = kmeans(df[,-1], 2) clus o=order(clus$cluster) data.frame(df$Country[o], clus$cluster[o])   #plotting the cluster plot(df$RedMeat, df$WhiteMeat, type="n", xlab="Red Meat", ylab="White Meat") text(x=df$RedMeat, y=df$White, labels=df$Country, col=clus$cluster+1) #The plot shows the only two variables, but all were used #for clustering! 
  
#same with 3 clusters:
clus = kmeans(df[,-1], 3)
plot(df$RedMeat, df$WhiteMeat, type="n", xlab="Red Meat", 
     ylab="White Meat")
text(x=df$RedMeat, y=df$White, labels=df$Country,
     col=clus$cluster+1)
 
#A nicer way to plot (requires::fpc)
rownames(df)=df$Country
library(fpc)
clusplot(df[,-1], clus$cluster, color=T, shade=F, 
         labels=2, lines=0, cex=.8, main="Protein Clusters") 
 
 #The plot takes the first two principle components as 
#dimensions. This is not the same as the variables!
#What are principle components and what can I do with them?
#Wait for next post!
Created by Pretty R at inside-R.org

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