{"word_count": 192, "character_count": 2316}
For the data scienec course of tomorrow, I just wanted to post some functions to illustrate cluster analysis. Consider the dataset of the French 2012 elections&#62; elections2012=read.table("http://freakonometrics.free.fr/elections_2012_T1.csv",sep=";",dec=",",header=TRUE)&#62; voix=which(substr(names(+ elections2012),1,11)=="X..Voix.Exp")&#62; elections2012=elections2012[1:96,]&#62; X=as.matrix(elections2012[,voix])&#62; colnames(X)=c("JOLY","LE PEN","SARKOZY","MÉLENCHON","POUTOU","ARTHAUD","CHEMINADE","BAYROU","DUPONT-AIGNAN","HOLLANDE")&#62; rownames(X)=elections2012[,1]The hierarchical cluster analysis is obtained using&#62; cah=hclust(dist(X))&#62; plot(cah,cex=.6)To get five groups, we have to prune the tree&#62; rect.hclust(cah,k=5)&#62; groups.5 &#60;- cutree(cah,5)We have to zoom-in to visualize the French regions,It is also possible to use&#62; library(dendroextras)&#62; plot(colour_clusters(cah,k=5))And again, if we zoom-in, we getThe interpretation of the clusters can be obtained using&#62; aggregate(X,list(groups.5),mean)  Group.1     JOLY   LE PEN  SARKOZY1       1 2.185000 18.00042 28.740422       2 1.943824 23.22324 25.780293       3 2.240667 15.34267 23.459334       4 2.620000 21.90600 34.322005       5 3.140000  9.05000 33.80000It is also possible to visualize those clusters on a map, using&#62; library(RColorBrewer)&#62; CL=brewer.pal(8,"Set3")&#62; carte_classe &#60;- function(groupes){+ library(stringr)+ elections2012$dep &#60;- elections2012[,2]+ elections2012$dep &#60;- tolower(elections2012$dep)+ elections2012$dep &#60;- str_replace_all(elections2012$dep, pattern = " |-|'|/", replacement = "")+ library(maps)+ france&#60;-map(database="france")+ france$dep &#60;- france$names+ france$dep &#60;- tolower(france$dep)+ france$dep &#60;- str_replace_all(france$dep, pattern = " |-|'|/", replacement = "")+ corresp_noms &#60;- elections2012[, c(1,2, ncol(elections2012))]+ corresp_noms$dep[which(corresp_noms$dep %in% "corsesud")] &#60;- "corsedusud"+ col2001&#60;-groupes+1+ names(col2001) &#60;- corresp_noms$dep[match(names(col2001), corresp_noms[,1])]+ color &#60;- col2001[match(france$dep, names(col2001))]+ map(database="france", fill=TRUE, col=CL[color])+ }&#62; carte_classe(cutree(cah,5))or, if we simply want 4 clusters&#62; carte_classe(cutree(cah,4))&#160;