%Performs calibration on  xo2,yo2
%*****************************************************************************************************

%close all;
%clear;

%perform calibration on xcal,ycal (2)
tic
[trainedModel, validationRMSE] = trainRegressionModelE(xcal, ycal);
partitionedModel = crossval(trainedModel.RegressionGP, 'KFold', 5);
zcal = kfoldPredict(partitionedModel);
figure
mdlcal = fitlm(zcal,ycal); %returns a linear regression model of the responses y, fit to the data matrix X.
anova(mdlcal,'summary'); %test fit by anova
plot(mdlcal)
title('mdle2 cal')
TMcal = trainedModel;
MARDcal = 100*mean(abs(ycal-zcal)./zcal); 
SEPcal = validationRMSE;
toc

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

fncal = "cal_"+ idacq+idses;
save(fncal, 'TMcal','MARDcal','SEPcal','mdlcal');
