# Getting started with cluster analysis in R

I have a huge dataset which contains 20 columns and many rows. I have done clustering in SAS, Knime and SPSS, but I am new to R. I have to do clustering on my dataset. I have imported my data into R.

• What are some suggestions for getting started with cluster analysis in R?
-
For reference, this Q was asked on SO where I attempted an answer: stackoverflow.com/questions/5648383/clustering-in-r It would appear the OP has not attempted to improve the quality of their question in the meantime –  Gavin Simpson Apr 15 '11 at 11:26
What kind of clustering method would you like to use? –  nico Apr 15 '11 at 11:27
as u said 3 lines of code is enough for my dataset...could u please mention those 3 lines then directly i wil copy it and paste onto the console... –  user4182 Apr 15 '11 at 11:30
@nico not a particular kind....in sas, knime and spss when i give the dataset that itself groups into clusters people who are having similar characterstics...in the same way i need those clusters here in r language also is it possible to get like same sas, knime and spss?? –  user4182 Apr 15 '11 at 11:32
@sridher Are you incapable of reading my answer? In one of the comments I told you exactly which three lines. But you can't just copy and paste it in, because I didn't have your data so used my own code/data and the objects don't have the same names as yours. All of this is included in my answer and the comments if you bother to look. –  Gavin Simpson Apr 15 '11 at 11:55

## dummy data
require(MASS)
set.seed(1)
dat <- data.frame(mvrnorm(100, mu = c(2,6,3),
Sigma = matrix(c(10,   2,   4,
2,   3, 0.5,
4, 0.5,   2), ncol = 3)))


So my data are in object dat, you have read your data in and called it something. Use that object instead of dat in this code below: [@sridher - the codes below are the three lines I mentioned!]

set.seed(2) ## *k*-means uses a random start
klust <- kmeans(scale(dat, center = TRUE, scale = TRUE), centers = 3)
klust


The first line (set.seed(2)) fixes the random number generator at a given starting point so the results are reproducible. We do this because kmeans(), if not given the starting cluster centres will randomly choose centers samples from your data as the cluster centres.

The second line calls kmeans() on the standardised data (all the variables in my data set are in different units, so scaling them to zero mean and unit variance would seem appropriate). We ask for 3 groups by specifying centers = 3.

The third line prints the fitted k-means object to the screen showing the output from the function.

This is just an example though. Why three groups? I don't even do any subsequent analysis of the clustering solution. Furthermore, you probably want to run the kmeans() code several times to make sure you get similar clusterings each time, but using different random starts --- set a different seed for each run.

There is a lot more to clustering than just throwing your data at an algorithm!

You can automate that bit to some extent using the cascadeKM() function in package vegan:

require(vegan)
fit <- cascadeKM(scale(dat, center = TRUE, scale = TRUE), 1, 4, iter = 100)
plot(fit, sortg = TRUE)


which suggests 2 groups is the best solution for these data:

but we know the data generation process had three groups, and as such k-means and the summary stats of the results have not been able to correctly identify the presence of three groups in this small sample of data.

With some real data this time, using the famous Iris data set

fit2 <- cascadeKM(iris[,1:4], 1, 4, iter = 100)
plot(fit2, sortg = TRUE)


which clearly favours 3 groups,

, which is good seeing as there really are three species in the data set

> with(iris, unique(Species))
[1] setosa     versicolor virginica
Levels: setosa versicolor virginica

-
wow u r really amazing gavin....great to see good patience from u....this time i understood well....and when i pasted 3 lines into the console i got this error Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric –  user4184 Apr 15 '11 at 12:37
So your data contains non-numeric variables. Subset the data to only include the numeric ones. –  Gavin Simpson Apr 15 '11 at 12:50
My very +1 for this instructive response (which nicely complements the other one you gave on SO, btw). For the OP @sridher who seems to wonder about understanding how Cluster Analysis works, there are a lot of clustering tutorials, including this one or this one (both in PDF). –  chl Apr 15 '11 at 18:06

A lot of people coming from SAS or SPSS to R find the Quick-R website useful. There is a page on cluster analysis which you may find useful in getting you started.

-