function [press,cumpress,minlv,b] = plscvbkf(x,y,split,lv)
%PLSCVBKF Fast Cross validation for PLS using contiguous data blocks
% This function is primarily intended for use with GENALG
% Inputs are the matrix of predictor variables (x), vector
% of predicted variable (y), number of divisions of the data
% (split), and maximum number of latent variables to calculate (lv).
% Outputs are the prediction residual error sum of squares for each
% test set (press), cumulative PRESS (cumpress), number of latent
% variables at minimum PRESS (minlv), and the final regression vector
% (b) at minimum PRESS.
%
% This cross validation routine forms the test sets out of
% contiguous blocks of data. Note that this routine does not
% mean center each test set. The primary emphasis for this routine
% is speed.
%
% I/O format is:
% [press,cumpress,minlv,b] = plscvbkf(x,y,split,lv);
% Copyright
% Eigenvector Technologies
% 1995
[mx,nx] = size(x);
[my,ny] = size(y);
if mx ~= my
error('Number of samples must be the same in both blocks')
end
press = zeros(split,lv);
ind = ones(split,2);
for i = 1:split
ind(i,2) = round(i*mx/split);
end
for i = 1:split-1
ind(i+1,1) = ind(i,2) +1;
end
for i = 1:split
calx = [x(1:ind(i,1)-1,:); x(ind(i,2)+1:mx,:)];
testx = x(ind(i,1):ind(i,2),:);
caly = [y(1:ind(i,1)-1,:); y(ind(i,2)+1:mx,:)];
testy = y(ind(i,1):ind(i,2),:);
bbr = simpls1(calx,caly,lv);
for j = 1:lv
ypred = testx*bbr((j-1)*ny+1:j*ny,:)';
press(i,j) = sum(sum((ypred-testy).^2));
end
end
cumpress = sum(press);
[a,minlv] = min(cumpress);
if nargout > 3
b = simpls1(x,y,minlv);
b = b(minlv,:);
end