myPCA <- function(X) {
X_mat <- as.matrix(X)
object_names <- rownames(X)
var_names <- colnames(X)
### 1. Zentrierung der Daten (wird für die Berechnung von Z gebraucht)
X_cent <- scale(X_mat, center = TRUE, scale = FALSE)
### 2. Berechnung der Kovarianzmatrix S
# (cov() teilt durch n - 1; FactoMineR::PCA() teilt durch n)
X_cov <- cov(X_cent)
### 3. Berechnung der Eigenwerte lambda und der Eigenvektoren v
# der Kovarianzmatrix S (s. Legendre & Legendre, 1998, eq. 9.1 und 9.2)
# Die Eigenwerte und -vektoren erfüllen die Gleichung S*v = lambda*v.
X_eig <- eigen(X_cov)
# (Eigenwerte sind ein Maß für die Bedeutung (Varianz) der Achsen. Sie
# können als "Proportions Explained" (Anteil der erklärten Variation)
# ausgedrückt werden, indem sie durch die Summe aller Eigenwerte geteilt
# werden.)
### 4. Eigenvektoren als Matrix U abspeichern
U <- X_eig$vectors
rownames(U) <- var_names
### 5. Matrix Z berechnen
Z <- X_cent%*%U # (s. Legendre & Legendre, 1998, eq. 9.4)
rownames(Z) <- object_names
# Output als Liste mit allen 3 Matrizen
result <- list(X_eig$values,U,Z)
names(result) <- c("eigenvalues","U", "Z")
return(result)
}
myPCA(X = spiderA_hell)$eigenvalues
[1] 0.248053334 0.119191137 0.064210974 0.015898351 0.014478086 0.008943353
[7] 0.007839521 0.004929006 0.003947960 0.003283669 0.002067132 0.001010258
$U
[,1] [,2] [,3] [,4] [,5]
Alopacce 0.44831003 -0.01051498 -0.07281611 -0.05666960 -0.429319959
Alopcune -0.12785124 0.12757386 0.04630874 -0.46260980 -0.063937831
Alopfabr 0.43329110 -0.23978183 -0.27375823 0.04070122 -0.405977175
Arctlute -0.03320900 0.07458943 -0.09454273 0.02086127 -0.009869284
Arctperi 0.25455090 -0.23512646 -0.31188298 -0.08422874 0.597339052
Auloalbi -0.09486432 0.18192790 -0.21534250 -0.22044311 -0.157217302
Pardlugu -0.26588116 -0.40651236 0.31235772 -0.63247655 -0.161965294
Pardmont 0.29050105 0.55910845 0.60653125 0.04986589 -0.002808814
Pardnigr -0.16547594 0.31962467 -0.35064846 -0.13755907 -0.292474173
Pardpull -0.17877235 0.47872277 -0.38711064 -0.18028625 0.190225844
Trocterr -0.50546184 -0.15069924 0.07823190 0.47356014 -0.129189928
Zoraspin -0.22386514 0.01652224 -0.14370560 0.21633601 -0.317005579
[,6] [,7] [,8] [,9] [,10]
Alopacce 0.49272342 0.18466612 0.34668564 -0.06888300 -0.32976581
Alopcune 0.00869231 0.54206663 -0.17530396 0.26110454 -0.36795474
Alopfabr -0.12215228 -0.15320428 -0.42885049 0.46681999 0.22451984
Arctlute -0.09536584 0.03158727 0.09488580 0.14706014 0.15060798
Arctperi -0.23357564 0.05950429 -0.06269729 0.20023873 -0.42289897
Auloalbi 0.06907352 -0.39087624 -0.55393942 -0.47133534 -0.34633740
Pardlugu -0.03071048 -0.27033172 0.10451502 0.19386871 0.08069962
Pardmont -0.20523668 -0.13032571 -0.15930349 0.26180267 -0.13940378
Pardnigr -0.43954702 0.38857799 0.02503611 -0.09939054 0.23721921
Pardpull 0.43081871 -0.30728916 0.14148203 0.43194329 0.13336627
Trocterr 0.27999686 0.19687437 -0.33892083 0.30333285 -0.19205885
Zoraspin -0.41657377 -0.34208897 0.41661663 0.17518388 -0.49707827
[,11] [,12]
Alopacce -0.298769340 0.079094537
Alopcune 0.467679968 0.014300325
Alopfabr 0.133002680 -0.070856328
Arctlute 0.007005019 0.959379187
Arctperi -0.370414932 0.023756448
Auloalbi -0.064079096 0.166136008
Pardlugu -0.332793143 0.020824909
Pardmont -0.245158268 0.008432984
Pardnigr -0.460094797 -0.142758191
Pardpull -0.024704238 -0.123696701
Trocterr -0.335898267 0.031280077
Zoraspin 0.192304900 -0.052734797
$Z
[,1] [,2] [,3] [,4] [,5] [,6]
1 -0.00348869 0.42904434 0.022619718 0.0164596268 0.0030033653 0.16430845
2 -0.42363467 0.20940431 -0.307307540 0.0121736335 0.0328212032 0.07845732
3 -0.10011058 0.34027054 -0.101219556 -0.0852068648 -0.0365128498 0.16318337
4 -0.34709362 0.31362632 -0.307000843 -0.0061576386 0.0217755872 0.07745555
5 -0.36514474 0.29956651 -0.373991345 -0.0763136220 -0.0234134817 -0.09213171
6 -0.36314292 0.29690641 -0.207276809 0.1288208036 -0.0267560721 -0.16899310
7 -0.29312155 0.38668018 -0.242016280 0.0007510936 0.0204879398 -0.02803255
8 -0.40930399 -0.24418118 0.180879100 -0.4122434951 -0.0378017133 -0.05614103
9 0.27297341 0.49648011 0.417028630 -0.0035661001 0.1365744813 -0.11392932
10 0.53946446 0.29439670 0.471452311 0.1061717673 0.0280403531 -0.07666919
11 0.48711189 0.37772367 0.429801961 0.0184710350 0.0425896938 -0.01234774
12 0.28066617 0.44788155 0.369791458 -0.0474661840 0.0955881041 0.06077478
13 -0.27677596 0.34684158 -0.210586736 -0.0864275332 -0.0342815210 -0.02221359
14 -0.38460016 0.25414702 -0.166567533 0.0960156276 0.0642535779 0.01435748
15 -0.46993004 -0.42672618 0.141468142 0.1857220460 0.0036922271 0.01742715
16 -0.50324445 -0.29742947 0.058442677 0.0410831630 -0.0647257200 -0.09110856
17 -0.45812202 -0.37687210 0.103961344 0.3048966089 0.0270791333 0.05066370
18 -0.44197114 -0.40731940 0.189872770 0.0692531934 0.0841290167 0.15464581
19 -0.49448911 -0.39412742 0.129287492 -0.0501828012 -0.0551902786 -0.07729363
20 -0.47110046 -0.44689513 0.202896492 -0.0841753522 -0.0001323746 0.01417256
21 -0.42528089 -0.30404168 0.249080287 -0.1482793110 0.0331521793 0.02770215
22 0.75034720 -0.28084090 -0.192237731 0.0520086321 -0.0333842498 -0.02438719
23 0.72339108 -0.26878718 -0.040124024 -0.1051793327 -0.0303576567 0.01130256
24 0.86178147 -0.23805681 -0.192309127 -0.0361985495 -0.0289536231 -0.05237930
25 0.15511630 0.03511414 0.002250673 0.0832331033 -0.3600677883 -0.14625294
26 0.59758036 -0.46308043 -0.399983087 0.0033322377 0.4027386691 -0.11565299
27 0.83500398 -0.24538779 -0.210518419 -0.0619686979 -0.0910181655 0.14889131
28 0.72711865 -0.13433770 -0.017694026 0.0849729105 -0.1733300371 0.09419065
[,7] [,8] [,9] [,10] [,11]
1 0.040719765 0.0418820991 0.0427109931 -0.0815454233 -0.0550662711
2 -0.147394527 -0.1376637266 -0.1429142673 -0.0201856545 0.0066577220
3 0.092972219 -0.0826854950 0.0742980379 -0.0161042949 -0.0230964994
4 -0.123990787 0.0212052715 -0.0177151013 -0.0162499013 0.0212466556
5 0.154352370 0.0572614993 -0.0609766686 0.1260044554 -0.0252112089
6 -0.015327021 0.0534021536 0.0154136461 -0.0501163115 0.0586215416
7 0.004730482 0.0190029881 0.0023698190 0.1054092772 -0.0814119995
8 -0.088822714 -0.0653835186 -0.0666117245 -0.0226756098 0.0059008615
9 0.051957321 -0.0009526133 -0.0116185038 0.0476003654 -0.0117518675
10 -0.070308191 -0.0083554039 -0.0546286742 0.0495912472 -0.0108872124
11 -0.071584093 -0.0232249393 -0.0702877264 -0.0218908336 0.0143199812
12 0.113088703 0.0354288772 0.0330587496 -0.0582074608 0.0478392206
13 0.073097252 -0.0435360294 0.0472427965 -0.0472732542 0.0719724337
14 -0.156976404 0.0826957636 0.1100206974 0.0148671152 0.0168673292
15 -0.068985874 0.0446754621 -0.0226210361 0.0006590691 -0.0216613195
16 0.157905387 0.0193512735 0.0019489032 -0.0433718644 0.0366389725
17 -0.007599635 -0.0033366857 -0.0357983882 -0.0114844057 0.0007909651
18 0.142961962 -0.1262321618 -0.0190490754 0.0729509862 -0.0022536050
19 0.029430251 0.0625475986 -0.0063605996 0.0208175682 -0.0332852660
20 0.014449245 0.0280748808 0.0223481817 -0.0144468365 0.0540837044
21 -0.122769960 0.0677511042 0.1066139088 0.0278598817 -0.0170844291
22 -0.021286903 -0.1039880310 0.0875329060 0.0582961301 0.0419768516
23 -0.048490504 0.0263733360 0.0561456682 -0.0136701588 -0.0989504090
24 -0.049104996 -0.0219797368 0.0258166582 0.0746643485 0.0849958745
25 0.013539716 -0.0760000039 -0.0001490894 -0.0998739946 -0.0690796698
26 0.051815058 -0.0317203670 -0.0047842305 -0.1002761319 -0.0476513309
27 0.048185043 0.1914792249 -0.1419738528 -0.0214848983 0.0279789858
28 0.003436835 -0.0260728205 0.0299679725 0.0401365903 0.0074999888
[,12]
1 -0.0413872683
2 -0.0371791374
3 0.0491197192
4 -0.0159605272
5 -0.0504908011
6 0.0889373190
7 0.0691271296
8 0.0214365194
9 -0.0324372064
10 0.0090949676
11 0.0201008471
12 -0.0114326350
13 -0.0033268807
14 -0.0314291673
15 0.0026404530
16 -0.0273400353
17 0.0029197978
18 0.0217566029
19 -0.0174163225
20 0.0095473913
21 -0.0107855513
22 -0.0142425388
23 0.0110145492
24 -0.0151559204
25 -0.0212318408
26 0.0007829363
27 0.0195369355
28 0.0038006642
































