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

close all;
%%clear;
%load('cal_d1r0c0s0119cad','TMcal'); %load model
%load('dat_d1r0c0s2022cad','xprd','xprdn','yprd'); %load test spectra for testing
%xtest=xprd;
%ytest=yprd;
%load('spe_d1r0c0s0122baln');
%xtest=xo2b;%x1ad2022;
%xtest=xot;
%load('sgl_d1r0c0s0122b');
%ytest=yo2b;%y2022;
%ytest=yot;
xtest=x1dc030610141720n;
ytest=y1030610141720;

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 = TMcal %select calibration model
w(ip) = trainedModel.predictFcn(v(ip,:));
%RDp(ip) = 100*(avp(w(ip),6))/avp(u(ip),6)-1);
end

MARDp = 100*mean(abs(avp(w,6)./avp(u,6)-1));
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')

save('prd_d1r0c0s2022cdc2');
