HCPC factominer error

Hello,
I'm on ubuntu 20.04 and I'm trying on Rstudio, the Hierarchical Clustering on Principle Components (HCPC ) function of a package called factominer.I tried this example of the package website : http://factominer.free.fr/book/decoupage.txt
Here is the R script :

library(FactoMineR)
tea <- read.table("http://factominer.free.fr/book/tea.csv",header=TRUE,sep=";")
nb.clusters <- 4
coupure <- quantile(tea[,22], seq(0,1,1/nb.clusters))
Xqual <- cut(tea[,22],coupure, include.lowest=TRUE)
summary(Xqual)
hist(tea$age,col="grey",main="Histogram of the variable age",freq=FALSE, xlab="age", nclass=15)

vari <- tea[,22]
res.hcpc <- HCPC(vari, iter.max=10,nb.clust = -1)

max.cla = unlist(by(res.hcpc$data.clust[,1],res.hcpc$data.clust[,2],max))
breaks=c(min(vari),max.cla)
aaQuali = cut(vari, breaks, include.lowest=TRUE)
summary(aaQuali)

don.quali <- don
for (i in 1:ncol(don.quali)){
  vari = don.quali[,i]
  res.hcpc=HCPC(vari, nb.clust=-1, graph=FALSE)
  maxi = unlist(by(res.hcpc$data.clust[,1], res.hcpc$data.clust[,2],max))
  breaks=c(min(vari),maxi)
  aaQuali = cut(vari, breaks, include.lowest=TRUE)
  don.quali[,i] = aaQuali
}

and here the results in console R :

R version 4.2.2 Patched (2022-11-10 r83330) -- "Innocent and Trusting"
Copyright (C) 2022 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(FactoMineR)
> tea <- read.table("http://factominer.free.fr/book/tea.csv", header = TRUE, sep = ";")
> nb.clusters <- 4
> coupure <- quantile(tea[, 22], seq(0, 1, 1 / nb.clusters))
> Xqual <- cut(tea[, 22], coupure, include.lowest = TRUE)
> summary(Xqual)
[15,23] (23,32] (32,48] (48,90] 
     86      66      74      74 
> hist(tea$age, col = "grey", main = "Histogram of the variable age", freq = FALSE, xlab = "age", nclass = 15)
> vari <- tea[, 22]
> res.hcpc <- HCPC(vari, iter.max = 10,nb.clust = -1)
Error in as.data.frame(data.clust[, -2]) : object 'data.clust' not found
> 

I'm getting an error on this command : res.hcpc <- HCPC(vari, iter.max=10) it shows me the dendogram but when I cut at the suggested line it give me an error, so I try to add the nb.clust = -1 option(it is for automatically cutting the tree at the suggested level) by doing this instead :
res.hcpc <- HCPC(vari, iter.max=10,nb.clust = -1)
but I still have the same error :
Error in as.data.frame(data.clust[, -2]) : object 'data.clust' not found

Thanks in advance for your help.

I get that, too. What I'd try is to retrace the steps from the text's example code to see how it differs from what you are trying to derive, which will illuminate what may need to change in how you are trying to do it.

library(FactoMineR)
tea <- read.table("http://factominer.free.fr/book/tea.csv",header=TRUE,sep=";")
summary(tea)
#>   breakfast         afternoon.tea        evening          after.lunch       
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>  after.dinner         anytime              home               work          
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>    tearoom            friends           restaurant            pub           
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>    variety              how               sugar              format         
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>  place.of.purchase      type               sex             profession       
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>     sport                age           age_Q            frequency        
#>  Length:300         Min.   :15.00   Length:300         Length:300        
#>  Class :character   1st Qu.:23.00   Class :character   Class :character  
#>  Mode  :character   Median :32.00   Mode  :character   Mode  :character  
#>                     Mean   :37.05                                        
#>                     3rd Qu.:48.00                                        
#>                     Max.   :90.00                                        
#>     exotic          spirituality       good.for.health      diuretic        
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>  friendliness       iron.absorption      feminine           refined         
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#>                                                                             
#>    slimming          stimulant           relaxant         no.effect.health  
#>  Length:300         Length:300         Length:300         Length:300        
#>  Class :character   Class :character   Class :character   Class :character  
#>  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
#>                                                                             
#>                                                                             
#> 
res.mca<-MCA(tea, quanti.sup=22, quali.sup=c(19:21,23:36))

#> Warning: ggrepel: 31 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

plot(res.mca,invisible=c("var","quali.sup"),cex=0.7)

plot(res.mca,invisible=c("ind","quali.sup"))
#> Warning: ggrepel: 26 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps

plot(res.mca,invisible="quali.sup")

plot(res.mca,invisible="ind")

plot(res.mca,invisible=c("ind","var"))
#> Warning: ggrepel: 24 unlabeled data points (too many overlaps). Consider
#> increasing max.overlaps


round(res.mca$eig,2)
#>        eigenvalue percentage of variance cumulative percentage of variance
#> dim 1        0.15                   9.88                              9.88
#> dim 2        0.12                   8.10                             17.99
#> dim 3        0.09                   6.00                             23.99
#> dim 4        0.08                   5.20                             29.19
#> dim 5        0.07                   4.92                             34.11
#> dim 6        0.07                   4.76                             38.87
#> dim 7        0.07                   4.52                             43.39
#> dim 8        0.07                   4.36                             47.74
#> dim 9        0.06                   4.12                             51.87
#> dim 10       0.06                   3.90                             55.77
#> dim 11       0.06                   3.81                             59.57
#> dim 12       0.05                   3.63                             63.20
#> dim 13       0.05                   3.46                             66.66
#> dim 14       0.05                   3.25                             69.91
#> dim 15       0.05                   3.22                             73.13
#> dim 16       0.05                   3.13                             76.26
#> dim 17       0.05                   3.04                             79.30
#> dim 18       0.04                   2.68                             81.98
#> dim 19       0.04                   2.54                             84.52
#> dim 20       0.04                   2.44                             86.96
#> dim 21       0.04                   2.38                             89.34
#> dim 22       0.03                   2.32                             91.66
#> dim 23       0.03                   2.06                             93.72
#> dim 24       0.03                   1.92                             95.63
#> dim 25       0.03                   1.82                             97.45
#> dim 26       0.02                   1.41                             98.86
#> dim 27       0.02                   1.14                            100.00
lapply(dimdesc(res.mca),lapply,round,4)
#> Error in Math.data.frame(structure(list(`Dim 1` = c(-0.541014547284892, : non-numeric-alike variable(s) in data frame: breakfast, afternoon.tea, evening, after.lunch, after.dinner, anytime, home, work, tearoom, friends, restaurant, pub, variety, how, sugar, format, place.of.purchase, type, sex, profession, sport, age_Q, frequency, exotic, spirituality, good.for.health, diuretic, friendliness, iron.absorption, feminine, refined, slimming, stimulant, relaxant, no.effect.health
lapply(dimdesc(res.mca),lapply,signif,3)
#> Error in Math.data.frame(structure(list(`Dim 1` = c(-0.541014547284892, : non-numeric-alike variable(s) in data frame: breakfast, afternoon.tea, evening, after.lunch, after.dinner, anytime, home, work, tearoom, friends, restaurant, pub, variety, how, sugar, format, place.of.purchase, type, sex, profession, sport, age_Q, frequency, exotic, spirituality, good.for.health, diuretic, friendliness, iron.absorption, feminine, refined, slimming, stimulant, relaxant, no.effect.health

plotellipses(res.mca,keepvar=c("relaxant","restaurant","profession","place.of.purchase"))


res.mca <- MCA(tea, quanti.sup=22, quali.sup=c(19:21,23:36), graph=FALSE)
new.data <- cbind.data.frame(tea[,11],res.mca$ind$coord)
res.pca <- PCA(new.data,quali.sup=1,scale=FALSE,graph=FALSE)
res.pca$eig[1:5,]=res.mca$eig[1:5,]
concat.data <- cbind.data.frame(tea[,11],res.mca$ind$coord)
ellipse.coord <- coord.ellipse(concat.data,bary=TRUE)
#> Error in if (scale[1] > 0) r <- r/scale[1]: missing value where TRUE/FALSE needed
plot.PCA(res.pca, habillage=1, ellipse=ellipse.coord, cex=0.8,label="none")
#> Error in plot.PCA(res.pca, habillage = 1, ellipse = ellipse.coord, cex = 0.8, : object 'ellipse.coord' not found

catdes(tea, num.var = 18)
#> 
#> Link between the cluster variable and the categorical variables (chi-square test)
#> =================================================================================
#>                        p.value df
#> place.of.purchase 1.109612e-18 10
#> format            8.441989e-11 10
#> tearoom           1.672882e-03  5
#> friends           4.271608e-02  5
#> slimming          4.329224e-02  5
#> variety           4.963497e-02 10
#> 
#> Description of each cluster by the categories
#> =============================================
#> $cheapest
#> NULL
#> 
#> $known.brand
#>                                             Cla/Mod   Mod/Cla   Global
#> place.of.purchase=supermarket             42.708333 86.315789 64.00000
#> format=sachet                             41.176471 73.684211 56.66667
#> tearoom=not.tearoom                       36.363636 92.631579 80.66667
#> profession=employee                       47.457627 29.473684 19.66667
#> sport=not.sporty                          38.842975 49.473684 40.33333
#> good.for.health=good for health           35.238095 77.894737 70.00000
#> profession=senior management              17.142857  6.315789 11.66667
#> good.for.health=not.good for health       23.333333 22.105263 30.00000
#> sport=sporty                              26.815642 50.526316 59.66667
#> format=loose                              13.888889  5.263158 12.00000
#> format=sachet+loose                       21.276596 21.052632 31.33333
#> place.of.purchase=specialist.shop          6.666667  2.105263 10.00000
#> tearoom=tearoom                           12.068966  7.368421 19.33333
#> place.of.purchase=supermarket+specialist. 14.102564 11.578947 26.00000
#>                                                p.value    v.test
#> place.of.purchase=supermarket             1.266805e-08  5.690483
#> format=sachet                             4.473843e-05  4.081539
#> tearoom=not.tearoom                       1.794356e-04  3.746337
#> profession=employee                       4.834575e-03  2.817854
#> sport=not.sporty                          2.981255e-02  2.172572
#> good.for.health=good for health           4.155241e-02  2.037975
#> profession=senior management              4.597973e-02 -1.995579
#> good.for.health=not.good for health       4.155241e-02 -2.037975
#> sport=sporty                              2.981255e-02 -2.172572
#> format=loose                              1.158619e-02 -2.524503
#> format=sachet+loose                       8.370027e-03 -2.636767
#> place.of.purchase=specialist.shop         8.371610e-04 -3.340207
#> tearoom=tearoom                           1.794356e-04 -3.746337
#> place.of.purchase=supermarket+specialist. 5.845313e-05 -4.018970
#> 
#> $luxury
#>                                              Cla/Mod  Mod/Cla   Global
#> place.of.purchase=specialist.shop          70.000000 39.62264 10.00000
#> format=loose                               55.555556 37.73585 12.00000
#> variety=black                              28.378378 39.62264 24.66667
#> age_Q=60 and +                             31.578947 22.64151 12.66667
#> no.effect.health=no.effect.health          27.272727 33.96226 22.00000
#> tearoom=tearoom                            27.586207 30.18868 19.33333
#> place.of.purchase=supermarket+specialist.  25.641026 37.73585 26.00000
#> tearoom=not.tearoom                        15.289256 69.81132 80.66667
#> no.effect.health=not.without.effect.health 14.957265 66.03774 78.00000
#> variety=flavoured                          12.435233 45.28302 64.33333
#> age_Q=15-24                                 7.608696 13.20755 30.66667
#> format=sachet                               8.235294 26.41509 56.66667
#> place.of.purchase=supermarket               6.250000 22.64151 64.00000
#>                                                 p.value    v.test
#> place.of.purchase=specialist.shop          1.665291e-11  6.732712
#> format=loose                               3.094223e-08  5.536020
#> variety=black                              7.934470e-03  2.654846
#> age_Q=60 and +                             2.532323e-02  2.236436
#> no.effect.health=no.effect.health          2.694825e-02  2.212267
#> tearoom=tearoom                            3.585918e-02  2.098521
#> place.of.purchase=supermarket+specialist.  3.849553e-02  2.069539
#> tearoom=not.tearoom                        3.585918e-02 -2.098521
#> no.effect.health=not.without.effect.health 2.694825e-02 -2.212267
#> variety=flavoured                          1.928697e-03 -3.100998
#> age_Q=15-24                                1.569371e-03 -3.161540
#> format=sachet                              1.140733e-06 -4.865664
#> place.of.purchase=supermarket              1.444316e-11 -6.753388
#> 
#> $shop.brand
#>                                             Cla/Mod   Mod/Cla Global
#> place.of.purchase=supermarket             10.416667 95.238095     64
#> place.of.purchase=supermarket+specialist.  1.282051  4.761905     26
#>                                                p.value    v.test
#> place.of.purchase=supermarket             0.0008502018  3.335912
#> place.of.purchase=supermarket+specialist. 0.0139850954 -2.457646
#> 
#> $unknown
#>                Cla/Mod  Mod/Cla   Global    p.value   v.test
#> format=sachet 6.470588 91.66667 56.66667 0.01087617 2.546653
#> how=milk      9.523810 50.00000 21.00000 0.02635851 2.220889
#> 
#> $varies
#>                                            Cla/Mod   Mod/Cla   Global
#> place.of.purchase=supermarket+specialist. 56.41026 39.285714 26.00000
#> format=sachet+loose                       52.12766 43.750000 31.33333
#> friends=friends                           43.36735 75.892857 65.33333
#> after.lunch=after.lunch                   56.81818 22.321429 14.66667
#> tearoom=tearoom                           51.72414 26.785714 19.33333
#> slimming=not.slimming                     40.00000 91.071429 85.00000
#> variety=flavoured                         41.96891 72.321429 64.33333
#> place.of.purchase=specialist.shop         20.00000  5.357143 10.00000
#> format=sachet                             31.76471 48.214286 56.66667
#> slimming=slimming                         22.22222  8.928571 15.00000
#> place.of.purchase=supermarket             32.29167 55.357143 64.00000
#> tearoom=not.tearoom                       33.88430 73.214286 80.66667
#> after.lunch=not.after.lunch               33.98438 77.678571 85.33333
#> friends=not.friends                       25.96154 24.107143 34.66667
#>                                                p.value    v.test
#> place.of.purchase=supermarket+specialist. 7.141311e-05  3.971528
#> format=sachet+loose                       4.236418e-04  3.524901
#> friends=friends                           2.880745e-03  2.980185
#> after.lunch=after.lunch                   4.905579e-03  2.813169
#> tearoom=tearoom                           1.356760e-02  2.468512
#> slimming=not.slimming                     2.154609e-02  2.298276
#> variety=flavoured                         2.586794e-02  2.228189
#> place.of.purchase=specialist.shop         3.659540e-02 -2.090249
#> format=sachet                             2.370215e-02 -2.261923
#> slimming=slimming                         2.154609e-02 -2.298276
#> place.of.purchase=supermarket             1.730140e-02 -2.380239
#> tearoom=not.tearoom                       1.356760e-02 -2.468512
#> after.lunch=not.after.lunch               4.905579e-03 -2.813169
#> friends=not.friends                       2.880745e-03 -2.980185

Created on 2023-03-05 with reprex v2.0.2

1 Like

Hello,thank you for your response, it seemed you used instructions from the link for MCA : http://factominer.free.fr/book/tea.txt instead of the one concerning HCPC : http://factominer.free.fr/book/decoupage.txt

I misunderstood, sorry. Thought you were extending the code from tea.txt. The problem with the code from decoupage.txt is that the argument to HPPC() is given as the res argument istea[,22] which is not

Either the result of a factor analysis or a dataframe.

Since that is an error in the text's code I suggest you contact the author, Francois Husson, whose address is given at the help(HCPC) page. The line can be made to work with

res.hcpc <- HCPC(tea,kk=10, nb.clust=-1)

as far as the following line, but then throws another error.

The example in help(HPPC)

hc2 <- HCPC(iris[,1:4], kk=10, nb.clust=-1)

would not work in the sample code in substitution, either.

To reconstruct the authors' intent for the decoupage.txt code would be very difficult without reading the entire text to that point and the persons in the best position to explain that would be the authors.

1 Like

Thank you again for your help. Have a nice day;)

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.