[R] Obtaining covariance matrices for kmeans output clusters
Luke Hutchison
lukeh at email.byu.edu
Wed Oct 23 22:41:06 CEST 2002
I am having trouble getting a covariance matrix for each cluster which
is output by kmeans(). My input looks like:
> imagedat <- read.table("table", header=TRUE)
> imagedat
Red Green Blue
0_0 5 7 8
1_0 5 5 18
2_0 7 8 49
3_0 22 8 76
4_0 54 10 67
5_0 50 9 28
6_0 18 10 15
7_0 9 7 6
8_0 2 5 7
...
I cluster using
> cl <- kmeans(imagedat, nclust, maxsteps)
> cl
$cluster
[1] 1 1 9 8 2 9 1 1 1 1 1 1 1 1 1 8 8 8 8 4
[25] 9 9 8 8 8 2 2 9 1 1 9 9 7 10 10 10 10 10 10 10 10
...
$centers
Red Green Blue
1 9.940421 7.744428 11.11652
2 85.198120 18.363348 68.10173
3 109.247072 80.873439 87.42371
...
I then try applying "var" to each Red, Green, Blue triplet (i.e. row) in
the cluster output using
> covar <- tapply(imagedat, factor(cl$clust), var)
arguments must have same length
but obviously imagedat is being looked at as a stacked array, three
times the length of cl$clust, so I get the above error.
So my questions are:
(1) How can I get ten 3x3 covariance matrices from the clustering
results "cl$clust", i.e. one for each cluster, with the covariance of
Red, Blue and Green from "imagedat" for each cluster?
(2) How then can I apply operations to all of the matrices at once, i.e.
take the inverse of all of them, or multiply them all by a constant,
without having to write a for-loop? How can I apply operations to them
each in turn, say in a for-loop?
Thanks very much for any pointers, I'm new to R and its
data-manipulation and storage mechanisms are puzzling to me.
Luke Hutchison.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list