########----Langkah-langkah Analisis PCA----########
#UAP Analisis Multivariat Kelompok 1A
####----Package----####
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.3
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.3.3
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.3.3
## corrplot 0.92 loaded
data=read.table(file.choose(),header=T)
head(data)
## X1 X2 X3 X4 X5 X6 X7
## 1 74.70 6.03 2447345 5482.5 96 3413666 116790
## 2 75.13 5.89 7549537 15386.6 212 2710494 259705
## 3 75.64 5.94 2844925 5757.2 137 2742476 78329
## 4 74.95 4.23 3002334 6642.9 74 3191663 80998
## 5 73.73 4.53 1802264 3679.2 75 2943033 48813
## 6 73.18 4.11 4399659 8743.5 101 3404177 110150
####----Standarisasi Data----####
standardized <- scale(x=data)
head(standardized)
## X1 X2 X3 X4 X5 X6
## [1,] 0.25797144 0.99796447 -0.28150468 -0.22249807 -0.2388113 0.85245768
## [2,] 0.36694407 0.89930796 0.58063531 0.62661498 -0.1966165 -0.34239274
## [3,] 0.49619067 0.93454243 -0.21432382 -0.19894708 -0.2238976 -0.28804799
## [4,] 0.32132762 -0.27047635 -0.18772573 -0.12301293 -0.2468137 0.47522369
## [5,] 0.01214948 -0.05906955 -0.39050687 -0.37710128 -0.2464500 0.05274433
## [6,] -0.12723411 -0.35503907 0.04838647 0.05707884 -0.2369925 0.83633369
## X7
## [1,] -0.01408853
## [2,] 0.70583700
## [3,] -0.20783345
## [4,] -0.19438853
## [5,] -0.35651849
## [6,] -0.04753712
####----Menghitung Koefisien Korelasi----####
korelasi_data<-cor(data)
korelasi_standard<-cor(standardized) #standarisasi tidak mengubah nilai korelasi data awal
korelasi_standard
## X1 X2 X3 X4 X5 X6 X7
## X1 1.0000000 0.3935673 0.1104273 0.1451547 0.4911139 0.1554034 0.1286529
## X2 0.3935673 1.0000000 0.3451334 0.3913571 0.2841967 0.1568887 0.4966399
## X3 0.1104273 0.3451334 1.0000000 0.9948773 0.1295650 -0.4561994 0.9290992
## X4 0.1451547 0.3913571 0.9948773 1.0000000 0.1415750 -0.4501027 0.9543978
## X5 0.4911139 0.2841967 0.1295650 0.1415750 1.0000000 0.5160930 0.1675299
## X6 0.1554034 0.1568887 -0.4561994 -0.4501027 0.5160930 1.0000000 -0.3673741
## X7 0.1286529 0.4966399 0.9290992 0.9543978 0.1675299 -0.3673741 1.0000000
corrplot(korelasi_standard,method="square") #Variabel yang memiliki korelasi adalah X3-X4, X3-X7, dan X4-X7

####----Menghitung Matriks Varians-Kovarians----####
covariance<-cov(standardized);covariance
## X1 X2 X3 X4 X5 X6 X7
## X1 1.0000000 0.3935673 0.1104273 0.1451547 0.4911139 0.1554034 0.1286529
## X2 0.3935673 1.0000000 0.3451334 0.3913571 0.2841967 0.1568887 0.4966399
## X3 0.1104273 0.3451334 1.0000000 0.9948773 0.1295650 -0.4561994 0.9290992
## X4 0.1451547 0.3913571 0.9948773 1.0000000 0.1415750 -0.4501027 0.9543978
## X5 0.4911139 0.2841967 0.1295650 0.1415750 1.0000000 0.5160930 0.1675299
## X6 0.1554034 0.1568887 -0.4561994 -0.4501027 0.5160930 1.0000000 -0.3673741
## X7 0.1286529 0.4966399 0.9290992 0.9543978 0.1675299 -0.3673741 1.0000000
####----Menghitung Nilai Eigen dan Vektor Eigen----####
Eigens<-eigen(korelasi_standard)
Eigens #eigen value digunakan untuk menentukan PC, jika eigen value >1 maka gunakan PC yang bersesuaian
## eigen() decomposition
## $values
## [1] 3.395876666 1.996471964 0.692055234 0.655879787 0.199797480 0.058472251
## [7] 0.001446618
##
## $vectors
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.1403857 -0.48288367 0.77771409 0.21579844 0.30166348 0.06331857
## [2,] -0.2921591 -0.35565230 0.08451947 -0.81164863 -0.30839845 -0.16463836
## [3,] -0.5222005 0.10373635 -0.13248699 0.15742910 0.20524554 -0.50855679
## [4,] -0.5315210 0.08234292 -0.10330306 0.11723153 0.18310909 -0.23074257
## [5,] -0.1107425 -0.58334189 -0.29391695 0.48464943 -0.57092698 -0.01416610
## [6,] 0.2276950 -0.53045688 -0.49414830 -0.13901265 0.63469980 -0.01580658
## [7,] -0.5249934 0.03348073 -0.17094797 -0.04382694 0.09712577 0.81028532
## [,7]
## [1,] -0.026399864
## [2,] -0.004700866
## [3,] -0.611107924
## [4,] 0.774286295
## [5,] 0.004218856
## [6,] 0.013027649
## [7,] -0.161620757
Nilai_Eigen=eigen(covariance)$value;Nilai_Eigen
## [1] 3.395876666 1.996471964 0.692055234 0.655879787 0.199797480 0.058472251
## [7] 0.001446618
Vektor_Eigen=eigen(covariance)$vector;Vektor_Eigen
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.1403857 -0.48288367 0.77771409 0.21579844 0.30166348 0.06331857
## [2,] -0.2921591 -0.35565230 0.08451947 -0.81164863 -0.30839845 -0.16463836
## [3,] -0.5222005 0.10373635 -0.13248699 0.15742910 0.20524554 -0.50855679
## [4,] -0.5315210 0.08234292 -0.10330306 0.11723153 0.18310909 -0.23074257
## [5,] -0.1107425 -0.58334189 -0.29391695 0.48464943 -0.57092698 -0.01416610
## [6,] 0.2276950 -0.53045688 -0.49414830 -0.13901265 0.63469980 -0.01580658
## [7,] -0.5249934 0.03348073 -0.17094797 -0.04382694 0.09712577 0.81028532
## [,7]
## [1,] -0.026399864
## [2,] -0.004700866
## [3,] -0.611107924
## [4,] 0.774286295
## [5,] 0.004218856
## [6,] 0.013027649
## [7,] -0.161620757
####----Menentukan Jumlah Komponen Utama dan Persamaan Komponen Utama----####
####----Membentuk Komponen Matriks Korelasi----####
#corPC1
corX1PC1=-0.1403857*sqrt(3.395876666);corX1PC1
## [1] -0.2587014
corX2PC1=-0.2921591*sqrt(3.395876666);corX2PC1
## [1] -0.538388
corX3PC1=-0.5222005*sqrt(3.395876666);corX3PC1
## [1] -0.9623061
corX4PC1=-0.5315210*sqrt(3.395876666);corX4PC1
## [1] -0.9794818
corX5PC1=-0.1107425*sqrt(3.395876666);corX5PC1
## [1] -0.2040752
corX6PC1= 0.2276950*sqrt(3.395876666);corX6PC1
## [1] 0.4195942
corX7PC1=-0.5249934*sqrt(3.395876666);corX7PC1
## [1] -0.9674528
#corPC2
corX1PC2=-0.48288367*sqrt(1.996471964);corX1PC2
## [1] -0.682298
corX2PC2=-0.35565230*sqrt(1.996471964);corX2PC2
## [1] -0.5025245
corX3PC2= 0.10373635*sqrt(1.996471964);corX3PC2
## [1] 0.1465759
corX4PC2= 0.08234292*sqrt(1.996471964);corX4PC2
## [1] 0.1163477
corX5PC2=-0.58334189*sqrt(1.996471964);corX5PC2
## [1] -0.8242421
corX6PC2=-0.53045688*sqrt(1.996471964);corX6PC2
## [1] -0.7495174
corX7PC2= 0.03348073*sqrt(1.996471964);corX7PC2
## [1] 0.04730712
KomponenMatriksKorelasi = data.frame(corPC1 = c(corX1PC1,corX2PC1,corX3PC1,corX4PC1,corX5PC1,corX6PC1,corX7PC1),
corPC2 = c(corX1PC2,corX2PC2,corX3PC2,corX4PC2,corX5PC2,corX6PC2,corX7PC2))
KomponenMatriksKorelasi
## corPC1 corPC2
## 1 -0.2587014 -0.68229804
## 2 -0.5383880 -0.50252449
## 3 -0.9623061 0.14657590
## 4 -0.9794818 0.11634772
## 5 -0.2040752 -0.82424206
## 6 0.4195942 -0.74951736
## 7 -0.9674528 0.04730712
####----Membentuk PCA----####
pca <- prcomp(x=data, scale. = TRUE, center = TRUE)
summary(pca)
## Importance of components:
## PC1 PC2 PC3 PC4 PC5 PC6 PC7
## Standard deviation 1.8428 1.4130 0.83190 0.8099 0.44699 0.24181 0.03803
## Proportion of Variance 0.4851 0.2852 0.09887 0.0937 0.02854 0.00835 0.00021
## Cumulative Proportion 0.4851 0.7703 0.86920 0.9629 0.99144 0.99979 1.00000
pca$x
## PC1 PC2 PC3 PC4 PC5
## [1,] 0.16542775 -0.840377073 0.003385538 -1.058352159 0.34756103
## [2,] -1.37726883 -0.065250776 -0.326049043 -0.564496025 0.03075217
## [3,] -0.05670948 -0.334142111 -0.757502898 -0.767867410 -0.29412767
## [4,] 0.43491829 -0.203188709 -0.135561411 0.067738489 0.54294951
## [5,] 0.64638418 0.047429566 -0.202467512 -0.166268603 0.01223617
## [6,] 0.30761455 -0.109553655 0.476756849 0.045983079 0.75300356
## [7,] 0.86319573 0.693805673 -0.741318038 0.599647815 -0.33931609
## [8,] -0.04152246 0.612266696 -0.011887131 0.166543448 -0.17679392
## [9,] 1.11576991 -0.547208621 0.123282128 -0.335017143 0.54318106
## [10,] 0.30864918 -1.546695645 -1.140226670 -1.250096136 0.19006398
## [11,] -0.99827937 -6.699842927 1.366954816 1.722321080 -0.60192113
## [12,] -6.98876400 0.718825648 0.590612266 -0.526529816 0.07784289
## [13,] -4.07556153 1.234581080 0.156576346 0.621314284 -0.06289896
## [14,] 0.16720621 -0.007736436 -2.303219711 1.152628807 -0.50653298
## [15,] -4.65089395 1.206034580 0.129431614 0.869065739 0.41766916
## [16,] -1.50606670 -0.797658096 -0.536677222 -1.342784906 -0.67082355
## [17,] 0.69689140 0.056716372 -1.043227451 1.332936953 0.37495763
## [18,] 0.69327803 1.147514147 -0.265578091 0.972956311 -0.31805991
## [19,] 0.58830888 1.811361004 0.299722538 0.584070997 -0.91219639
## [20,] 0.27074055 0.661365635 0.190085284 -0.530939671 -0.62352205
## [21,] 0.94759139 -0.063904528 -0.013361594 -0.004624436 0.33741211
## [22,] 0.68666307 -0.197277227 -0.183313933 -0.018846769 0.36460496
## [23,] 0.40272892 -0.915479012 -0.910962035 -0.434153401 0.47950168
## [24,] 1.27071585 -0.035205195 0.156131542 -0.058670585 0.29145371
## [25,] 0.57897767 -1.035116488 -0.109508365 -1.125670055 0.29750212
## [26,] 1.02302954 0.999158513 -0.021996428 0.693228958 -0.19716303
## [27,] 0.13523272 -0.336002111 0.195998171 0.027246758 0.78939727
## [28,] 1.03687419 0.636888061 -0.159267249 0.607770831 0.01035252
## [29,] 1.37298971 0.627560428 0.333898428 0.485212619 0.08826028
## [30,] 1.51548933 1.115648001 0.573138950 0.888682104 0.03374464
## [31,] 0.45661386 -0.185162267 -0.273804377 -1.253681112 -0.64861229
## [32,] 1.08326365 0.374508910 0.307747633 -0.253571330 -0.19692992
## [33,] 1.22035026 0.702036740 1.703802717 -1.289935813 -0.69367845
## [34,] 1.70616150 1.274099822 2.528404338 0.144157096 0.26012989
## PC6 PC7
## [1,] -0.025024895 0.0006258284
## [2,] -0.015427704 -0.0029342254
## [3,] 0.128221634 -0.0116665194
## [4,] -0.027204857 0.0488289160
## [5,] -0.009878474 0.0038826128
## [6,] 0.035761968 0.0372325697
## [7,] -0.139680361 -0.0146849455
## [8,] 0.090837005 0.0343804985
## [9,] -0.003185981 0.0038942884
## [10,] 0.170731107 -0.0341306171
## [11,] 0.022433772 0.0070032151
## [12,] -0.899068965 0.0028598022
## [13,] 0.670531114 0.0334093631
## [14,] -0.125208381 -0.0752062657
## [15,] 0.623103305 -0.0504520180
## [16,] -0.108890077 -0.0146687478
## [17,] -0.138440265 -0.0444298767
## [18,] 0.010992535 0.0351953003
## [19,] -0.026035042 0.0285130467
## [20,] 0.088762022 0.0197815222
## [21,] -0.053156745 0.0085221130
## [22,] -0.011832501 0.0100671971
## [23,] 0.006597383 -0.0157815097
## [24,] -0.068682969 0.0020586404
## [25,] 0.103987334 -0.0062733509
## [26,] -0.110216929 0.0208402614
## [27,] 0.014361585 0.0495631971
## [28,] -0.115396357 0.0151890781
## [29,] -0.129395957 0.0153671345
## [30,] -0.193359653 0.0298925392
## [31,] 0.134021602 -0.0088288027
## [32,] -0.029798355 0.0102187054
## [33,] 0.177873602 0.0107515720
## [34,] -0.048331498 -0.1490205225
####----Rekonstruksi Data----####
head(pca)$x[,1:2]
## PC1 PC2
## [1,] 0.16542775 -0.840377073
## [2,] -1.37726883 -0.065250776
## [3,] -0.05670948 -0.334142111
## [4,] 0.43491829 -0.203188709
## [5,] 0.64638418 0.047429566
## [6,] 0.30761455 -0.109553655
## [7,] 0.86319573 0.693805673
## [8,] -0.04152246 0.612266696
## [9,] 1.11576991 -0.547208621
## [10,] 0.30864918 -1.546695645
## [11,] -0.99827937 -6.699842927
## [12,] -6.98876400 0.718825648
## [13,] -4.07556153 1.234581080
## [14,] 0.16720621 -0.007736436
## [15,] -4.65089395 1.206034580
## [16,] -1.50606670 -0.797658096
## [17,] 0.69689140 0.056716372
## [18,] 0.69327803 1.147514147
## [19,] 0.58830888 1.811361004
## [20,] 0.27074055 0.661365635
## [21,] 0.94759139 -0.063904528
## [22,] 0.68666307 -0.197277227
## [23,] 0.40272892 -0.915479012
## [24,] 1.27071585 -0.035205195
## [25,] 0.57897767 -1.035116488
## [26,] 1.02302954 0.999158513
## [27,] 0.13523272 -0.336002111
## [28,] 1.03687419 0.636888061
## [29,] 1.37298971 0.627560428
## [30,] 1.51548933 1.115648001
## [31,] 0.45661386 -0.185162267
## [32,] 1.08326365 0.374508910
## [33,] 1.22035026 0.702036740
## [34,] 1.70616150 1.274099822
####----Visualisasi Data Hasil Rekonstruksi----####
fviz_pca(pca)
