data(iris)
sapply(iris[,1:4],var) #to check variability of the data
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 0.6856935 0.1899794 3.1162779 0.5810063
range(sapply(iris[,1:4],var))
## [1] 0.1899794 3.1162779
iris.stand <- as.data.frame(scale(iris[,1:4])) #to scale the data so that their sd is 1
sapply(iris.stand,sd)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1 1 1 1
pca <- prcomp(iris.stand,scale=T) #similar with princomp(): princomp(iris.stand, cor=T)
pca
## Standard deviations:
## [1] 1.7083611 0.9560494 0.3830886 0.1439265
##
## Rotation:
## PC1 PC2 PC3 PC4
## Sepal.Length 0.5210659 -0.37741762 0.7195664 0.2612863
## Sepal.Width -0.2693474 -0.92329566 -0.2443818 -0.1235096
## Petal.Length 0.5804131 -0.02449161 -0.1421264 -0.8014492
## Petal.Width 0.5648565 -0.06694199 -0.6342727 0.5235971
summary(pca) #to check the summary
## Importance of components:
## PC1 PC2 PC3 PC4
## Standard deviation 1.7084 0.9560 0.38309 0.14393
## Proportion of Variance 0.7296 0.2285 0.03669 0.00518
## Cumulative Proportion 0.7296 0.9581 0.99482 1.00000
screeplot(pca, type="lines",col=2) #screeplot of pca and we can conclude that PC1 and PC2 show 95% of the variability

pca$rotation
## PC1 PC2 PC3 PC4
## Sepal.Length 0.5210659 -0.37741762 0.7195664 0.2612863
## Sepal.Width -0.2693474 -0.92329566 -0.2443818 -0.1235096
## Petal.Length 0.5804131 -0.02449161 -0.1421264 -0.8014492
## Petal.Width 0.5648565 -0.06694199 -0.6342727 0.5235971
biplot(pca,cex=0.8, main = "Biplot of Iris Data")
abline(h = 0, v = 0, lty = 2, col = 8)
