%% Linear regression prediction code for Boston marathon analysis
%% Clear workspace
clear all 

%% Load and prepare data and fitted models
cd ../Data
load TIM2
cd ../Code
TIM2.gender=nominal(TIM2.Gender1F2M);
TIM2.K35Fin=TIM2.K3540+TIM2.K40Fin;
TIM2.K30Fin=TIM2.K3035+TIM2.K3540+TIM2.K40Fin;
TIM2.K25Fin=TIM2.K2530+TIM2.K3035+TIM2.K3540+TIM2.K40Fin;
TIM2.K20Fin=TIM2.K2025+TIM2.K2530+TIM2.K3035+TIM2.K3540+TIM2.K40Fin;
TIM2.FTsplits=zeros(length(TIM2),1);TIM2.FTsplitsagegender=zeros(length(TIM2),1);
cd ../Models
loadfilename=['LM_TIM2'];
load(loadfilename)
cd ../Code

%% Sort data by available splits and use respective model

% Starting at 20K
ind_new=isnan(TIM2.K2025);
Xnew=dataset();
Xnew.K05=TIM2.K05(ind_new);
Xnew.K510=TIM2.K510(ind_new);
Xnew.K1015=TIM2.K1015(ind_new);
Xnew.K1520=TIM2.K1520(ind_new);
Xnew.Age=TIM2.Age(ind_new);
Xnew.gender=nominal(TIM2.Gender1F2M(ind_new));
% Predict
mdl=TIM2_mdl_0_20;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplits(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new);
mdl=TIM2_mdl_0_20_age_gender;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplitsagegender(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new);

% Starting at 25K
ind_new=~isnan(TIM2.K2025)&isnan(TIM2.K2530);
Xnew=dataset();
Xnew.K05=TIM2.K05(ind_new);
Xnew.K510=TIM2.K510(ind_new);
Xnew.K1015=TIM2.K1015(ind_new);
Xnew.K1520=TIM2.K1520(ind_new);
Xnew.K2025=TIM2.K2025(ind_new);
Xnew.Age=TIM2.Age(ind_new);
Xnew.gender=nominal(TIM2.Gender1F2M(ind_new));
% Predict
mdl=TIM2_mdl_0_25;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplits(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new);
mdl=TIM2_mdl_0_25_age_gender;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplitsagegender(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new);

% Starting at 30K
ind_new=~isnan(TIM2.K2025)&~isnan(TIM2.K2530)&isnan(TIM2.K3035);
Xnew=dataset();
Xnew.K05=TIM2.K05(ind_new);
Xnew.K510=TIM2.K510(ind_new);
Xnew.K1015=TIM2.K1015(ind_new);
Xnew.K1520=TIM2.K1520(ind_new);
Xnew.K2025=TIM2.K2025(ind_new);
Xnew.K2530=TIM2.K2530(ind_new);
Xnew.Age=TIM2.Age(ind_new);
Xnew.gender=nominal(TIM2.Gender1F2M(ind_new));
% Predict
mdl=TIM2_mdl_0_30;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplits(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new)+TIM2.K2530(ind_new);
mdl=TIM2_mdl_0_30_age_gender;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplitsagegender(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new)+TIM2.K2530(ind_new);

% Starting at 35K
ind_new=~isnan(TIM2.K2025)&~isnan(TIM2.K2530)&~isnan(TIM2.K3035)&isnan(TIM2.K3540);
Xnew=dataset();
Xnew.K05=TIM2.K05(ind_new);
Xnew.K510=TIM2.K510(ind_new);
Xnew.K1015=TIM2.K1015(ind_new);
Xnew.K1520=TIM2.K1520(ind_new);
Xnew.K2025=TIM2.K2025(ind_new);
Xnew.K2530=TIM2.K2530(ind_new);
Xnew.K3035=TIM2.K3035(ind_new);
Xnew.Age=TIM2.Age(ind_new);
Xnew.gender=nominal(TIM2.Gender1F2M(ind_new));
% Predict
mdl=TIM2_mdl_0_35;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplits(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new)+TIM2.K2530(ind_new)+...
     TIM2.K3035(ind_new);
mdl=TIM2_mdl_0_35_age_gender;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplitsagegender(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new)+TIM2.K2530(ind_new)+...
    TIM2.K3035(ind_new);

% Starting at 40K
ind_new=~isnan(TIM2.K2025)&~isnan(TIM2.K2530)&~isnan(TIM2.K3035)&...
    ~isnan(TIM2.K3540)&isnan(TIM2.K40Fin);
Xnew=dataset();
Xnew.K05=TIM2.K05(ind_new);
Xnew.K510=TIM2.K510(ind_new);
Xnew.K1015=TIM2.K1015(ind_new);
Xnew.K1520=TIM2.K1520(ind_new);
Xnew.K2025=TIM2.K2025(ind_new);
Xnew.K2530=TIM2.K2530(ind_new);
Xnew.K3035=TIM2.K3035(ind_new);
Xnew.K3540=TIM2.K3540(ind_new);
Xnew.Age=TIM2.Age(ind_new);
Xnew.gender=nominal(TIM2.Gender1F2M(ind_new));
% Predict
mdl=TIM2_mdl_0_40;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplits(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new)+TIM2.K2530(ind_new)+...
     TIM2.K3035(ind_new)+TIM2.K3540(ind_new);
mdl=TIM2_mdl_0_40_age_gender;
[ypred,yci]= predict(mdl,Xnew,'Prediction','Observation');
TIM2.FTsplitsagegender(ind_new)=ypred+TIM2.K05(ind_new)+TIM2.K510(ind_new)...
    +TIM2.K1015(ind_new)+TIM2.K1520(ind_new)+TIM2.K2025(ind_new)+TIM2.K2530(ind_new)+...
    TIM2.K3035(ind_new)+TIM2.K3540(ind_new);
TIM2LM=TIM2;


%% Save results
cd ../Results
savefilename=['TIM2LM'];
save(savefilename, 'TIM2LM')
cd ../Code

%% Notes

%%
% 
% * The folder structure used in this code is BostonAnalysis/Code,
% BostonAnalysis/Data, BostonAnalysis/Models, and BostonAnalysis/Results. 
% 
