 %Performs prediction using calibration data saved in trainedModel 
%********************************************************************************

%close all;
%clear;
%idtst = "s2022c";
%fntest="spect_"+idacq+idses;
%load(fntest); %load test spectra for testing
%need to lad dat from PRD and cal from CAL
%xtest=xprd;
%ytest=yprd;
xtest=x2022;
ytest=y2022;
%xtest=xtest2;
%ytest=ytest2;
%xtest=xo2b;
%ytest=yo2b;
%xtest=xo4;
%ytest=yo4;
%xtest=x2022n;
%ytest=y2022n;

v=[];
u=[];
w=[];
for ip=1:length(ytest)
v(ip,:) = xtest(ip,:); %select x spectra data to use for prediction
u(ip) = ytest(ip);; %select y glucose data to use for error calculation if available
%u(ip)=u(ip)';
trainedModel = TMprd %select calibration model
w(ip) = trainedModel.predictFcn(v(ip,:));
RDp(ip) = 100*(w(ip)-u(ip))/u(ip);
end
%u=u';
MARDp = 100*mean(abs(w-u)./u);
w=w';
[totalClark, percentClark] = clarke(u,w);

mdlpred = fitlm(w,u); %returns a linear regression model of the responses y, fit to the data matrix X.
anova(mdlpred,'summary'); %test fit by anova
figure;plot(mdlpred)
title('mdle2 pred')

[N M] = size(w);
rn=[1:N];
rn=rn';
figure; plot(rn,u,'-ro',rn,w,'-bo')

fnpred = "prd_" +idacq+idses%+idtst; 
%save(fnpred);
