2

I have produced an HCPC object and would like to return a list of the observations and which clusters they have fallen into. I can't find a command to do this; does anyone know?

FYI, here is my work:

file <- read.csv("file", header=TRUE) 
library(FactoMineR) 
res.mca = MCA(file, graph=FALSE) 
# manually cut tree according to inertia gain 
res.hcpc = HCPC(res.mca, nb.clust=0) 

Edit: A reproducible example:

library(FactoMineR)
data(tea)
res.mca  <- MCA(tea, quanti.sup = 19, quali.sup = 20:36)
res.hcpc <- HCPC(res.mca, nb.clust = 5)
Arun
  • 116,683
  • 26
  • 284
  • 387
Jeff Coughlin
  • 263
  • 3
  • 8

2 Answers2

4

The output $data.clust gives the a dataframe with the input data and a column (the last one) with the cluster the individuals belong.

library(FactoMineR)
data(tea)
res.mca  <- MCA(tea, quanti.sup = 19, quali.sup = 20:36)
res.hcpc <- HCPC(res.mca, nb.clust = 5)
res.hcpc$data.clust

Note that if you want that the individuals are in the same order as in the input dataset, you should use the argument order=FALSE in HCPC :

res.hcpc <- HCPC(res.mca, nb.clust = 5, order=FALSE)
res.hcpc$data.clust
scoa
  • 19,359
  • 5
  • 65
  • 80
Husson
  • 141
  • 3
1

Although this post was written 3 years ago, my answer can be of help for readers. I was just looking for a solution to the same question as I used HCPC for outliers detection and here is the idea I got: I extracted a subset from the dataframe $data.clust issued from hcpc(), based on the field "cluster". outliers<-subset(DHCPC$data.clust, clust==4)

I found the answer here: how to extract a subset of a data frame based on a condition involving a field?

Community
  • 1
  • 1