home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 6.ddi / CHEM.DI$ / PLS.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  1.9 KB  |  57 lines

  1. function [p, q, w, b, t, u, x, y, l] = pls(a, c, maxrank)
  2. % PLS   Calculate factors and coefficients for Partial Least Squares regression.
  3. %
  4. %         PLS calculates the factors and coefficents for the Partial
  5. %         Least Squares regression analysis in latent variables.
  6. %
  7. %  [p, q, w, b] = pls(a, c, maxrank)    or,
  8. %
  9. %  [p, q, w, b, t, u, x, y, l] = pls(a, c, maxrank)
  10. %
  11. % Where:
  12. %
  13. %  p        is the matrix of spectral factors
  14. %  q        is the matrix of concentration factors
  15. %  w        is the matrix of weights
  16. %  b        is the vector containing the inner relationships
  17. %  t        is the matrix of spectral loadings
  18. %  u        is the matrix of concentration loadings
  19. %  x        is the matrix of spectral residuals
  20. %  y        is the matrix of concentration residuals
  21. %  l        is the vector containing the numbers of iterations required
  22. %  a        is the training set absorbance matrix
  23. %  c        is the training set concentration matrix
  24. %  maxrank  is optional limit to the number of factors tested
  25. %
  26.  
  27. % Copyright (c) 1989-92 by The MathWorks, Inc.
  28.  
  29. [i,j] = size(a);
  30. k = 0;
  31. x = a';
  32. y = c';
  33. if nargin == 3, i = maxrank; end
  34. for h = 1:i,
  35.     u(:,h) = y(:,1);
  36.     told(:,h) = zeros(j,1);
  37.     t(:,h) = ones(j,1);
  38.     while (sum(abs(told(:,h)-t(:,h)))) > (1e-15) & k < 100
  39.         told(:,h) = t(:,h);
  40.         k = k + 1;
  41.         w(h,:)  = u(:,h)' * x / (u(:,h)' * u(:,h));
  42.         w(h,:)  =  w(h,:) / sqrt(sum(w(h,:) .* w(h,:)));
  43.         t(:,h)  = x * w(h,:)' / (w(h,:) * w(h,:)');
  44.         q(h,:) = t(:,h)' * y / (t(:,h)' * t(:,h));
  45.         u(:,h)  = y * q(h,:)'/ (q(h,:) * q(h,:)');
  46.     end
  47.         l(h) = k;
  48.     k = 0;
  49. p(h,:) = t(:,h)' * x / (t(:,h)' * t(:,h));
  50. p(h,:) = p(h,:) / sqrt(sum(p(h,:) .* p(h,:)));
  51. t(:,h)  = t(:,h) /  sqrt(sum(p(h,:) .* p(h,:)));
  52. w(h,:)  = w(h,:) /  sqrt(sum(p(h,:) .* p(h,:)));
  53. b(h) = (u(:,h)' * t(:,h)) / (t(:,h)' * t(:,h));
  54. x = x - (t(:,h) * p(h,:));
  55. y = y - (b(h) * t(:,h) * q(h,:));
  56. end
  57.