Herleitung der Hauptkomponenten: Y t = (Y 1,..., Y m ) Erwartung:µ Kovarianz:Σ Z j = a 1j Y 1 + a 2j Y 2 +... + a mj Y m = a t j Y a t j = (a 1j, a 2j,..., a mj ) Z 1, Z 2,...,Z m unkorreliert Varianzen fallen mit wachsendem Index j = 1,..., m a t j a j = m k=1 a 2 kj = 1. MV04f01
1. Hauptkomponente: Z 1 : Finde a 1 so, dass Nebenbedingung: Zielfunktion: Var(Z 1 ) = Var(a t 1Y ) a t 1 a 1 = 1 Maximum!! Var(a t 1 Y ) = at 1 Σa 1 Rückblick, Mathe: f(y 1,...,y m ) zu maximieren! λ, Lagrange Multiplikator, so dass in stationären Punkten y 0 gilt: Nebenbedingung g(y 1,...,y m ) = c f y i (y 0 ) λ g y i (y 0 ) = 0 i = 1,..., m L(y, λ) = f(y) λ[g(y) c] L y (y 0) = 0 MV04f02
L(a 1 ) = a t 1Σa 1 λ(a t 1a 1 1) (a t 1Σa 1 ) a 1 = 2Σa 1 L a 1 = 2Σa 1 2λa 1 = 0 (Σ λi)a 1 = 0 Dabei ist I = I m m die m-dimensionale Einheitsmatrix. Nach Theorem 16.8.2 (Mathe) hat das homogene Gleichungssystem genau dann nichttriviale Lösungen a 1 0, wenn (Σ λi)a 1 = 0 det(σ λi) = 0 λ Eigenwert von Σ MV04f03
Eigenwerte Lösungen der Gleichung: det(σ λi) = 0 Gleichung ist Polynom n-ter Ordnung in λ, das m Nullstellen hat, die Eigenwerte λ 1, λ 2,..., λ m genannt werden. λ i c i Eigenvektor: Σc i = λ i c i Eigenvektoren gewöhnlich normiert, damit sie eindeutig sind: c t ic i = 1 MV04f04
Eigenschaften von Eigenwerten und Eigenvektoren a) m λ i = Spur(Σ) i=1 b) m λ i = det(σ) i=1 c) Σ reelle symmetrische Matrix, Eigenwerte und Eigenvektoren reell d) Σ positiv definit, alle Eigenwerte strikt positiv e) Σ positiv semidefinit, Rang(Σ) = p < m, p positive und m-p Eigenwerte =0 f) λ i λ j c i und c j orthonormiert MV04f04
g) m m-matrix C = (c 1,..., c m ) Eigenvektoren in den Spalten x = Cy C t C = I C t ΣC = Λ = λ 1 0... 0 0 λ 2... 0. 0 0... λ m Dabei ist p = Rang(Σ) Aus C t ΣC = Λ folgt: x t Σx = y t C t ΣCy = y t Λy = λ 1 y 2 1 +...λ p y 2 p Spektralzerlegung von Σ Σ = CΛC t = λ 1 c 1 c t 1 +... + λ pc p c t p MV04f04
Beispiel: Bestimmung der Eigenwerte und Eigenvektoren Σ = ( 1 1/2 1/2 1 ) det(σ λi) = det ( 1 λ 1/2 1/2 1 λ ) = (1 λ) 2 1/4 = λ 2 2λ + 3/4 Nullstellen: λ 1,2 = 1 ± 1 3/4, d.h. λ 1 = 3/2 und λ 2 = 1/2 Zu jedem Eigenwert λ i gehört ein Vektor c i, der Eigenvektor genannt wird, für den gilt: Σc i = λ i c i (Σ λ i I) c i = 0 Für λ 1 = 3/2 das Gleichungssystem (Σ 3/2I)c 1 = 0 zu lösen. MV04f04a
(Σ 3/2I)c 1 = 0 0.5c 11 + 0.5c 12 = 0 0.5c 11 0.5c 12 = 0 Das bedeutet c 11 = c 12, d.h jeder Vektor c t 1 = (c 11, c 11 ) ist eine Lösung. Für λ 2 = 1/2 ist das Gleichungssystem (Σ 1/2I)c 2 = 0 0.5c 21 + 0.5c 22 = 0 0.5c 21 + 0.5c 22 = 0 c 21 = c 22 Jeder Vektor c t 2 = (c 21, c 21 ) ist eine Lösung. Normierung: c t i c i = 1 c t 1 = (1/ 2, 1/ 2) c t 2 = (1/ 2, 1/ 2) MV04f04a
Bestimmung der ersten Hauptkomponente: Var(Z 1 ) = Var(a t 1Y ) = a t 1Σa 1 Max!! L(a 1 ) = a t 1Σa 1 λ(a t 1a 1 1) L a 1 = 2Σa 1 2λa 1 = 0 (Σ λi)a 1 = 0 λ muss ein Eigenwert sein und a 1 ein Eigenvektor Var(a t 1Y ) = a t 1Σa 1 = a t 1λIa 1 = λa t 1Ia 1 = λa t 1a 1 = λ Maximum!! λ = λ 1 (1. Eigenwert) a 1 (1. Eigenvektor) MV04f05
2. Hauptkomponente: Z 2 = a t 2Y a t 2a 2 = 1 Cov(Z 2, Z 1 ) = Cov(a t 2 Y, at 1 Y ) = E[at 2 (Y µ)(y µ)t a 1 ] = a t 2 Σa 1 = 0 Σa 1 = λ 1 a 1 a t 2a 1 = 0 Var(Z 2 ) = a t 2Σa 2 Maximum unter Nebenbedingungen: a t 2a 2 = 1 a t 2a 1 = 0 L(a 2 ) = a t 2 Σa 2 λ(a t 2 a 2 1) δa t 2 a 1 L a 2 = 2(Σ λi)a 2 δa 1 = 0 von links mal a t 1 2a t 1Σa 2 2λ a t 1a 2 }{{} =0 δ a t 1a 1 }{{} =1 = 2a t 1Σa 2 δ = 0 MV04f06
2a t 1Σa 2 δ = 0 0 = Cov(Z 2, Z 1 ) = a t 2Σa 1 = a t 1Σa 2 a t 1Σa 2 = 0 δ = 0 L a 2 = 2(Σ λi)a 2 δa 1 = 0 (Σ λi)a 2 = 0 Damit eine nichttriviale Lösung existiert, muss λ ein Eigenwert sein. Var(Z 2 ) = a t 2Σa 2 = a t 2λIa 2 = λa t 2a 2 = λ soll maximiert werden. λ = λ 2 a 2 2. Eigenvektor 2. Hauptkomponente: Z 2 = a t 2 Y MV04f07
1. Hauptkomponente: Z 1 = a t 1Y, wobei a 1 der zu λ 1 gehörige Eigenvektor 2. Hauptkomponente: Z 2 = a t 2Y, wobei a 2 der zu λ 2 gehörige Eigenvektor 3. Hauptkomponente: Z 3 = a t 3 Y, wobei a 3 der zu λ 3 gehörige Eigenvektor usw. Die Eigenwerte sind der Größe nach zu ordnen. Bei identischen Eigenwerten ist darauf zu achten, dass die zugehörigen Eigenvektoren orthogonal sind. MV04f07a
A m m = [a 1,...,a m ] Spalten sind die Eigenvektoren Z = (Z 1, Z 2,..., Z m ) t = A t Y Var(Z) = Λ = Vektor der Hauptkomponenten λ 1 0... 0 0 λ 2... 0. 0 0... λ m Var(Z) = Var(A t Y ) = A t ΣA Λ = A t ΣA Σ = AΛA t MV04f08 m Var(Z i ) = i=1 m λ i = Spur(Λ) i=1
Var(Z) = A t ΣA = Λ = λ 1 0... 0 0 λ 2... 0. 0 0... λ m m Var(Z i ) = i=1 m λ i = Spur(Λ) i=1 Spur(Λ) = Spur(A t (ΣA)) = Spur((ΣA)A t ) m = Spur(Σ) = Var(Y i ) i=1 MV04f08
m m Var(Z i ) = Var(Y i ) i=1 i=1 m m Var(Z i ) = i=1 i=1 Anteil der i-ten Hauptkomponente an der Totalvariation: λ i λ i / m j=1 λ j Anteil der ersten p Hauptkomponenten an der Totalvariation: p / m λ j j=1 j=1 λ j MV04f09
cov(teil01.frame) Groesse Schuh Gewicht Groesse 92.34441 24.387788 103.94037 Schuh 24.38779 8.922498 32.98380 Gewicht 103.94037 32.983795 195.32201 eigen(cov(teil01.frame)) $values [1] 266.322975 28.256195 2.009739 $vectors Gewicht Schuh Groesse Groesse 0.5226376-0.8312910 0.1892224 Schuh 0.1569001-0.1243717-0.9797520 Gewicht 0.8379930 0.5417443 0.0654283 MV04f10
eigen(cov(teil01.frame)) $values [1] 266.322975 28.256195 2.009739 Eigenwerte: λ 1 = 266.32 λ 2 = 28.26 λ 3 = 2.01 $vectors Gewicht Schuh Groesse Groesse 0.5226376-0.8312910 0.1892224 Schuh 0.1569001-0.1243717-0.9797520 Gewicht 0.8379930 0.5417443 0.0654283 Eigenvektoren: a t 1 = ( 0.5226, 0.1569, 0.8380) a t 2 = ( 0.8313, 0.1244, 0.5417) a t 3 = ( 0.1892, 0.9798, 0.0654) MV04f10a
Hauptkomponenten: HK1 = +0.5226 Groesse + 0.1569 Schuh + 0.8380 Gewicht HK2 = 0.8313 Groesse 0.1244 Schuh + 0.5417 Gewicht HK3 = +0.1892 Groesse 0.9798 Schuh + 0.0654 Gewicht In R: Eigenvektoren<-eigen(cov(teil01.frame))$vectors HK1<-as.matrix(teil01.frame)%*%Eigenvektoren[,1] HK2<-as.matrix(teil01.frame)%*%Eigenvektoren[,2] HK3<-as.matrix(teil01.frame)%*%Eigenvektoren[,3] Einfacher: as.matrix(teil01.frame)%*%eigenvektoren MV04f11
Die Kovarianzmatrix der Hauptkomponenten: cov(cbind(hk1,hk2,hk3)) [,1] [,2] [,3] [1,] 2.663230e+02 3.356920e-14-1.334241e-15 [2,] 3.356920e-14 2.825619e+01 1.217791e-14 [3,] -1.334241e-15 1.217791e-14 2.009739e+00 In der Diagonalen stehen die Eigenwerte. Die Kovarianzen, Werte außerhalb der Diagonalen, sind nahezu Null. MV04f11a
Anteile an der Totalvariation: Eigenwerte<-eigen(cov(teil01.frame))$values Eigenwerte [1] 266.322975 28.256195 2.009739 print(round(eigenwerte/sum(eigenwerte)*100,digits=2)) [1] 89.80 9.53 0.68 Die 1. Hauptkomponente Z 1 erklärt 89.8% der Variation, die 2. erklärt 9.53% der Variation. print(round(cumsum(eigenwerte)/sum(eigenwerte)*100,digits=2)) [1] 89.80 99.32 100.00 Die beiden ersten Hauptkomponenten Z 1 und Z 2 erklären zusammen 99.32% der Variation. MV04f12
Verwendung der Korrelationsmatrix cor(teil01.frame) Groesse Schuh Gewicht Groesse 1.0000000 0.8496184 0.7739330 Schuh 0.8496184 1.0000000 0.7900996 Gewicht 0.7739330 0.7900996 1.0000000 eigen(cor(teil01.frame)) $values [1] 2.6095464 0.2410677 0.1493859 $vectors Gewicht Schuh Groesse Groesse 0.5809216-0.4715234 0.66347255 Schuh 0.5846446-0.3254109-0.74316787 Gewicht 0.5663222 0.8196179 0.08663539 MV04f13
eigen(cor(teil01.frame)) $values [1] 2.6095464 0.2410677 0.1493859 Eigenwerte: λ 1 = 2.61 λ 2 = 0.24 λ 3 = 0.15 $vectors Gewicht Schuh Groesse Groesse 0.5809216-0.4715234 0.66347255 Schuh 0.5846446-0.3254109-0.74316787 Gewicht 0.5663222 0.8196179 0.08663539 Eigenvektoren: a t 1 = ( 0.5809, 0.5846, 0.5663) a t 2 = ( 0.4715, 0.3254, 0.8196) a t 3 = ( 0.6635, 0.7432, 0.0866) MV04f13a
Summe der Eigenwerte bei Verwendung der Korrelationsmatrix cor(teil01.frame) Groesse Schuh Gewicht Groesse 1.0000000 0.8496184 0.7739330 Schuh 0.8496184 1.0000000 0.7900996 Gewicht 0.7739330 0.7900996 1.0000000 eigen(cor(teil01.frame)) $values [1] 2.6095464 0.2410677 0.1493859 Summe der Diagonalelemente in der Korrelationsmatrix und damit auch die Summe der Eigenwerte ist 3! Der Beitrag der j-ten Hauptkomponente an der Totalvariation ist λ j /3. MV04f13b
Anteile an der Totalvariation eigen(cor(teil01.frame)) $values [1] 2.6095464 0.2410677 0.1493859 Der Beitrag der j-ten Hauptkomponente an der Totalvariation ist λ j /3. Anteile in Prozent: eigen(cor(teil01.frame))$values*100/3 86.984879 8.035589 4.979531 Kumierte Anteile: cumsum(eigen(cor(teil01.frame))$values*100)/3 86.98488 95.02047 100.00000 Die 1. Hauptkomponente erklärt 87%, die zweite 8%, zusammen erklären sie 95% der Variation. MV04f13c
Berechnung der Hauptkomponenten bei Verwendung der Korrelationsmatrix Voraussetzung: Daten sind standardisiert, d.h. wir müssen durch die Standardabweichnungen dividieren! Standardabweichungen: sqrt(diag(cov(teil01.frame))) Groesse Schuh Gewicht 9.609600 2.987055 13.975765 stand<-sqrt(diag(cov(teil01.frame))) > diag(1/stand) [,1] [,2] [,3] [1,] 0.1040626 0.0000000 0.00000000 [2,] 0.0000000 0.3347779 0.00000000 [3,] 0.0000000 0.0000000 0.07155243 MV04f13d
Standardisierung der Datenmatrix: K = 1/s 1 0... 0 0 1/s 2... 0. 0 0... 1/s m X = XK ist die standardisierte Datenmatrix. In R: teil01stand.frame<-as.matrix(teil01.frame)%*%diag(1/stand) > cov(teil01stand.frame) [,1] [,2] [,3] [1,] 1.0000000 0.8496184 0.7739330 [2,] 0.8496184 1.0000000 0.7900996 [3,] 0.7739330 0.7900996 1.0000000 MV04f14
Berechnung der Hauptkomponenten in R: teil01stand.frame%*%eigen(cor(teil01.frame))$vectors HK1 HK2 HK3 21.64-8.51 1.83 22.32-9.37 1.40 19.12-9.22 1.96 18.82-8.85 2.01 20.42-8.68 2.30 23.97-9.29 2.21 22.12-9.18 2.50 22.28-9.16 1.27 22.63-9.89 1.86 19.91-9.19 3.02 22.11-9.85 2.34 19.22-8.54 2.19 24.42-9.07 2.91 MV04f15
Mittelwertkorrektur: Z = A t Y Z = A t (Y µ) Datenmatrix so verändern: (X 1µ t ) > mitte<-mean(teil01.frame) > mitte Groesse Schuh Gewicht 177.22566 41.81858 69.10619 > eins<-rep(1,nrow(teil01.frame)) > eins [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [112] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [149] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [186] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [223] 1 1 1 1
Mittelwertkorrektur in R: teil01.frame-eins%*%t(mitte) Groesse Schuh Gewicht 1-6.23 0.18 6.89 2-1.23 3.18 1.89 3-16.23-3.82-21.11 4-19.23-4.82-19.11 5-9.23-2.82-4.11 6 12.77 4.18 16.89 7 3.77 0.18 3.89 8-3.23 3.18 3.89 9 5.77 3.18-1.11 10-5.23-4.82-13.11 11 5.77 1.18-4.11 12-17.23-4.82-12.11 13 18.77 3.18 23.89
Berechnung der Hauptkomponenten in R: (as.matrix(teil01.frame) - eins%*%t(mitte))%*%eigenvektoren Gewicht Schuh Groesse 1 2.55 8.89-0.90 2 1.45 1.65-3.23 3-26.77 2.53-0.71 4-26.81 6.23-0.17 5-8.70 5.80 0.75 6 21.49-1.99-0.57 7 5.26-1.05 0.79 8 2.08 4.40-3.47 9 2.59-5.80-2.10 10-14.47-2.16 2.87 11-0.24-7.17-0.33 12-19.90 8.36 0.67 13 30.33-3.06 2.00 MV04f16
R-Funktion prcomp prcomp(teil01.frame) Standard deviations: 16.319405 5.315656 1.417653 Rotation: PC1 PC2 PC3 Groesse 0.5226376 0.8312910 0.1892224 Schuh 0.1569001 0.1243717-0.9797520 Gewicht 0.8379930-0.5417443 0.0654283
prcomp(x, retx = TRUE, center = TRUE, scale. = FALSE, tol = NULL) aus<-prcomp(teil01.frame) # Ausgabe in aus gespeichert > aus$sdev # Standardabweichungen = Wurzel(Eigenwerte) [1] 16.319405 5.315656 1.417653 aus$rotation # Eigenvektoren PC1 PC2 PC3 Groesse 0.5226376-0.8312910 0.1892224 Schuh 0.1569001-0.1243717-0.9797520 Gewicht 0.8379930 0.5417443 0.0654283
aus$x # Hauptkomponenten PC1 PC2 PC3 1 2.5516584 8.887455-0.9047276 2 1.4455821 1.649163-3.2250132 3-26.7661221 2.529012-0.7099360 4-26.8149492 6.230745-0.1669945 5-8.7048777 5.795256 0.7471499 6 21.4893041-1.987118-0.5742272 7 5.2640560-1.050688 0.7912114 8 2.0762928 4.395234-3.4726014 9 2.5900667-5.795107-2.0967414 10-14.4700642-2.156863 2.8746888 11-0.2377125-7.171596-0.3335223 12-19.9037230 8.360373 0.6694484 MV04f17
R-Funktion princomp Usage: princomp(x, cor = FALSE, scores = TRUE, covmat = NULL, subset = rep(true, nrow(as.matrix(x)))) princomp(teil01.frame) Call: princomp(x = teil01.frame) Standard deviations: Comp.1 Comp.2 Comp.3 16.283260 5.303882 1.414513 3 variables and 226 observations. MV04f18
Elemente der Ausgabeliste von princomp: princomp returns a list with class "princomp"containing the following components: sdev: the standard deviations of the principal components. loadings: the matrix of variable loadings (i.e., a matrix whose columns contain the eigenvectors). center: the means that were subtracted. scale: the scalings applied to each variable. n.obs: the number of observations. scores: if scores = TRUE, the scores of the supplied data on the principal components. call: the matched call. MV04f19