library(MASS,cluster,mva,class)

# kmeans clustering on the wine data

# first look at wine.km1 and wine.km2:

wine.km1$withinss

wine.km2$withinss

# now plot the linear discriminant functions, using the kmeans clusters as labels:

plot(wine.lda,title="linear discriminants for wine data", cex=0.8)

plot(wine.lda, col=as.character(wine.km2$cluster),cex=0.8)

# hierarchical clustering

plot(hclust(dist(wine.x),method="single"), labels=wine[,1])

plot(agnes(wine.x, metric="manhattan", method="average"),labels=wine[,1])

plot(agnes(dist(wine.x),method="average")labels=wine[,1])

plot(diana(dist(wine.x))labels=wine[,1])

# principal components

wine.pc <- princomp(wine.x,cor=T)

plot(wine.pc)

biplot(wine.pc)

# MDS

wine.mds <- cmdscale(dist(wine.x),k=2)
eqscplot(wine.mds$points,type="n")
text(wine.mds$points,labels=wine[,1],col=as.character(wine[,1]),cex=0.8)

plot(wine.mds2[,1],wine.mds2[,2],col=as.character(wine[,1]))

#SOM

wine.gr <- somgrid()
wine.som <- batchSOM(wine,wine.gr,c(4,4,2,2,1,1,1,0,0))
plot(wine.som)
bins <- as.numeric(knn1(wine.som$code,wine.x,0:47))
plot(wine.som$grid,type="n")
symbols(wine.som$grid$pts[,1],wine.som$grid$pts,[,2],circles=rep(0.4,48),inches=F,add=T)
text(wine.som$grid$pts[bins,],as.character(wine[,1])