You are on page 1of 13

Nama : Muammar Qois Al Qorni

NIM : 082111633017
#1.Carilah data yang sesuai untuk PCA dengan covarians
library (DT )
library (factoextra )

## Loading required package: ggplot2

## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa

library (ggplot2)
library (rgl)
summary(mtcars)

## mpg cyl disp hp


## Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
## 1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
## Median :19.20 Median :6.000 Median :196.3 Median :123.0
## Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
## 3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
## Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
## drat wt qsec vs
## Min. :2.760 Min. :1.513 Min. :14.50 Min. :0.0000
## 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 1st Qu.:0.0000
## Median :3.695 Median :3.325 Median :17.71 Median :0.0000
## Mean :3.597 Mean :3.217 Mean :17.85 Mean :0.4375
## 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 3rd Qu.:1.0000
## Max. :4.930 Max. :5.424 Max. :22.90 Max. :1.0000
## am gear carb
## Min. :0.0000 Min. :3.000 Min. :1.000
## 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000
## Median :0.0000 Median :4.000 Median :2.000
## Mean :0.4062 Mean :3.688 Mean :2.812
## 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000
## Max. :1.0000 Max. :5.000 Max. :8.000

mtcars_pca <- prcomp(x = mtcars, scale. = TRUE , center = TRUE )


mtcars.pca <- princomp(x = mtcars, cor = FALSE )

#A.Tampilkan eigenvalue-nya (variansnya) table dan grafik


mtcars_standardized <- scale(x = mtcars)
mtcars_covariance <- cov(mtcars_standardized)
mtcars_eigen <- eigen(mtcars_covariance)
mtcars_eigen

mtcars_pca$rotation
## eigen() decomposition
## $values
## [1] 6.60840025 2.65046789 0.62719727 0.26959744 0.22345110 0.21159612
## [7] 0.13526199 0.12290143 0.07704665 0.05203544 0.02204441
##
## $vectors
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 0.3625305 -0.01612440 -0.22574419 0.022540255 -0.10284468 0.10879743
## [2,] -0.3739160 -0.04374371 -0.17531118 0.002591838 -0.05848381 -0.16855369
## [3,] -0.3681852 0.04932413 -0.06148414 -0.256607885 -0.39399530 0.33616451
## [4,] -0.3300569 -0.24878402 0.14001476 0.067676157 -0.54004744 -0.07143563
## [5,] 0.2941514 -0.27469408 0.16118879 -0.854828743 -0.07732727 -0.24449705
## [6,] -0.3461033 0.14303825 0.34181851 -0.245899314 0.07502912 0.46493964
## [7,] 0.2004563 0.46337482 0.40316904 -0.068076532 0.16466591 0.33048032
## [8,] 0.3065113 0.23164699 0.42881517 0.214848616 -0.59953955 -0.19401702
## [9,] 0.2349429 -0.42941765 -0.20576657 0.030462908 -0.08978128 0.57081745
## [10,] 0.2069162 -0.46234863 0.28977993 0.264690521 -0.04832960 0.24356284
## [11,] -0.2140177 -0.41357106 0.52854459 0.126789179 0.36131875 -0.18352168
## [,7] [,8] [,9] [,10] [,11]
## [1,] 0.367723810 -0.754091423 0.235701617 0.13928524 0.124895628
## [2,] 0.057277736 -0.230824925 0.054035270 -0.84641949 0.140695441
## [3,] 0.214303077 0.001142134 0.198427848 0.04937979 -0.660606481
## [4,] -0.001495989 -0.222358441 -0.575830072 0.24782351 0.256492062
## [5,] 0.021119857 0.032193501 -0.046901228 -0.10149369 0.039530246
## [6,] -0.020668302 -0.008571929 0.359498251 0.09439426 0.567448697
## [7,] 0.050010522 -0.231840021 -0.528377185 -0.27067295 -0.181361780
## [8,] -0.265780836 0.025935128 0.358582624 -0.15903909 -0.008414634
## [9,] -0.587305101 -0.059746952 -0.047403982 -0.17778541 -0.029823537
## [10,] 0.605097617 0.336150240 -0.001735039 -0.21382515 0.053507085
## [11,] -0.174603192 -0.395629107 0.170640677 0.07225950 -0.319594676

## PC1 PC2 PC3 PC4 PC5 PC6


## mpg -0.3625305 0.01612440 -0.22574419 -0.022540255 0.10284468 -0.10879743
## cyl 0.3739160 0.04374371 -0.17531118 -0.002591838 0.05848381 0.16855369
## disp 0.3681852 -0.04932413 -0.06148414 0.256607885 0.39399530 -0.33616451
## hp 0.3300569 0.24878402 0.14001476 -0.067676157 0.54004744 0.07143563
## drat -0.2941514 0.27469408 0.16118879 0.854828743 0.07732727 0.24449705
## wt 0.3461033 -0.14303825 0.34181851 0.245899314 -0.07502912 -0.46493964
## qsec -0.2004563 -0.46337482 0.40316904 0.068076532 -0.16466591 -0.33048032
## vs -0.3065113 -0.23164699 0.42881517 -0.214848616 0.59953955 0.19401702
## am -0.2349429 0.42941765 -0.20576657 -0.030462908 0.08978128 -0.57081745
## gear -0.2069162 0.46234863 0.28977993 -0.264690521 0.04832960 -0.24356284
## carb 0.2140177 0.41357106 0.52854459 -0.126789179 -0.36131875 0.18352168
## PC7 PC8 PC9 PC10 PC11
## mpg 0.367723810 -0.754091423 0.235701617 0.13928524 -0.124895628
## cyl 0.057277736 -0.230824925 0.054035270 -0.84641949 -0.140695441
## disp 0.214303077 0.001142134 0.198427848 0.04937979 0.660606481
## hp -0.001495989 -0.222358441 -0.575830072 0.24782351 -0.256492062
## drat 0.021119857 0.032193501 -0.046901228 -0.10149369 -0.039530246
## wt -0.020668302 -0.008571929 0.359498251 0.09439426 -0.567448697
## qsec 0.050010522 -0.231840021 -0.528377185 -0.27067295 0.181361780
## vs -0.265780836 0.025935128 0.358582624 -0.15903909 0.008414634
## am -0.587305101 -0.059746952 -0.047403982 -0.17778541 0.029823537
## gear 0.605097617 0.336150240 -0.001735039 -0.21382515 -0.053507085
## carb -0.174603192 -0.395629107 0.170640677 0.07225950 0.319594676

data( "mtcars")
datatable(mtcars)

fviz_eig(mtcars_pca, main = "Grafik Prcomp" ) #prcomp


## mpg cyl disp hp drat wt
## mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.68117191 -0.8676594
## cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.69993811 0.7824958
## disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.71021393 0.8879799
## hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.44875912 0.6587479
## drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.00000000 -0.7124406
## wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.71244065 1.0000000
## qsec 0.4186840 -0.5912421 -0.4336979 -0.7082234 0.09120476 -0.1747159
## vs 0.6640389 -0.8108118 -0.7104159 -0.7230967 0.44027846 -0.5549157
## am 0.5998324 -0.5226070 -0.5912270 -0.2432043 0.71271113 -0.6924953
## gear 0.4802848 -0.4926866 -0.5555692 -0.1257043 0.69961013 -0.5832870
## carb -0.5509251 0.5269883 0.3949769 0.7498125 -0.09078980 0.4276059
## qsec vs am gear carb
## mpg 0.41868403 0.6640389 0.59983243 0.4802848 -0.55092507
## cyl -0.59124207 -0.8108118 -0.52260705 -0.4926866 0.52698829
## disp -0.43369788 -0.7104159 -0.59122704 -0.5555692 0.39497686
## hp -0.70822339 -0.7230967 -0.24320426 -0.1257043 0.74981247
## drat 0.09120476 0.4402785 0.71271113 0.6996101 -0.09078980
## wt -0.17471588 -0.5549157 -0.69249526 -0.5832870 0.42760594
## qsec 1.00000000 0.7445354 -0.22986086 -0.2126822 -0.65624923
## vs 0.74453544 1.0000000 0.16834512 0.2060233 -0.56960714
## am -0.22986086 0.1683451 1.00000000 0.7940588 0.05753435
## gear -0.21268223 0.2060233 0.79405876 1.0000000 0.27407284
## carb -0.65624923 -0.5696071 0.05753435 0.2740728 1.00000000

fviz_eig(mtcars.pca, main = "Grafik Princomp" ) #princomp

#B. Tampilkan matriks PC-nya


mtcars_covariance <- cov(mtcars_standardized)
mtcars_covariance
mtcars_pca$rotation

## PC1 PC2 PC3 PC4 PC5 PC6


## mpg -0.3625305 0.01612440 -0.22574419 -0.022540255 0.10284468 -0.10879743
## cyl 0.3739160 0.04374371 -0.17531118 -0.002591838 0.05848381 0.16855369
## disp 0.3681852 -0.04932413 -0.06148414 0.256607885 0.39399530 -0.33616451
## hp 0.3300569 0.24878402 0.14001476 -0.067676157 0.54004744 0.07143563
## drat -0.2941514 0.27469408 0.16118879 0.854828743 0.07732727 0.24449705
## wt 0.3461033 -0.14303825 0.34181851 0.245899314 -0.07502912 -0.46493964
## qsec -0.2004563 -0.46337482 0.40316904 0.068076532 -0.16466591 -0.33048032
## vs -0.3065113 -0.23164699 0.42881517 -0.214848616 0.59953955 0.19401702
## am -0.2349429 0.42941765 -0.20576657 -0.030462908 0.08978128 -0.57081745
## gear -0.2069162 0.46234863 0.28977993 -0.264690521 0.04832960 -0.24356284
## carb 0.2140177 0.41357106 0.52854459 -0.126789179 -0.36131875 0.18352168
## PC7 PC8 PC9 PC10 PC11
## mpg 0.367723810 -0.754091423 0.235701617 0.13928524 -0.124895628
## cyl 0.057277736 -0.230824925 0.054035270 -0.84641949 -0.140695441
## disp 0.214303077 0.001142134 0.198427848 0.04937979 0.660606481
## hp -0.001495989 -0.222358441 -0.575830072 0.24782351 -0.256492062
## drat 0.021119857 0.032193501 -0.046901228 -0.10149369 -0.039530246
## wt -0.020668302 -0.008571929 0.359498251 0.09439426 -0.567448697
## qsec 0.050010522 -0.231840021 -0.528377185 -0.27067295 0.181361780
## vs -0.265780836 0.025935128 0.358582624 -0.15903909 0.008414634
## am -0.587305101 -0.059746952 -0.047403982 -0.17778541 0.029823537
## gear 0.605097617 0.336150240 -0.001735039 -0.21382515 -0.053507085
## carb -0.174603192 -0.395629107 0.170640677 0.07225950 0.319594676

#c.Plot di 2-dimensi untuk individunya


fviz_pca_ind(mtcars_pca) # Graph of individuals #prcomp
fviz_pca_ind(mtcars.pca) # Graph of individuals #princomp

#D.Plot di 2-dimensi untuk variabelnya


fviz_pca_var(mtcars_pca) # Graph of variables #prcomp
fviz_pca_var(mtcars.pca) # Graph of variables #princomp

#E.Bi-Plot di 2-dimensi untuk individu dan variabelnya


fviz_pca_biplot(mtcars_pca, repel = TRUE ,
col.var = "#2E9FDF" , # Variables color
col.ind = "#696969" # Individuals color
) #prcomp

## Warning: ggrepel: 7 unlabeled data points (too many overlaps). Consider


## increasing max.overlaps

#F. Plot PC di 3-dimensi


plot3d(mtcars_pca$x[, 1:3], col = mtcars$hp )
plot3d(mtcars.pca$scores[, 1:3], col = mtcars$mpg )

#2. Carilah data yang sesuai untuk PCA dengan correlation


library (DT )
library (factoextra )
library (ggplot2)
str(trees)
## 'data.frame': 31 obs. of 3 variables:
## $ Girth : num 8.3 8.6 8.8 10.5 10.7 10.8 11 11 11.1 11.2 ...
## $ Height: num 70 65 63 72 81 83 66 75 80 75 ...
## $ Volume: num 10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 ...

fviz_pca_biplot(mtcars.pca, repel = TRUE ,


col.var = "#2E9FDF" , # Variables color
col.ind = "#696969" # Individuals color
) #princomp

## Warning: ggrepel: 31 unlabeled data points (too many overlaps). Consider


## increasing max.overlaps

## Warning: ggrepel: 9 unlabeled data points (too many overlaps). Consider


## increasing max.overlaps

trees_pca <- prcomp(x = trees, scale. = TRUE , center = TRUE )


trees.pca <- princomp(x = trees, cor = FALSE )

#A.Tampilkan eigenvalue-nya (variansnya) table dan grafik


trees_standardized <- scale(x = trees)
trees_correlation <- cor(trees_standardized,use= 'complete.obs' )
trees_eigen <- eigen(trees_correlation)
trees_eigen

## eigen() decomposition
## $values
## [1] 2.4103000 0.5616173 0.0280827
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.6085705 0.4099013 0.67942837
## [2,] -0.4891267 -0.8680065 0.08555556
## [3,] -0.6248176 0.2802600 -0.72873681

data( "trees")
datatable(trees)

fviz_eig(trees_pca, main = "Grafik Prcomp" ) #prcomp


fviz_eig(trees.pca, main = "Grafik Princomp" ) #princomp
fviz_pca_ind(trees.pca) # Graph of individuals #princomp

#B. Tampilkan matriks PC-nya


trees_correlation <- cor(trees_standardized,use= 'complete.obs' )
trees_correlation

## Girth Height Volume


## Girth 1.0000000 0.5192801 0.9671194
## Height 0.5192801 1.0000000 0.5982497
## Volume 0.9671194 0.5982497 1.0000000

trees_pca$rotation

## PC1 PC2 PC3


## Girth 0.6085705 0.4099013 0.67942837
## Height 0.4891267 -0.8680065 0.08555556
## Volume 0.6248176 0.2802600 -0.72873681

#C.Plot di 2-dimensi untuk individunya


fviz_pca_ind(trees_pca) # Graph of individuals #prcomp
#D.Plot di 2-dimensi untuk variabelnya
fviz_pca_var(trees_pca, col.var = "green", col.ind = "gray") # comp of variables #prcomp
fviz_pca_var(trees.pca, col.var = "orange", col.ind = "red") # Graph of variables #princomp

## Warning: ggrepel: 4 unlabeled data points (too many overlaps). Consider


## increasing max.overlaps
#E.Bi-Plot di 2-dimensi untuk individu dan variabelnya
fviz_pca_biplot(trees_pca, repel = TRUE ,
col.var = "blue", # Variables color
col.ind = "green" # Individuals color
) #prcomp

fviz_pca_biplot(trees.pca, repel = TRUE ,


col.var = "orange", # Variables color
col.ind = "purple" # Individuals color
) #princomp
#F. Plot PC di 3-dimensi
plot3d(trees_pca$x[, 1:3], col = trees$Girth )
plot3d(trees.pca$scores[, 1:3], col = trees$Height )

You might also like