. . 4.3 . , . ,
function Xs = scaling(X) % scaling: the output matrix is Xs % matrix X must be centered Xs = X * inv(diag(std(X))); %end of scaling |
5.2. SVD/PCA
(PCA). PCA X, .. T P
X = TP t + E
T (scores), P (loadings), E .
T P SVD MatLab, svd.
function [T, P] = pcasvd(X) % pcasvd: calculates PCA components. % The output matrices are T and P. % T contains scores % P contains loadings [U,D,V] = svd(X); T = U * D; P = V; %end of pcasvd |
5.3 PCA/NIPALS
PCA , NIPALS, . X ( ; . 4.3) E 0, a =0. .
1. t
2. p t = t t E a / t t t
3. p = p / (p t p)½
4. t = E a p / p t p
5. , , 2
(a -) , t a = t p a = p. E a +1 = E a t p t , a a +1.
NIPALS , . PCA, ( numberPC). , , [P,T] = pcanipals (X) X.
function [T, P] = pcanipals(X, numberPC) % pcanipals: calculates PCA components. % The output matrices are T and P. % T contains scores % P contains loadings % calculation of number of components [X_r, X_c] = size(X); P=[]; T=[]; if lenfth(numberPC) > 0 pc = numberPC{1}; elseif (length(numberPC) == 0) & X_r < X_c pc = X_r; else pc = X_c; end; % calculation of scores and loadings for each component for k = 1:pc P1 = rand(X_c, 1); T1 = X * P1; d0 = T1'*T1; P1 = (T1' * X/(T1' * T1))'; P1 = P1/norm(P1); T1 = X * P1; d = T1' * T1; while d - d0 > 0.0001; P1 = (T1' * X/(T1' * T1)); P1 = P1/norm(P1); T1 = X * P1; d0 = T1'*T1; P1 = (T1' * X/(T1' * T1)); P1 = P1/norm(P1); T1 = X * P1; d = T1'*T1; end X = X - T1 * P1; P = cat(1, P, P1'); T = [T,T1]; end |
PCA Chemometrics Excel.
|
|
PLS1
(PLS). X Y:
X = TP t+ E Y = UQ t+ F T = XW (P t W)1
, X - t a Y - u a. Y (.. K >1), PLS1 PLS2. y k . T (U) P (W, Q), , . PLS1. PLS2 , .
PLS PLS1 , . X Y
[E0, mX] = mc(X); [F0, mY] = mc(Y); |
E 0 f 0, a =0.
1. w t = f a t E a
2. w = w / (w t w)½
3. t = E a w
4. q = t t f a / t t t
5. u = q f a / q 2
6. p t = t t E a / t t t
(a -) , t a = t p a = p. E a +1 = E a t p t , a a +1.
,
function [w, t, u, q, p] = pls(x, y) %PLS: calculates a PLS component. %The output vectors are w, t, u, q and p. % % Choose a vector from y as starting vector u. u = y(:, 1); % The convergence criterion is set very high. kri = 100; % The commands from here to end are repeated until convergence. while (kri > 1e - 10) % Each starting vector u is saved as uold. uold = u; w = (u' * x)'; w = w/norm(w); t = x * w; q = (t' * y)'/(t' * t); u = y * q/(q' * q); % The convergence criterion is the norm of u-uold divided by the norm of u. kri = norm(uold - u)/norm(u); end; % After convergence, calculate p. p = (t' * x)'/(t' * t); % End of pls |
PLS1 Chemometrics Add In Excel.
PLS2
PLS2 . X Y ( ; . 4.3), E 0 F 0, a =0. .
1. u
2. w t = u t E a
3. w = w / (w t w)½
4. t = E a w
5. q t = t t F a / t t t
6. u = F a q / q t q
7. , , 2
8. p t = t t E a / t t t
(a -) PLS2 : t a = t, p a = p, w a = w, u a = u q a= q. E a +1 = E a t p t F a +1 = F a tq t , a a +1.
, .
|
|
function [W, T, U, Q, P, B, SS] = plsr(x, y, a) % PLS: calculates a PLS component. % The output matrices are W, T, U, Q and P. % B contains the regression coefficients and SS the sums of % squares for the residuals. % a is the numbers of components. % % For a components: use all commands to end. for i=1:a % Calculate the sum of squares. Use the function ss. sx = [sx; ss(x)]; sy = [sy; ss(y)]; % Use the function pls to calculate one component. [w, t, u, q, p] = pls(x, y); % Calculate the residuals. x = x - t * p'; y = y - t * q'; % Save the vectors in matrices. W = [W w]; T = [T t]; U = [U u]; Q = [Q q]; P = [P p]; end; % Calculate the regression coefficients after the loop. B=W*inv(P'*W)*Q'; % Add the final residual SS to the sum of squares vectors. sx=[sx; ss(x)]; sy=[sy; ss(y)]; % Make a matrix of the ss vectors for X and Y. SS = [sx sy]; %Calculate the fraction of SS used. [a, b] = size(SS); tt = (SS * diag(SS(1,:).^(-1)) - ones(a, b)) * (-1) %End of plsr function [ss] = ss(x) %SS: calculates the sum of squares of a matrix X. % ss=sum(sum(x. * x)); %End of ss |
PLS2 Chemometrics Add In Excel.
MatLab . , , the Unsrambler 16% . MatLab . , MatLab . , , .
Chemometrics - Microsoft Excel. Excel.