> library(factoextra, pos=18) > library(psych, pos=20) > data(iris, package="datasets") > attach(iris) > summary(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100 setosa :50 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300 versicolor:50 Median :5.800 Median :3.000 Median :4.350 Median :1.300 virginica :50 Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800 Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500 > ###HOW MANY COMPONENTS TO EXTRACT > fa.parallel(iris[,1:4]) Parallel analysis suggests that the number of factors = 2 and the number of components = 1 > pca.iris1 <- prcomp( ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, scale. = TRUE) > pca.iris1 Standard deviations (1, .., p=4): [1] 1.7083611 0.9560494 0.3830886 0.1439265 Rotation (n x k) = (4 x 4): 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 > pca.iris1a <- prcomp(iris[,1:4], scale. = TRUE) > pca.iris1a Standard deviations (1, .., p=4): [1] 1.7083611 0.9560494 0.3830886 0.1439265 Rotation (n x k) = (4 x 4): 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 > #### NOTE THAT THERE ARE EIGENVECTORS, NOT LOADINGS! > get_eig(pca.iris1a) eigenvalue variance.percent cumulative.variance.percent Dim.1 2.91849782 72.9624454 72.96245 Dim.2 0.91403047 22.8507618 95.81321 Dim.3 0.14675688 3.6689219 99.48213 Dim.4 0.02071484 0.5178709 100.00000 > ##### USEFUL PLOTS > fviz_eig(pca.iris1) > fviz_pca_ind(pca.iris1) > fviz_pca_var (pca.iris1) > fviz_pca_biplot(pca.iris1) > fviz_pca_ind(pca.iris1, col.ind = Species) > ### EIGENVALUES > pca.iris1$sdev [1] 1.7083611 0.9560494 0.3830886 0.1439265 > #### 2 COMPONENTS > pca.iris2 <- prcomp(iris[,1:4], scale. = TRUE, rank. = 2) > pca.iris2 Standard deviations (1, .., p=4): [1] 1.7083611 0.9560494 0.3830886 0.1439265 Rotation (n x k) = (4 x 2): PC1 PC2 Sepal.Length 0.5210659 -0.37741762 Sepal.Width -0.2693474 -0.92329566 Petal.Length 0.5804131 -0.02449161 Petal.Width 0.5648565 -0.06694199 > #### SCORES FOR 2 COMPONENTS (delete 'head'!!!) > head(pca.iris2$x) PC1 PC2 [1,] -2.257141 -0.4784238 [2,] -2.074013 0.6718827 [3,] -2.356335 0.3407664 [4,] -2.291707 0.5953999 [5,] -2.381863 -0.6446757 [6,] -2.068701 -1.4842053 > #########PCA WITH ROTATION > rot.iris1 <- principal(iris[,1:4], nfactors=2, rotate="none", scores=TRUE) > rot.iris1 Principal Components Analysis Call: principal(r = iris[, 1:4], nfactors = 2, rotate = "none", scores = TRUE) Standardized loadings (pattern matrix) based upon correlation matrix PC1 PC2 h2 u2 com Sepal.Length 0.89 0.36 0.92 0.0774 1.3 Sepal.Width -0.46 0.88 0.99 0.0091 1.5 Petal.Length 0.99 0.02 0.98 0.0163 1.0 Petal.Width 0.96 0.06 0.94 0.0647 1.0 PC1 PC2 SS loadings 2.92 0.91 Proportion Var 0.73 0.23 Cumulative Var 0.73 0.96 Proportion Explained 0.76 0.24 Cumulative Proportion 0.76 1.00 Mean item complexity = 1.2 Test of the hypothesis that 2 components are sufficient. The root mean square of the residuals (RMSR) is 0.03 with the empirical chi square 1.72 with prob < NA Fit based upon off diagonal values = 1 > rot.iris2 <- principal(iris[,1:4], nfactors=2, rotate="varimax", scores=TRUE) > rot.iris2 Principal Components Analysis Call: principal(r = iris[, 1:4], nfactors = 2, rotate = "varimax", scores = TRUE) Standardized loadings (pattern matrix) based upon correlation matrix RC1 RC2 h2 u2 com Sepal.Length 0.96 0.05 0.92 0.0774 1.0 Sepal.Width -0.14 0.98 0.99 0.0091 1.0 Petal.Length 0.94 -0.30 0.98 0.0163 1.2 Petal.Width 0.93 -0.26 0.94 0.0647 1.2 RC1 RC2 SS loadings 2.70 1.13 Proportion Var 0.68 0.28 Cumulative Var 0.68 0.96 Proportion Explained 0.71 0.29 Cumulative Proportion 0.71 1.00 Mean item complexity = 1.1 Test of the hypothesis that 2 components are sufficient. The root mean square of the residuals (RMSR) is 0.03 with the empirical chi square 1.72 with prob < NA Fit based upon off diagonal values = 1 > plot (rot.iris2) > rot.iris2$scores RC1 RC2 [1,] -1.08347536 0.90672621 [2,] -1.37753582 -0.26488762 [3,] -1.41983213 0.11651976 [4,] -1.47160687 -0.14746339 [5,] -1.09529635 1.09495360 [6,] -0.63365514 1.86410357 [7,] -1.33037969 0.51537157 [8,] -1.15390444 0.64769567 [9,] -1.66842803 -0.65066663 [10,] -1.36423815 -0.04311195 [11,] -0.83620200 1.44285473 [12,] -1.23615450 0.57689252 [13,] -1.47197590 -0.29223727 [14,] -1.78019992 -0.44200579 [15,] -0.57446257 2.25287161 [16,] -0.32645115 3.07854790 [17,] -0.70827248 1.88391401 [18,] -1.03948748 0.90115058 [19,] -0.56491763 1.74730235 [20,] -0.90493951 1.55965595 [21,] -0.91478696 0.76949153 [22,] -0.89958284 1.33292766 [23,] -1.37172250 0.98306295 [24,] -0.97281113 0.43283619 [25,] -1.18019149 0.56203469 [26,] -1.28977494 -0.24186757 [27,] -1.04727433 0.63159181 [28,] -1.01436882 0.93469887 [29,] -1.07165437 0.71849882 [30,] -1.36386912 0.10166193 [31,] -1.35204813 -0.08656545 [32,] -0.86411987 0.76824550 [33,] -0.82656943 2.26719045 [34,] -0.61124808 2.58649590 [35,] -1.32025027 -0.04868757 [36,] -1.28712987 0.22024818 [37,] -0.90032089 1.04337990 [38,] -1.18973643 1.06760395 [39,] -1.64845116 -0.42456137 [40,] -1.10345223 0.68062094 [41,] -1.10859402 0.87317792 [42,] -1.82442956 -1.94528033 [43,] -1.57118874 0.01774395 [44,] -0.92066736 0.84159321 [45,] -0.78633428 1.53426989 [46,] -1.38400014 -0.30338852 [47,] -0.93027305 1.56027897 [48,] -1.45163000 0.07864188 [49,] -0.88665420 1.40992946 [50,] -1.21118998 0.43149562 [51,] 0.90267052 0.63850475 [52,] 0.60663652 0.44528272 [53,] 0.89488366 0.36894598 [54,] -0.37636164 -1.80550434 [55,] 0.52121821 -0.41135525 [56,] 0.01097051 -0.65865355 [57,] 0.67611209 0.61802926 [58,] -0.90298763 -1.73050816 [59,] 0.52232586 -0.14612607 [60,] -0.34785985 -1.02029254 [61,] -0.96975160 -2.59209874 [62,] 0.22115009 -0.14679112 [63,] -0.29469549 -1.84530377 [64,] 0.33270708 -0.32128066 [65,] -0.16873951 -0.42585268 [66,] 0.65671970 0.33343411 [67,] 0.12575650 -0.26042476 [68,] -0.18378950 -0.81034362 [69,] 0.11942001 -1.83209440 [70,] -0.35527711 -1.31416989 [71,] 0.54230218 0.24907166 [72,] 0.11950763 -0.50218942 [73,] 0.36038319 -1.15552159 [74,] 0.20610010 -0.53128206 [75,] 0.36545845 -0.19711878 [76,] 0.56763628 0.07935618 [77,] 0.66589561 -0.31690903 [78,] 0.86197815 0.06583892 [79,] 0.28893409 -0.34987634 [80,] -0.38479893 -1.03470590 [81,] -0.46301486 -1.56329521 [82,] -0.52565708 -1.55276697 [83,] -0.13312241 -0.81158966 [84,] 0.36758557 -0.82747293 [85,] 0.02485209 -0.32627530 [86,] 0.52607803 0.75031133 [87,] 0.75667059 0.31300066 [88,] 0.10187332 -1.56191261 [89,] -0.03683662 -0.22946307 [90,] -0.29909921 -1.36319902 [91,] -0.22983853 -1.15628118 [92,] 0.35268395 -0.09517539 [93,] -0.15309928 -1.03769493 [94,] -0.89116664 -1.91873555 [95,] -0.13407591 -0.89787365 [96,] -0.01171796 -0.19591478 [97,] -0.00636129 -0.42264307 [98,] 0.26455405 -0.26296932 [99,] -0.77542715 -1.43422276 [100,] -0.06364684 -0.63884312 [101,] 1.31450941 0.50346471 [102,] 0.39864481 -0.91005035 [103,] 1.40762751 0.13066401 [104,] 0.77745204 -0.32230610 [105,] 1.13024786 -0.06751063 [106,] 1.79046888 0.26062210 [107,] -0.33258920 -1.60781620 [108,] 1.41255440 -0.02772166 [109,] 0.86204467 -1.08512087 [110,] 1.90312719 1.45829751 [111,] 0.98895416 0.42061420 [112,] 0.73866669 -0.72240395 [113,] 1.18165356 0.05169864 [114,] 0.29626373 -1.38590396 [115,] 0.65721543 -0.71677583 [116,] 1.10777428 0.36105684 [117,] 0.89833332 -0.03035030 [118,] 2.21261299 2.05224040 [119,] 1.83033500 -0.61707234 [120,] 0.11178729 -1.92270799 [121,] 1.43465263 0.50587275 [122,] 0.34305083 -0.75041865 [123,] 1.73832511 -0.14813493 [124,] 0.56960926 -0.72994316 [125,] 1.28440367 0.67232612 [126,] 1.42203283 0.61766887 [127,] 0.53913393 -0.53676316 [128,] 0.58459849 -0.13233572 [129,] 0.92123668 -0.52726037 [130,] 1.21948597 0.19642002 [131,] 1.43105460 -0.21161946 [132,] 2.16957861 2.14410002 [133,] 0.96522456 -0.53283600 [134,] 0.51358550 -0.50196884 [135,] 0.38470248 -1.02931212 [136,] 1.83562516 0.30715916 [137,] 1.23453539 0.75000343 [138,] 0.88651233 0.15787709 [139,] 0.51549195 -0.16030838 [140,] 1.25208264 0.31072917 [141,] 1.32045056 0.21824654 [142,] 1.28409539 0.31443575 [143,] 0.39864481 -0.91005035 [144,] 1.42150910 0.46304226 [145,] 1.46035520 0.65002362 [146,] 1.16321412 0.02247994 [147,] 0.55498905 -1.18277671 [148,] 0.93034607 -0.02664372 [149,] 1.10278663 0.73255900 [150,] 0.52100276 -0.20809148 > rot.iris1$scores PC1 PC2 [1,] -1.32123186 0.500417476 [2,] -1.21403663 -0.702769837 [3,] -1.37929566 -0.356431814 [4,] -1.34146506 -0.622771017 [5,] -1.39423839 0.674312074 [6,] -1.21092698 1.552435768 [7,] -1.42585100 0.049668059 [8,] -1.30264721 0.232627101 [9,] -1.36203363 -1.162705285 [10,] -1.27434115 -0.488936622 [11,] -1.26382937 1.088025218 [12,] -1.35706910 0.138731323 [13,] -1.29424841 -0.759629342 [14,] -1.53615587 -1.002350233 [15,] -1.28274938 1.939069820 [16,] -1.31978019 2.800394212 [17,] -1.28791079 1.546631080 [18,] -1.27785400 0.509603506 [19,] -1.10763046 1.464702090 [20,] -1.36712589 1.175758896 [21,] -1.11681962 0.426223344 [22,] -1.28757542 0.963376853 [23,] -1.61855789 0.477813486 [24,] -1.06101521 0.089192937 [25,] -1.29933124 0.143084839 [26,] -1.13871080 -0.652194018 [27,] -1.19664555 0.252450332 [28,] -1.26515198 0.549542123 [29,] -1.24822532 0.326522879 [30,] -1.32155780 -0.352078298 [31,] -1.24855126 -0.525972895 [32,] -1.06855582 0.441693060 [33,] -1.52556552 1.869764529 [34,] -1.42710447 2.242087885 [35,] -1.23096329 -0.479750592 [36,] -1.28803984 -0.214862561 [37,] -1.19314213 0.689660204 [38,] -1.47445017 0.617452569 [39,] -1.41745219 -0.942588384 [40,] -1.26581329 0.280300576 [41,] -1.33393388 0.460478859 [42,] -1.08403212 -2.436705388 [43,] -1.48979742 -0.499452239 [44,] -1.14606246 0.492390465 [45,] -1.24676422 1.190749614 [46,] -1.20749270 -0.741257282 [47,] -1.39125779 1.168024038 [48,] -1.39688362 -0.402654117 [49,] -1.30066329 1.040351743 [50,] -1.28572055 0.009607856 [51,] 0.64278120 0.899630318 [52,] 0.42666362 0.619873154 [53,] 0.72398966 0.642477144 [54,] 0.23772638 -1.828928695 [55,] 0.62743395 -0.217274490 [56,] 0.22676092 -0.618485521 [57,] 0.43552685 0.805856126 [58,] -0.28430500 -1.931117766 [59,] 0.54133955 0.033594998 [60,] 0.00666606 -1.077941971 [61,] -0.06428872 -2.766814237 [62,] 0.25710138 -0.065983882 [63,] 0.32793504 -1.839687482 [64,] 0.41979375 -0.194135175 [65,] -0.01945920 -0.457651472 [66,] 0.51071419 0.530688304 [67,] 0.20433748 -0.204650791 [68,] 0.09265008 -0.825742897 [69,] 0.71472192 -1.691154523 [70,] 0.09621341 -1.357942306 [71,] 0.43036544 0.413417385 [72,] 0.27786684 -0.435047482 [73,] 0.72002181 -0.972972142 [74,] 0.36921066 -0.434075307 [75,] 0.40993385 -0.066105468 [76,] 0.51005289 0.261446757 [77,] 0.73304977 -0.080537751 [78,] 0.79249609 0.345385353 [79,] 0.38784578 -0.235524964 [80,] -0.02348695 -1.103691477 [81,] 0.07630615 -1.628635026 [82,] 0.01368234 -1.639272228 [83,] 0.14091388 -0.810273181 [84,] 0.61904458 -0.660768047 [85,] 0.13066964 -0.299997741 [86,] 0.25036056 0.881501429 [87,] 0.61182991 0.544227850 [88,] 0.60938156 -1.441736207 [89,] 0.04059796 -0.228827539 [90,] 0.16538115 -1.385792549 [91,] 0.16281450 -1.167605818 [92,] 0.36437519 0.025981726 [93,] 0.19633245 -1.030390082 [94,] -0.21129846 -2.105012363 [95,] 0.16836175 -0.892080585 [96,] 0.05329998 -0.188888922 [97,] 0.13285045 -0.401270965 [98,] 0.33626601 -0.161452418 [99,] -0.26116978 -1.609370229 [100,] 0.14977711 -0.624290209 [101,] 1.07612493 0.907395631 [102,] 0.67551030 -0.728556908 [103,] 1.28655677 0.585883921 [104,] 0.84018659 -0.048983557 [105,] 1.08968515 0.307577929 [106,] 1.60544806 0.834409500 [107,] 0.21411881 -1.627833419 [108,] 1.34324748 0.437909397 [109,] 1.17070464 -0.741659604 [110,] 1.31835834 2.002612295 [111,] 0.79586254 0.722183810 [112,] 0.93500574 -0.439613714 [113,] 1.09907120 0.437058808 [114,] 0.73515351 -1.211631670 [115,] 0.85622696 -0.461058685 [116,] 0.92765408 0.704970769 [117,] 0.85843587 0.266480293 [118,] 1.41552487 2.665264758 [119,] 1.93146600 0.018536260 [120,] 0.73728384 -1.779245613 [121,] 1.18880750 0.949142832 [122,] 0.57055579 -0.596052099 [123,] 1.69049530 0.431211972 [124,] 0.77781015 -0.502277907 [125,] 0.99221134 1.056991895 [126,] 1.14015786 1.050586624 [127,] 0.68555766 -0.329834481 [128,] 0.59562446 0.067079361 [129,] 1.04332669 -0.195320065 [130,] 1.08725547 0.586176074 [131,] 1.42113996 0.270298485 [132,] 1.34469915 2.737886132 [133,] 1.08670455 -0.186134035 [134,] 0.64999588 -0.305365580 [135,] 0.70152518 -0.845778845 [136,] 1.63280792 0.893199175 [137,] 0.91959065 1.113972986 [138,] 0.78542933 0.440374891 [139,] 0.53954459 0.017954714 [140,] 1.08048655 0.704849184 [141,] 1.17544418 0.639962667 [142,] 1.10950440 0.718867727 [143,] 0.67551030 -0.728556908 [144,] 1.19046549 0.904371701 [145,] 1.16572276 1.093736015 [146,] 1.09125512 0.403403877 [147,] 0.91277918 -0.934776850 [148,] 0.88745372 0.280498837 [149,] 0.80088697 1.054211137 [150,] 0.56044853 -0.025365244 ###################################SCRIPT ##################################### library(factoextra, pos=18) library(psych, pos=20) data(iris, package="datasets") attach(iris) summary(iris) ###HOW MANY COMPONENTS TO EXTRACT fa.parallel(iris[,1:4]) pca.iris1 <- prcomp( ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, scale. = TRUE) pca.iris1 pca.iris1a <- prcomp(iris[,1:4], scale. = TRUE) pca.iris1a #### NOTE THAT THERE ARE EIGENVECTORS, NOT LOADINGS! get_eig(pca.iris1a) ##### USEFUL PLOTS fviz_eig(pca.iris1) fviz_pca_ind(pca.iris1) fviz_pca_var (pca.iris1) fviz_pca_biplot(pca.iris1) fviz_pca_ind(pca.iris1, col.ind = Species) ### EIGENVALUES pca.iris1$sdev #### 2 COMPONENTS pca.iris2 <- prcomp(iris[,1:4], scale. = TRUE, rank. = 2) pca.iris2 #### SCORES FOR 2 COMPONENTS (delete 'head'!!!) head(pca.iris2$x) #########PCA WITH ROTATION rot.iris1 <- principal(iris[,1:4], nfactors=2, rotate="none", scores=TRUE) rot.iris1 rot.iris2 <- principal(iris[,1:4], nfactors=2, rotate="varimax", scores=TRUE) rot.iris2 plot (rot.iris2) rot.iris2$scores rot.iris1$scores