Cluster Analysis
Cluster Analysis
Formatting the Data
> mydata <- na.omit(mydata) # listwise deletion of missing > mydata <- scale(mydata) # standardize variables # (Opyional) Determine number of clusters > wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var)) for (i in 2:15) > wss[i] <- sum(kmeans(mydata, centers=i)$withinss) > plot(1:15, wss, type="b", xlab="Number of Clusters", ylab="Within groups sum of squares")
Kmeans Clustering
> fit <- kmeans(mydata, 5) # 5 cluster solution # get cluster means > aggregate(mydata,by=list(fit$cluster),FUN=mean) # append cluster assignment > mydata <- data.frame(mydata, fit$cluster)
Hierarcheal Clustering
# Ward Hierarchical Clustering > d <- dist(mydata, method = "euclidean") # distance matrix > fit <- hclust(d, method="ward") > plot(fit) # display dendogram > groups <- cutree(fit, k=5) # cut tree into 5 clusters # draw dendogram with red borders around the 5 clusters > rect.hclust(fit, k=5, border="red")