## PCA  #
summary(.PC, loadings=TRUE)
library(MVA)
pairs(.PC$scores[,1:3], ylim = c(-6, 4), xlim = c(-6, 4),
      panel = function(x,y, ...) {
          text(x, y, abbreviate(row.names(USairpollution)), 
               cex = 0.6)
          bvbox(cbind(x,y), add = TRUE)
      })
.PC$scores


# EFA life.RData #
library(psych)
cortest.bartlett(life)
det(cor(life))

source("KMO.src")
kmo(life)



cex <- 0.8
plot(life$F1, life$F2, type = "n", xlab = "Factor 1", ylab = "Factor 2")
text(life$F1, life$F2, abbreviate(rownames(life), 5), cex = cex)
plot(life$F1, life$F3, type = "n", xlab = "Factor 1", ylab = "Factor 3")
text(life$F1, life$F3, abbreviate(rownames(life), 5), cex = cex)
plot(life$F2, life$F2, type = "n", xlab = "Factor 2", ylab = "Factor 3")
text(life$F2,life$F2, abbreviate(rownames(life), 5), cex = cex)



# Scripting for H Cluster
library("MVA")

###################################################
### code chunk for Cluster Analysis Case 1, measure.RData 
###################################################
load("measure.RData")
dm <- dist(measure[, c("chest", "waist", "hips")])
par(mfrow=c(1,3))
plot(cs <- hclust(dm, method = "single"))
plot(cc <- hclust(dm, method = "complete"))
plot(ca <- hclust(dm, method = "average"))
par(mfrow=c(1,1))


###################################################
### code chunk for Cluster Analysis: Put hclust and PCA together
###################################################
par(mfrow=c(1,3))
body_pc <- princomp(dm, cor = TRUE)
layout(matrix(1:6, nr = 2), height = c(2, 1))
plot(cs <- hclust(dm, method = "single"), main = "Single")
abline(h = 3.8, col = "lightgrey")
xlim <- range(body_pc$scores[,1])
plot(body_pc$scores[,1:2], type = "n", xlim = xlim, ylim = xlim,
     xlab = "PC1", ylab = "PC2")
lab <- cutree(cs, h = 3.8)
text(body_pc$scores[,1:2], labels = lab, cex=0.6)
plot(cc <- hclust(dm, method = "complete"), main = "Complete")
abline(h = 10, col = "lightgrey")
plot(body_pc$scores[,1:2], type = "n", xlim = xlim, ylim = xlim,
     xlab = "PC1", ylab = "PC2")
lab <- cutree(cc, h = 10)  
text(body_pc$scores[,1:2], labels = lab, cex=0.6)     
plot(ca <- hclust(dm, method = "average"), main = "Average")
abline(h = 7.8, col = "lightgrey")
plot(body_pc$scores[,1:2], type = "n", xlim = xlim, ylim = xlim,
     xlab = "PC1", ylab = "PC2")
lab <- cutree(ca, h = 7.8)                             
text(body_pc$scores[,1:2], labels = lab, cex=0.6)     
par(mfrow=c(1,1))


###################################################
### code chunk for Cluster Analysis Case 2 jet.RData
###################################################
X <- scale(jet[, c("SPR", "RGF", "PLF", "SLF")],
           center = FALSE, scale = TRUE) 
dj <- dist(X)
plot(cc <- hclust(dj), main = "Jets clustering")
cc

###################################################
### code for PCA plot
###################################################
pr <- prcomp(dj)$x[, 1:2]
plot(pr, pch = (1:2)[cutree(cc, k = 2)],
     col = c("black", "darkgrey")[jet$CAR], 
     xlim = range(pr) * c(1, 1.5))
legend("topright", col = c("black", "black", 
                           "darkgrey", "darkgrey"), 
       legend = c("1 / no", "2 / no", "1 / yes", "2 / yes"), 
       pch = c(1:2, 1:2), title = "Cluster / CAR", bty = "n")



# Scripting for K-means Clustering
library("MVA")


plot(.cluster$withinss,type="b")	# Plot WGSS 


crime_pca <- prcomp(crime_s)
plot(crime_pca$x[, 1:2],pch = kmeans(crime_s, centers = 2)$cluster)

