DEV Community is a community of 606,099 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Clustering in R

Originally published on Wordpress.

Hello, everyone! I've been meaning to get a new blog post out for the past couple of weeks. During that time I've been messing around with clustering. Clustering, or cluster analysis, is a method of data mining that groups similar observations together. Classification and clustering are quite alike, but clustering is more concerned with exploration than actual results.

Note: This post is far from an exhaustive look at all clustering has to offer. Check out this guide for more. I am reading Data Mining by Aggarwal presently, which is very informative.

``````data("iris")
``````
``````##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
``````

For simplicity, we'll use the `iris` dataset. We're going to try to use the numeric data to correctly group the observations by species. There are 50 of each species in the dataset, so ideally we would end up with three clusters of 50.

``````library(ggplot2)
ggplot() +
geom_point(aes(iris\$Sepal.Length, iris\$Sepal.Width, col = iris\$Species))
``````

As you can see, there is already some groupings present. Let's use hierarcical clustering first.

``````iris2 <- iris[,c(1:4)]  #not going to use the `Species` column.
medians <- apply(iris2, 2, median)
iris3 <- scale(iris2, center = medians, scale = mads)

dist <- dist(iris3)
hclust <- hclust(dist, method = 'median')  #there are several methods for hclust
cut <- cutree(hclust, 3)

table(cut)
``````
``````## cut
##  1  2  3
## 49 34 67
``````
``````iris <- cbind(iris, cut)
iris\$cut <- factor(iris\$cut)
levels(iris\$cut) <- c('setosa', 'versicolor', 'virginica')
err <- iris\$Species == iris\$cut
table(err)
``````
``````## err
## FALSE  TRUE
##    38   112
``````
``````ggplot() +
geom_point(aes(iris2\$Sepal.Length, iris2\$Sepal.Width, col = iris\$cut))
``````

Nice groupings here, but it looks like the algorithm has some trouble determining between versicolor and virginica. If we used this information to classify the observations, we'd get an error rate of about .25. Let's try another clustering technique: DBSCAN.

``````library(dbscan)
``````
``````db <- dbscan(iris3, eps = 1, minPts = 20)
table(db\$cluster)
``````
``````##
##  0  1  2
##  5 48 97
``````
``````iris2 <- cbind(iris2, db\$cluster)

iris2\$cluster <- factor(iris2\$`db\$cluster`)

ggplot() +
geom_point(aes(iris2\$Sepal.Length, iris2\$Sepal.Width, col = iris2\$cluster))
``````

DBSCAN classifies points into three different categories: core, border, and noise points on the basis of density. Thus, the versicolor/ virginica cluster is treated as one group. Since our data is not structured in such a way that density is meaningful, DBSCAN is probably not a wise choice here.

Let's look at one last algo: the ROCK. No, not that ROCK.

``````library(cba)
``````
``````distm <- as.matrix(dist)
rock <- rockCluster(distm, 3, theta = .02)
``````
``````## Clustering:
## computing distances ...
## computing clusters ...
``````
``````iris\$rock <- rock\$cl
levels(iris\$rock) <- c('setosa', 'versicolor', 'virginica')

ggplot() +
geom_point(aes(iris2\$Sepal.Length, iris2\$Sepal.Width, col = rock\$cl))
``````

``````err <- (iris\$Species == iris\$rock)
table(err)
``````
``````## err
## FALSE  TRUE
##    24   126
``````

While it may not look like it, the ROCK seems to do the best job at determining clusters in this data - the error rate dropped to 16%. Typically this method is reserved for categorical data, but since we used `dist` it shouldn't cause any problems.

I have been working on a project using some of these (and similar) data mining procedures to explore spatial data and search for distinct groups. While clustering the `iris` data may not be all that meaningful, I think it is illustrative of the power of clustering. I have yet to try higher-dimension clustering techniques, which might perform even better.

Happy clustering,

Kiefer

Discussion (2)

Walker Harrison

Thanks for the walkthrough, Kiefer. I'm always so fascinated by cluster or nearest-neighbor analysis. It seems to accomplish the very difficult -- modeling non-parametric, complex trends -- in such a straightforward way that's usually based on Euclidean distance. Woo!

nihar • Edited

Clustering wines
K-Means
Learn R here: hackr.io/tutorials/learn-r
This first example is to learn to make cluster analysis with R. The library rattle is loaded in order to use the data set wines.

install.packages('rattle')

data(wine, package='rattle')

6 0.34 1.97 6.75 1.05 2.85 1450

In this data set we observe the composition of different wines. Given a set of observations (x1,x2,.,xn)(x1,x2,.,xn), where each observation is a dd-dimensional real vector, kk-means clustering aims to partition the n observations into (k≤nk≤n) S={S1,S2,.,Sk}S={S1,S2,.,Sk} so as to minimize the within-cluster sum of squares (WCSS). In other words, its objective is to find::

argminS∑i=1k∑xj∈Si∥xj−μi∥2
argminS∑i=1k∑xj∈Si‖xj−μi‖2

where μiμi is the mean of points in SiSi. The clustering optimization problem is solved with the function kmeans in R.

wine.stand <- scale(wine[-1]) # To standarize the variables

K-Means

k.means.fit <- kmeans(wine.stand, 3) # k = 3
In k.means.fit are contained all the elements of the cluster output:

attributes(k.means.fit)

Centroids:

k.means.fit\$centers

Clusters:

k.means.fit\$cluster

Cluster size:

k.means.fit\$size

[1] 51 62 65

A fundamental question is how to determine the value of the parameter kk. If we looks at the percentage of variance explained as a function of the number of clusters: One should choose a number of clusters so that adding another cluster doesn’t give much better modeling of the data. More precisely, if one plots the percentage of variance explained by the clusters against the number of clusters, the first clusters will add much information (explain a lot of variance), but at some point the marginal gain will drop, giving an angle in the graph. The number of clusters is chosen at this point, hence the “elbow criterion”.