0

I'm trying to do a multivariate k-means cluster plot in r. I have 3 variables, and 10 columns of data, plus the context (like species for Iris) so 11 variables. And my x is PeruReady, obviously

Following a tutorial online I got this far:

PeruReady.km <- kmeans(PeruReady[, -1], 3, iter.max=1000) 
tbl <- table(PeruReady[, 1], PeruReady.km$cluster) 
PeruReady.dist <- dist(PeruReady[, -1]) 
PeruReady.mds <- cmdscale(PeruReady.dist) 
c.chars <- c("*", "o", "+")[as.integer(PeruReady$Context)] 
a.cols <- rainbow(3)[PeruReady$cluster] 
plot(PeruReady.mds, col=a.cols, pch=c.chars, xlab="X", ylab="Y")

But my plot is coming up completely empty, what am I doing wrong?

jlhoward
  • 58,004
  • 7
  • 97
  • 140
Lmmken
  • 11
  • 1
  • 2
    If you really want help, you need to make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Include some sample data so we can run the code to see where your error is. – MrFlick Dec 10 '14 at 18:54
  • What is `PeruReady`? If it's not too large, post the result of `dput(PeruReady)` in your question. Also, the `{}` button is for code blocks. – jlhoward Dec 10 '14 at 20:53

1 Answers1

0

With a small data set (demand.sm), your code worked just fine. Have you normalized all your numeric columns?

dput(demand.sm) 

structure(list(Demand = c("rify la", "p quasi", "rify LD", "ventive", 
"ekeeper", " de min", " risk g", " approv", "uest te", "", "al trai", 
"cation", "ely inv", "rge tim", "get of ", "vey pro", "ent ONA", 
"ble sel", "cipline", "tus rep", "ced-ran"), normalized = structure(c(-1.15780226157481, 
-0.319393727330983, -1.15780226157481, -1.15780226157481, -0.319393727330983, 
-0.319393727330983, -0.319393727330983, -0.319393727330983, 0.519014806912847, 
0.519014806912847, 0.519014806912847, -0.738597994452898, -0.738597994452898, 
2.19583187540051, 2.19583187540051, -1.15780226157481, -0.319393727330983, 
-0.319393727330983, 0.519014806912847, 1.35742334115668, 0.519014806912847
), .Dim = c(21L, 1L), "`scaled:center`" = 3.76190476190476, "`scaled:scale`" = 2.38547190100328)), .Names = c("Demand", 
"normalized"), row.names = c(NA, -21L), class = "data.frame")
clusters <- kmeans(demand.sm[ , "normalized"], 5)

demand.dist <- dist(demand.sm[ , "normalized"]) 
demand.mds <- cmdscale(demand.dist) # multidimensional scaling of data matrix, aka principal coordinates analysis
c.chars <- c("*", "o", "+")[as.integer(clusters$Context)] 
a.cols <- rainbow(3)[clusters$cluster] 
plot(demand.mds, col=a.cols, pch=c.chars, xlab="X", ylab="Y")

enter image description here

lawyeR
  • 7,488
  • 5
  • 33
  • 63