[R] Problem with clusplot

166 views
Skip to first unread message

elisacarli21

unread,
Dec 6, 2011, 12:58:14 AM12/6/11
to r-h...@r-project.org
Dear all
I'm trying to run a cluster analysis with R
Here are the commands:

mydata <- na.omit(matrix) # listwise deletion of missing
mydata <- scale(matrix) # standardize variables

fit <- kmeans(mydata, 8) # 8 cluster solution
# get cluster means
aggregate(mydata,by=list(fit$cluster),FUN=mean)
# append cluster assignment
mydata <- data.frame(mydata, fit$cluster)

library(cluster)
clusplot(mydata, fit$cluster, color=TRUE, shade=TRUE, labels=2, lines=0)


I get the following error
Error in princomp.default(x, scores = TRUE, cor = ncol(x) != 2) :
'princomp' can only be used with more units than variables

How can I fix it?

Thanks

--
View this message in context: http://r.789695.n4.nabble.com/Problem-with-clusplot-tp4163621p4163621.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Sarah Goslee

unread,
Dec 6, 2011, 8:33:41 AM12/6/11
to elisacarli21, r-h...@r-project.org
Well, if I had to guess (and I do, since we have no idea what your data
look like, and calling your data matrix is a very bad idea): you have more
variables than units, so clusplot() can't use princomp() to create a reduced-
dimension plot.

You could roll your own using a different ordination method, or you could
remove some variables from your data, or you could think about whether
the data you have are appropriate for what you're trying to do with them.

Sarah

On Tue, Dec 6, 2011 at 12:58 AM, elisacarli21 <elisac...@gmail.com> wrote:
> Dear all
> I'm trying to run a cluster analysis with R
> Here are the commands:
>
> mydata <- na.omit(matrix) # listwise deletion of missing
> mydata <- scale(matrix) # standardize variables
>
>  fit <- kmeans(mydata, 8) # 8 cluster solution
>  # get cluster means
> aggregate(mydata,by=list(fit$cluster),FUN=mean)
>  # append cluster assignment
>  mydata <- data.frame(mydata, fit$cluster)
>
>  library(cluster)
> clusplot(mydata, fit$cluster, color=TRUE, shade=TRUE,  labels=2, lines=0)
>
>
> I get the following error
> Error in princomp.default(x, scores = TRUE, cor = ncol(x) != 2) :
>  'princomp' can only be used with more units than variables
>
> How can I fix it?
>
> Thanks
>

--
Sarah Goslee
http://www.functionaldiversity.org

Reply all
Reply to author
Forward
0 new messages