1276251155734027
Loading...

R Tutorials | Hierarchical Clustering Models with R | #rstats

#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).

2. Next, we install the library FactoMineR in R:
utils:::menuInstallPkgs()
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:
How to do cluster analysis in R?
Hierarchical Clustering with FactoMineR in R
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 in R with the library car | How to do a cluster analysis in R
Scatterplot in R with the library car
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").

technology 7348815999551037478

Post a Comment

Home item

More links from #glfintech:

#glfintech Newsletter

#glfintech Recommends

#glfintech on Twitter

Statistics


Partner and Clients

Warmboutique

Webstore selling textile goods

Adblock is enabled

Hi! We have detected that you are using adblock on your web browser and take this chance to ask you to pause it just for this site. Time is money and we invest lots of time in the content that we work really hard on, and advertising is the only source of income for this particular project.
Thanks.

No harming software. We promise!