-5

Below code generates this graph :

enter image description here

When clustering two dimensional items there is a centroid for each cluster, but why is no centroid generated for these graphs ?

Is each group of graphs generating a kmeans cluster of the other two items? So for example in first row going from left to right, "google" is the label, kmeans is being generated for "so" and "test", is this correct ?

cells = c(1,1,1,
          1,0,1,
          1,0,1,
          1,0,0,
          1,1,1,
          0,1,0,
          0,1,1,
          1,1,0,
          0,0,1,
          0,0,0,
          1,1,1,
          1,1,0,
          1,0,1,
          1,1,0,
          1,0,1,
          1,1,0,
          1,0,1,
          1,1,0,
          1,0,1,
          1,1,0,
          1,0,1,
          1,1,0,
          1,0,1,
          1,1,0)
rnames = c("a1","a2","a3","a4","a5","a6","a7","a8","a9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24")
cnames = c("google","so","test")
x <- matrix(cells, nrow=24, ncol=3, byrow=TRUE, dimnames=list(rnames, cnames))
# run K-Means
km <- kmeans(x, 8, 5)
# print components of km
print(km)
# plot clusters
plot(x, col = km$cluster)
# plot centers
pairs(jitter(x), col = cl$cluster)
3
  • 1
    Try pairs(jitter(x), col=km$cluster). Jul 8, 2013 at 22:00
  • @Jean V. Adams thanks but I just require an explanation to questions posted.
    – blue-sky
    Jul 8, 2013 at 22:08
  • 4
    This question appears to be off-topic because it is about interpreting statistical output, which belongs on CrossValidated.
    – Hong Ooi
    Jul 9, 2013 at 1:47

1 Answer 1

3

because you're not plotting the centroid. In your earlier question, the centroids were plotted by this command:

points(cl$centers, col = 1:5, pch = 8, cex = 2)

This added points for each centroid to the plot produced by the plot function. If you try to do this with pairs() it won't work. But you aren't even trying this in the code you posted, so I'm not sure why you expected to see the centroids plotted anyway.

To add points into pairs() plots is an unfortunately manual process. You can use the panel, lower.panel, and upper.panel parameters of the pairs() function to specify exactly what you want to plot for each pair of vectors. Here, I specify submethods to plot the points normally in the top panels and to plot the points with their centroids in the lower panels.

# I use the variable name "x" elsewhere, 
# renaming it here explicitly for clarity  
x.mat=x

# I moved the "jitter" into this submethod, so you won't see it
# in the main 'pairs()' call. I needed to do this to identify the source
# column the data came from in low.panelfun.
up.panelfun <- function(x,y,clust=cl$cluster,...){
  # this plots the main pairs plot
  sapply(unique(clust), function(c){ points(jitter(x[clust==c]),jitter(y[clust==c]), col=c)}) 
}

low.panelfun <- function(x,y,clust=cl$cluster,...){
  # this plots the main pairs plot
  up.panelfun(x,y,clust)

  # this finds the appropriate column the panel is related
  # to and plots the centroids.
  xi=which(length(x)==apply(x.mat, 2, function(v){sum(v==x)}))
  yi=which(length(y)==apply(x.mat, 2, function(v){sum(v==y)}))
  points(cl$centers[xi,],cl$centers[yi,], col = 1:5, pch = 8, cex = 2)
}

pairs(x.mat, col = cl$cluster
      ,lower.panel=low.panelfun
      ,upper.panel=up.panelfun
)

Amplified pairs plot, centroids added to lower panel

Because your dataset is so small, I found it useful to amplify your data by duplicating the results a few times to make the clusters a little more obvious:

# amplify clusters by replicating data a few times
pairs(rbind(x.mat, x.mat, x.mat, x.mat), col = cl$cluster
      ,lower.panel=low.panelfun
      ,upper.panel=up.panelfun
)

Considering all the additional work this took and that you really only needed the three plots, it probably would have been easier to just build a separate plot();points() call for each pair of variables.

3
  • how should I read the generated graphs. Each label "google" , "so" , "test" what is their meaning in relation to the other graphs ?
    – blue-sky
    Jul 8, 2013 at 22:20
  • 1
    It's just a normal two-dimensioanl scatterplot. The pairs() function takes all the possible pairs of variables you gives it and plots them against each other. Check the documentation for the pairs function, it's explained pretty well there.
    – David Marx
    Jul 8, 2013 at 22:22
  • thanks I found this useful : statmethods.net/graphs/scatterplot.html section : "Scatterplot Matrices"
    – blue-sky
    Jul 8, 2013 at 22:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.