R Tutorials | Hierarchical Clustering Models with R | #rstats
https://global-fintech.blogspot.com/2016/01/hierarchical-clustering-factominer-r.html
#rstats In this series of our R tutorials, we are going to do a cluster analysis with R. Using the R language, we can conduct it in several minutes. Otherwise, it could take us one or two hours of brainwork and calculations. For this example we apply the R library FactoMineR which was used in our example about the principal component analysis (follow the link to read more about FactoMineR and principal component analysis).
Hierarchical Clustering Analysis with R |
R Tutorials | How to Do Hierarchical Clustering with FactoMineR in R?
1. As ususal, we start with the preliminary steps setting up our R folder and adding the data to the working memory of R. We will use the same dataset that we used for demonstration in the previous post about principal component analysis with FactoMineR (follow the link):
getwd()
setwd(("C:/Users/Karol/Desktop/R”)
auto<-read.table("auto2004.csv", header=TRUE, sep=",")
attach(auto)
str(auto).
auto<-read.table("auto2004.csv", header=TRUE, sep=",")
attach(auto)
str(auto).
2. Next, we install the library FactoMineR in R:
utils:::menuInstallPkgs()
library(FactoMineR).
library(FactoMineR).
3. Our next action would be to do the hierarchical clustering in R in the form of a tree by means of the function HCPC():
HCPC(res.pca)->res.hcpc.
We get the following illustration which shows how dfferent car models are organised in clusters:
4. To get the result of the cluster analysis in R, we enter the following commands:
res.hcpc$desc.var
(helps to give an interpretation of the clusters)res.hcpc$data.clust ()
res.hcpc$data.clust[,7]->group
(gives the file with a variable containing the group to which the subjects belong).
5. Then, we attach the group to the data file:
cbind(auto, group)->auto.
6. We can also draw a scatterplot with differently coloured subgroups by means of the command scatterplot() which needs the library car:
utils:::menuInstallPkgs()
library(car)
scatterplot(auto$Dim.2~auto$Dim.1|group, smooth=FALSE, reg.line=FALSE)
scatterplot(auto$Dim.2~auto$Dim.1|group, smooth=FALSE, reg.line=FALSE)
7. Finally, we can add some text subscriptions through the command text() to the graph to make it more understandable:
text(auto$Dim.1, auto$Dim.2, modèle, pos=1, cex=0.4, col="orange").