function [MSE,yhat] = cverr_lwr1(data,h,order)

% CVERR_LWR1  --  Cross-validation error for locally weighted regression.
%
%  E = LWREG1(DATA,H,ORDER)
%  [E,YHAT] = LWREG1(DATA,H,ORDER)
%
%  DATA, H, and ORDER have the same interpretation as in LWREG1.
%  CVERR_LWR1 calculates the leave-one-out cross-validation error by
%  removing temporarily each point from the data set, predicting its
%  value YHAT based on the remaining N-1 data points, and calculating
%  the mean squared error MSE over all N points.
%  The predicted values YHAT may also be requested. They are useful for
%  outlier identification.
%
%  See also LWREG1.

% Original coding by Alex Petrov, Ohio State University
% $Revision: 1.0 $  $Date: 2000/11/29 15:15 $
%
% Part of the utils toolbox version 1.1 for MATLAB version 5 and up.
% http://alexpetrov.com/softw/utils/
% Copyright (c) Alexander Petrov 1999-2006, http://alexpetrov.com
% Please read the LICENSE and NO WARRANTY statement in ../utils_license.m

N = size(data,1) ;

yhat = zeros(N,1) ;
train = [2:N]' ;   % indices
for k = 1:N
  yhat(k) = lwreg1(data(train,:),data(k,1),h,order) ;
  train(k) = k ;   % will be harmlessly extended on the very last iteration
end

MSE = mean((data(:,2)-yhat).^2) ;

%-- Return MSE and possibly YHAT
%%%%% End of CVERR_LWR1.M
