## dummy data
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)
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
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))
 setosa versicolor virginica
Levels: setosa versicolor virginica