swiss
##              Fertility Agriculture Examination Education Catholic
## Courtelary        80.2        17.0          15        12     9.96
## Delemont          83.1        45.1           6         9    84.84
## Franches-Mnt      92.5        39.7           5         5    93.40
## Moutier           85.8        36.5          12         7    33.77
## Neuveville        76.9        43.5          17        15     5.16
## Porrentruy        76.1        35.3           9         7    90.57
## Broye             83.8        70.2          16         7    92.85
## Glane             92.4        67.8          14         8    97.16
## Gruyere           82.4        53.3          12         7    97.67
## Sarine            82.9        45.2          16        13    91.38
## Veveyse           87.1        64.5          14         6    98.61
## Aigle             64.1        62.0          21        12     8.52
## Aubonne           66.9        67.5          14         7     2.27
## Avenches          68.9        60.7          19        12     4.43
## Cossonay          61.7        69.3          22         5     2.82
## Echallens         68.3        72.6          18         2    24.20
## Grandson          71.7        34.0          17         8     3.30
## Lausanne          55.7        19.4          26        28    12.11
## La Vallee         54.3        15.2          31        20     2.15
## Lavaux            65.1        73.0          19         9     2.84
## Morges            65.5        59.8          22        10     5.23
## Moudon            65.0        55.1          14         3     4.52
## Nyone             56.6        50.9          22        12    15.14
## Orbe              57.4        54.1          20         6     4.20
## Oron              72.5        71.2          12         1     2.40
## Payerne           74.2        58.1          14         8     5.23
## Paysd'enhaut      72.0        63.5           6         3     2.56
## Rolle             60.5        60.8          16        10     7.72
## Vevey             58.3        26.8          25        19    18.46
## Yverdon           65.4        49.5          15         8     6.10
## Conthey           75.5        85.9           3         2    99.71
## Entremont         69.3        84.9           7         6    99.68
## Herens            77.3        89.7           5         2   100.00
## Martigwy          70.5        78.2          12         6    98.96
## Monthey           79.4        64.9           7         3    98.22
## St Maurice        65.0        75.9           9         9    99.06
## Sierre            92.2        84.6           3         3    99.46
## Sion              79.3        63.1          13        13    96.83
## Boudry            70.4        38.4          26        12     5.62
## La Chauxdfnd      65.7         7.7          29        11    13.79
## Le Locle          72.7        16.7          22        13    11.22
## Neuchatel         64.4        17.6          35        32    16.92
## Val de Ruz        77.6        37.6          15         7     4.97
## ValdeTravers      67.6        18.7          25         7     8.65
## V. De Geneve      35.0         1.2          37        53    42.34
## Rive Droite       44.7        46.6          16        29    50.43
## Rive Gauche       42.8        27.7          22        29    58.33
##              Infant.Mortality
## Courtelary               22.2
## Delemont                 22.2
## Franches-Mnt             20.2
## Moutier                  20.3
## Neuveville               20.6
## Porrentruy               26.6
## Broye                    23.6
## Glane                    24.9
## Gruyere                  21.0
## Sarine                   24.4
## Veveyse                  24.5
## Aigle                    16.5
## Aubonne                  19.1
## Avenches                 22.7
## Cossonay                 18.7
## Echallens                21.2
## Grandson                 20.0
## Lausanne                 20.2
## La Vallee                10.8
## Lavaux                   20.0
## Morges                   18.0
## Moudon                   22.4
## Nyone                    16.7
## Orbe                     15.3
## Oron                     21.0
## Payerne                  23.8
## Paysd'enhaut             18.0
## Rolle                    16.3
## Vevey                    20.9
## Yverdon                  22.5
## Conthey                  15.1
## Entremont                19.8
## Herens                   18.3
## Martigwy                 19.4
## Monthey                  20.2
## St Maurice               17.8
## Sierre                   16.3
## Sion                     18.1
## Boudry                   20.3
## La Chauxdfnd             20.5
## Le Locle                 18.9
## Neuchatel                23.0
## Val de Ruz               20.0
## ValdeTravers             19.5
## V. De Geneve             18.0
## Rive Droite              18.2
## Rive Gauche              19.3
boxplot (swiss)

boxplot (swiss)

cor (swiss)
##                   Fertility Agriculture Examination   Education   Catholic
## Fertility         1.0000000  0.35307918  -0.6458827 -0.66378886  0.4636847
## Agriculture       0.3530792  1.00000000  -0.6865422 -0.63952252  0.4010951
## Examination      -0.6458827 -0.68654221   1.0000000  0.69841530 -0.5727418
## Education        -0.6637889 -0.63952252   0.6984153  1.00000000 -0.1538589
## Catholic          0.4636847  0.40109505  -0.5727418 -0.15385892  1.0000000
## Infant.Mortality  0.4165560 -0.06085861  -0.1140216 -0.09932185  0.1754959
##                  Infant.Mortality
## Fertility              0.41655603
## Agriculture           -0.06085861
## Examination           -0.11402160
## Education             -0.09932185
## Catholic               0.17549591
## Infant.Mortality       1.00000000
pcr <- prcomp(swiss)
pcr
## Standard deviations (1, .., p=6):
## [1] 43.835630 21.602248 12.053416  4.759160  3.657544  2.488222
## 
## Rotation (n x k) = (6 x 6):
##                          PC1         PC2         PC3         PC4         PC5
## Fertility        -0.15163143  0.14270789  0.81454413 -0.49552828  0.12247267
## Agriculture      -0.28121756  0.85914886 -0.35256541 -0.24078519  0.02235164
## Examination       0.12207834 -0.17688621 -0.18767793 -0.57042350 -0.76887882
## Education         0.06329733 -0.32260928 -0.40096045 -0.58075837  0.61904343
## Catholic         -0.93748965 -0.32543441 -0.07870742  0.04104832 -0.08547871
## Infant.Mortality -0.01131739 -0.01498883  0.10014161 -0.17923725  0.05296062
##                            PC6
## Fertility         0.1805890073
## Agriculture      -0.0006755062
## Examination       0.0450281625
## Education         0.1032102386
## Catholic         -0.0043788222
## Infant.Mortality -0.9770814149
variab<-pcr$sdev^2/sum(pcr$sdev^2)
plot(variab,type= "o", col = "blue",lwd=2 )

summary(pcr)
## Importance of components:
##                           PC1     PC2      PC3     PC4     PC5    PC6
## Standard deviation     43.836 21.6022 12.05342 4.75916 3.65754 2.4882
## Proportion of Variance  0.746  0.1812  0.05641 0.00879 0.00519 0.0024
## Cumulative Proportion   0.746  0.9272  0.98361 0.99240 0.99760 1.0000
round(cor(swiss),2)
##                  Fertility Agriculture Examination Education Catholic
## Fertility             1.00        0.35       -0.65     -0.66     0.46
## Agriculture           0.35        1.00       -0.69     -0.64     0.40
## Examination          -0.65       -0.69        1.00      0.70    -0.57
## Education            -0.66       -0.64        0.70      1.00    -0.15
## Catholic              0.46        0.40       -0.57     -0.15     1.00
## Infant.Mortality      0.42       -0.06       -0.11     -0.10     0.18
##                  Infant.Mortality
## Fertility                    0.42
## Agriculture                 -0.06
## Examination                 -0.11
## Education                   -0.10
## Catholic                     0.18
## Infant.Mortality             1.00
pairs(swiss)

det(cor(swiss))
## [1] 0.03496976
acp=prcomp(swiss,scale=TRUE)
acp
## Standard deviations (1, .., p=6):
## [1] 1.7887865 1.0900955 0.9206573 0.6625169 0.4522540 0.3476529
## 
## Rotation (n x k) = (6 x 6):
##                         PC1        PC2         PC3         PC4         PC5
## Fertility        -0.4569876  0.3220284 -0.17376638  0.53555794 -0.38308893
## Agriculture      -0.4242141 -0.4115132  0.03834472 -0.64291822 -0.37495215
## Examination       0.5097327  0.1250167 -0.09123696 -0.05446158 -0.81429082
## Education         0.4543119  0.1790495  0.53239316 -0.09738818  0.07144564
## Catholic         -0.3501111  0.1458730  0.80680494  0.09947244 -0.18317236
## Infant.Mortality -0.1496668  0.8111645 -0.16010636 -0.52677184  0.10453530
##                          PC6
## Fertility         0.47295441
## Agriculture       0.30870058
## Examination      -0.22401686
## Education         0.68081610
## Catholic         -0.40219666
## Infant.Mortality -0.07457754
acp$sdev
## [1] 1.7887865 1.0900955 0.9206573 0.6625169 0.4522540 0.3476529
acp$sdev^2
## [1] 3.1997570 1.1883082 0.8476098 0.4389287 0.2045337 0.1208626
variab2<-acp$sdev^2/sum(acp$sdev^2)
X11()
plot(variab2,type= "o", col = "red",lwd=2 )

summary(acp)
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5     PC6
## Standard deviation     1.7888 1.0901 0.9207 0.66252 0.45225 0.34765
## Proportion of Variance 0.5333 0.1981 0.1413 0.07315 0.03409 0.02014
## Cumulative Proportion  0.5333 0.7313 0.8726 0.94577 0.97986 1.00000
acp$rotation
##                         PC1        PC2         PC3         PC4         PC5
## Fertility        -0.4569876  0.3220284 -0.17376638  0.53555794 -0.38308893
## Agriculture      -0.4242141 -0.4115132  0.03834472 -0.64291822 -0.37495215
## Examination       0.5097327  0.1250167 -0.09123696 -0.05446158 -0.81429082
## Education         0.4543119  0.1790495  0.53239316 -0.09738818  0.07144564
## Catholic         -0.3501111  0.1458730  0.80680494  0.09947244 -0.18317236
## Infant.Mortality -0.1496668  0.8111645 -0.16010636 -0.52677184  0.10453530
##                          PC6
## Fertility         0.47295441
## Agriculture       0.30870058
## Examination      -0.22401686
## Education         0.68081610
## Catholic         -0.40219666
## Infant.Mortality -0.07457754
summary(acp)
## Importance of components:
##                           PC1    PC2    PC3     PC4     PC5     PC6
## Standard deviation     1.7888 1.0901 0.9207 0.66252 0.45225 0.34765
## Proportion of Variance 0.5333 0.1981 0.1413 0.07315 0.03409 0.02014
## Cumulative Proportion  0.5333 0.7313 0.8726 0.94577 0.97986 1.00000
X11()
plot(acp)

X11()
plot(acp$sdev^2)

acp$x
##                      PC1         PC2         PC3          PC4         PC5
## Courtelary    0.35966320  1.38445294 -0.85051250  0.901220398  0.62485501
## Delemont     -1.61669718  1.01507287  0.54200749  0.500510824  0.63944520
## Franches-Mnt -2.08169474  0.73804451  0.46762748  1.485847610  0.40329414
## Moutier      -0.73961186  0.58951830 -0.57296512  1.060829924  0.22746839
## Neuveville    0.37744699  0.44404417 -0.62154168  0.243445489  0.07036727
## Porrentruy   -1.35457456  2.26736561  0.34677833 -0.304490831  0.82759212
## Broye        -1.70588478  1.11569053  0.42755045 -0.462117868 -0.81689334
## Glane        -2.15919258  1.74527576  0.39402942 -0.246773891 -0.80171700
## Gruyere      -1.50143716  0.61591364  0.70040053  0.465293939 -0.20115598
## Sarine       -0.95127129  1.87485258  0.65766071 -0.001952872 -0.29680368
## Veveyse      -1.98977728  1.52487142  0.40148471 -0.284527045 -0.62028283
## Aigle         0.79645531 -1.34438945 -0.33373145 -0.076442876 -0.43498539
## Aubonne      -0.17333509 -0.87244232 -0.82401299 -0.498793337  0.18643461
## Avenches      0.23310414  0.48391431 -0.79975220 -0.951245812 -0.11611103
## Cossonay      0.41586227 -1.06046222 -0.91823904 -0.733391216 -0.53199203
## Echallens    -0.59249012 -0.29764382 -0.84865076 -0.887294641 -0.40707537
## Grandson      0.46083396  0.17817635 -0.95582929  0.464409328  0.32130229
## Lausanne      2.75464368  0.63021848  0.40597944 -0.087421447  0.25149480
## La Vallee     3.39241613 -2.05305600  0.24225300  1.694613627 -0.49963197
## Lavaux        0.15271431 -0.65027049 -0.77457628 -0.947459432 -0.31485139
## Morges        0.70627127 -0.88377615 -0.62519400 -0.219839553 -0.49024369
## Moudon       -0.24966208  0.15566824 -1.17786188 -0.780165186  0.52825321
## Nyone         1.27620439 -1.24208575 -0.14250524 -0.110975582 -0.14568957
## Orbe          0.93966875 -1.85066669 -0.59225596  0.134260798 -0.06569470
## Oron         -0.95731037 -0.40859623 -1.30693030 -0.632333638  0.18078555
## Payerne      -0.48392114  0.82395905 -1.08715051 -0.772801139  0.28086226
## Paysd'enhaut -0.93124651 -1.17366513 -0.96561988  0.127849798  0.84214339
## Rolle         0.55359958 -1.58954300 -0.34371862 -0.108163848  0.18704679
## Vevey         1.94290010  0.59705666 -0.02021011 -0.198903657  0.08201694
## Yverdon       0.12203721  0.40960015 -0.90240418 -0.676274034  0.54017227
## Conthey      -2.38317443 -2.02276611  1.04128495  0.430598800  0.13299510
## Entremont    -1.93436729 -0.71850262  1.04264011 -0.724809501  0.12990280
## Herens       -2.55908086 -1.12168536  0.82950051 -0.191494657 -0.07550697
## Martigwy     -1.50705630 -0.60173085  0.96551279 -0.447204193 -0.31782028
## Monthey      -2.08032844 -0.04531614  0.65203813  0.228938170  0.14882965
## St Maurice   -1.23144667 -1.13822925  1.32843662 -0.338197959  0.15945698
## Sierre       -2.98214753 -1.21675728  0.79135414  0.955631678 -0.30609367
## Sion         -1.06762998 -0.32474201  1.22400362  0.509840403 -0.42575724
## Boudry        1.15534712  0.37211856 -0.78337665  0.133442264 -0.59979032
## La Chauxdfnd  1.96628970  0.91988564 -0.71244741  0.773971084 -0.29115130
## Le Locle      1.29313296  0.41023891 -0.56560353  1.130072588  0.02879514
## Neuchatel     3.04976701  1.89923565  0.33960819 -0.260340420 -0.79512594
## Val de Ruz   -0.01130497  0.22092431 -1.03201259  0.643215149  0.27030163
## ValdeTravers  1.34128486  0.33591622 -0.94050348  0.780451248 -0.16578592
## V. De Geneve  5.59516797  0.55731309  2.62736774 -0.318001744  0.03805959
## Rive Droite   1.83839091 -0.70723653  1.62590990 -0.817770415  0.92780802
## Rive Gauche   2.52144140  0.01423545  1.64417740 -0.585256326  0.69047649
##                      PC6
## Courtelary    0.28033964
## Delemont      0.09025482
## Franches-Mnt  0.08627169
## Moutier       0.30665938
## Neuveville    0.75910816
## Porrentruy   -0.70174843
## Broye        -0.07757248
## Glane         0.26752807
## Gruyere      -0.22788401
## Sarine       -0.03293721
## Veveyse      -0.12334422
## Aigle         0.27378194
## Aubonne       0.29079022
## Avenches      0.37470333
## Cossonay     -0.24293294
## Echallens    -0.31848413
## Grandson     -0.02923246
## Lausanne      0.23982711
## La Vallee    -0.24037010
## Lavaux        0.27006772
## Morges        0.12051748
## Moudon       -0.33910446
## Nyone        -0.25809836
## Orbe         -0.41163208
## Oron          0.13453598
## Payerne       0.36132900
## Paysd'enhaut  0.39630123
## Rolle         0.13279485
## Vevey        -0.24947330
## Yverdon      -0.09193142
## Conthey      -0.01592934
## Entremont    -0.21341405
## Herens       -0.03701734
## Martigwy     -0.38226288
## Monthey      -0.31143720
## St Maurice   -0.28510792
## Sierre        0.64117811
## Sion          0.26705293
## Boudry       -0.01819963
## La Chauxdfnd -0.85239150
## Le Locle     -0.06110993
## Neuchatel     0.45717792
## Val de Ruz    0.21233262
## ValdeTravers -0.72666398
## V. De Geneve  0.43474730
## Rive Droite   0.22632172
## Rive Gauche  -0.37534182
predict(acp)
##                      PC1         PC2         PC3          PC4         PC5
## Courtelary    0.35966320  1.38445294 -0.85051250  0.901220398  0.62485501
## Delemont     -1.61669718  1.01507287  0.54200749  0.500510824  0.63944520
## Franches-Mnt -2.08169474  0.73804451  0.46762748  1.485847610  0.40329414
## Moutier      -0.73961186  0.58951830 -0.57296512  1.060829924  0.22746839
## Neuveville    0.37744699  0.44404417 -0.62154168  0.243445489  0.07036727
## Porrentruy   -1.35457456  2.26736561  0.34677833 -0.304490831  0.82759212
## Broye        -1.70588478  1.11569053  0.42755045 -0.462117868 -0.81689334
## Glane        -2.15919258  1.74527576  0.39402942 -0.246773891 -0.80171700
## Gruyere      -1.50143716  0.61591364  0.70040053  0.465293939 -0.20115598
## Sarine       -0.95127129  1.87485258  0.65766071 -0.001952872 -0.29680368
## Veveyse      -1.98977728  1.52487142  0.40148471 -0.284527045 -0.62028283
## Aigle         0.79645531 -1.34438945 -0.33373145 -0.076442876 -0.43498539
## Aubonne      -0.17333509 -0.87244232 -0.82401299 -0.498793337  0.18643461
## Avenches      0.23310414  0.48391431 -0.79975220 -0.951245812 -0.11611103
## Cossonay      0.41586227 -1.06046222 -0.91823904 -0.733391216 -0.53199203
## Echallens    -0.59249012 -0.29764382 -0.84865076 -0.887294641 -0.40707537
## Grandson      0.46083396  0.17817635 -0.95582929  0.464409328  0.32130229
## Lausanne      2.75464368  0.63021848  0.40597944 -0.087421447  0.25149480
## La Vallee     3.39241613 -2.05305600  0.24225300  1.694613627 -0.49963197
## Lavaux        0.15271431 -0.65027049 -0.77457628 -0.947459432 -0.31485139
## Morges        0.70627127 -0.88377615 -0.62519400 -0.219839553 -0.49024369
## Moudon       -0.24966208  0.15566824 -1.17786188 -0.780165186  0.52825321
## Nyone         1.27620439 -1.24208575 -0.14250524 -0.110975582 -0.14568957
## Orbe          0.93966875 -1.85066669 -0.59225596  0.134260798 -0.06569470
## Oron         -0.95731037 -0.40859623 -1.30693030 -0.632333638  0.18078555
## Payerne      -0.48392114  0.82395905 -1.08715051 -0.772801139  0.28086226
## Paysd'enhaut -0.93124651 -1.17366513 -0.96561988  0.127849798  0.84214339
## Rolle         0.55359958 -1.58954300 -0.34371862 -0.108163848  0.18704679
## Vevey         1.94290010  0.59705666 -0.02021011 -0.198903657  0.08201694
## Yverdon       0.12203721  0.40960015 -0.90240418 -0.676274034  0.54017227
## Conthey      -2.38317443 -2.02276611  1.04128495  0.430598800  0.13299510
## Entremont    -1.93436729 -0.71850262  1.04264011 -0.724809501  0.12990280
## Herens       -2.55908086 -1.12168536  0.82950051 -0.191494657 -0.07550697
## Martigwy     -1.50705630 -0.60173085  0.96551279 -0.447204193 -0.31782028
## Monthey      -2.08032844 -0.04531614  0.65203813  0.228938170  0.14882965
## St Maurice   -1.23144667 -1.13822925  1.32843662 -0.338197959  0.15945698
## Sierre       -2.98214753 -1.21675728  0.79135414  0.955631678 -0.30609367
## Sion         -1.06762998 -0.32474201  1.22400362  0.509840403 -0.42575724
## Boudry        1.15534712  0.37211856 -0.78337665  0.133442264 -0.59979032
## La Chauxdfnd  1.96628970  0.91988564 -0.71244741  0.773971084 -0.29115130
## Le Locle      1.29313296  0.41023891 -0.56560353  1.130072588  0.02879514
## Neuchatel     3.04976701  1.89923565  0.33960819 -0.260340420 -0.79512594
## Val de Ruz   -0.01130497  0.22092431 -1.03201259  0.643215149  0.27030163
## ValdeTravers  1.34128486  0.33591622 -0.94050348  0.780451248 -0.16578592
## V. De Geneve  5.59516797  0.55731309  2.62736774 -0.318001744  0.03805959
## Rive Droite   1.83839091 -0.70723653  1.62590990 -0.817770415  0.92780802
## Rive Gauche   2.52144140  0.01423545  1.64417740 -0.585256326  0.69047649
##                      PC6
## Courtelary    0.28033964
## Delemont      0.09025482
## Franches-Mnt  0.08627169
## Moutier       0.30665938
## Neuveville    0.75910816
## Porrentruy   -0.70174843
## Broye        -0.07757248
## Glane         0.26752807
## Gruyere      -0.22788401
## Sarine       -0.03293721
## Veveyse      -0.12334422
## Aigle         0.27378194
## Aubonne       0.29079022
## Avenches      0.37470333
## Cossonay     -0.24293294
## Echallens    -0.31848413
## Grandson     -0.02923246
## Lausanne      0.23982711
## La Vallee    -0.24037010
## Lavaux        0.27006772
## Morges        0.12051748
## Moudon       -0.33910446
## Nyone        -0.25809836
## Orbe         -0.41163208
## Oron          0.13453598
## Payerne       0.36132900
## Paysd'enhaut  0.39630123
## Rolle         0.13279485
## Vevey        -0.24947330
## Yverdon      -0.09193142
## Conthey      -0.01592934
## Entremont    -0.21341405
## Herens       -0.03701734
## Martigwy     -0.38226288
## Monthey      -0.31143720
## St Maurice   -0.28510792
## Sierre        0.64117811
## Sion          0.26705293
## Boudry       -0.01819963
## La Chauxdfnd -0.85239150
## Le Locle     -0.06110993
## Neuchatel     0.45717792
## Val de Ruz    0.21233262
## ValdeTravers -0.72666398
## V. De Geneve  0.43474730
## Rive Droite   0.22632172
## Rive Gauche  -0.37534182