И другие программы этой серии
p = (t\' * x)\'/(t\' * t);
% End of pls
О вычислении PLS1 с помощью надстройки Chemometrics рассказано в пособии Проекционные методы в системе Excel.
Содержание
5.5 PLS2
Для PLS2 алгоритм выглядит следующим образом. Сначала исходные матрицы X и Y преобразуют (как минимум – центрируют; см. разделе 4.3), и они превращаются в матрицы E0 и F0, a=0. Далее к ним применяет следующий алгоритм.
1. Выбрать начальный вектор u
2. wt = ut Ea
3. w = w / (wtw)½
4. t = Ea w
5. qt = tt Fa / ttt
6. u = Fa q/ qtq
7. Проверить сходимость, если нет, то идти на 2
8. pt = tt Ea / ttt
После вычисления очередной (a-ой) PLS2 компоненты надо положить: ta=t, pa=p, wa=w, ua=u и qa=q. Для получения следующей компоненты надо вычислить остатки Ea+1 = Ea – t pt и Fa+1 = Fa – tqt и применить к ним тот же алгоритм, заменив индекс 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.