Saturday, April 25, 2015

Cluster Analysis

#K-means
par(mfrow=c(1,2))
x=iris$Sepal.Length
y=iris$Sepal.Width
s=iris$Species
kdf <- data.frame(x,y)
km <- kmeans(kdf[,c(1,2)],5)
plot(x,y, col=km$cluster, cex=1,pch=1, main="Plot of iris k-means Clusters")
points(km$centers, cex=1, pch=3, lwd=3)
plot(x,y, col=s, cex=1,pch=1, main="Plot of iris")


#Hierarchical Clustering
x=iris$Sepal.Length
y=iris$Sepal.Width
kdf <- data.frame(x,y)
disto=dist(kdf)
hc=hclust(disto)
plot(hc)

Resource: http://www.rdatamining.com/examples/kmeans-clustering