{"word_count": 286, "character_count": 2825}
In order to illustrate hierarchical clustering techniques and k-means, I did borrow François Husson's dataset, with monthly average temperature in several French cities.&#62; temp=read.table(+ "http://freakonometrics.free.fr/FR_temp.txt",+ header=TRUE,dec=",")We have 15 cities, with monthly observations&#62; X=temp[,1:12]&#62; boxplot(X)Since the variance seems to be rather stable, we will not 'normalize' the variables here,&#62; apply(X,2,sd)    Janv     Fevr     Mars     Avri 2.007296 1.868409 1.529083 1.414820      Mai     Juin     juil     Aout 1.504596 1.793507 2.128939 2.011988     Sept     Octo     Nove     Dece 1.848114 1.829988 1.803753 1.958449In order to get a hierarchical cluster analysis, use for instance&#62; h &#60;- hclust(dist(X), method = "ward")&#62; plot(h, labels = rownames(X), sub = "")An alternative is to use&#62; library(FactoMineR)&#62; h2=HCPC(X)&#62; plot(h2)Here, we visualise observations with a principal components analysis. We have here also an automatic selection of the number of classes, here 3. We can get the description of the groups using&#62; h2$desc.indor directly&#62; cah=hclust(dist(X))&#62; groups.3 &#60;- cutree(cah,3)We can also visualise those classes by ourselves,&#62; acp=PCA(X,scale.unit=FALSE)&#62; plot(acp$ind$coord[,1:2],col="white")&#62; text(acp$ind$coord[,1],acp$ind$coord[,2],+ rownames(acp$ind$coord),col=groups.3)It is possible to plot the centroïds of those clusters&#62; PT=aggregate(acp$ind$coord,list(groups.3),mean)&#62; points(PT$Dim.1,PT$Dim.2,pch=19)If we add Voroid sets around those centroïds, here we do not see them (actually, we see the point - in the middle - that is exactly at the intersection of the three regions),&#62; library(tripack)&#62; V &#60;- voronoi.mosaic(PT$Dim.1,PT$Dim.2)&#62; plot(V,add=TRUE)To visualize those regions, use&#62; p=function(x,y){+   which.min((PT$Dim.1-x)^2+(PT$Dim.2-y)^2)+ }&#62; vx=seq(-10,12,length=251)&#62; vy=seq(-6,8,length=251)&#62; z=outer(vx,vy,Vectorize(p))&#62; image(vx,vy,z,col=c(rgb(1,0,0,.2),+ rgb(0,1,0,.2),rgb(0,0,1,.2)))&#62; CL=c("red","black","blue")&#62; text(acp$ind$coord[,1],acp$ind$coord[,2],+ rownames(acp$ind$coord),col=CL[groups.3])Actually, those three groups (and those three regions) are also the ones we obtain using a k-mean algorithm,&#62; km=kmeans(acp$ind$coord[,1:2],3)&#62; kmK-means clustering with 3 clusters of sizes 3, 7, 5(etc). But actually, since again we have some spatial data, it is possible to visualize them on a map&#62; library(maps)&#62; map("france")&#62; points(temp$Long,temp$Lati,col=groups.3,pch=19)or, to visualize the regions, use e.g.&#62; library(car)&#62; for(i in 1:3) + dataEllipse(temp$Long[groups.3==i],+ temp$Lati[groups.3==i], levels=.7,add=TRUE,+ col=i+1,fill=TRUE)Those three regions actually make sense, geographically speaking.