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

%% Load and prepare data
cd ../Data
load TIM2
cd ../Code

TIM2.gender=nominal(TIM2.Gender1F2M);

% Create cumulative times to finish
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;

%% Fit different models based on available splits 

TIM2_mdl_0_40 = LinearModel.fit(TIM2,...
    'K40Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530+K3035+K3540','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_40

TIM2_mdl_0_40_age = LinearModel.fit(TIM2,...
    'K40Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530+K3035+K3540+Age','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_40_age

TIM2_mdl_0_40_gender = LinearModel.fit(TIM2,...
    'K40Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530+K3035+K3540+gender','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_40_gender

TIM2_mdl_0_40_age_gender = LinearModel.fit(TIM2,...
    'K40Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530+K3035+K3540+Age+gender','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_40_age_gender

TIM2_mdl_0_35 = LinearModel.fit(TIM2,...
    'K35Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530+K3035','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_35

TIM2_mdl_0_35_age_gender = LinearModel.fit(TIM2,...
    'K35Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530+K3035+Age+gender','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_35_age_gender

TIM2_mdl_0_30 = LinearModel.fit(TIM2,...
    'K30Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_30_age_gender = LinearModel.fit(TIM2,...
    'K30Fin ~ K05 + K510 + K1015 + K1520 + K2025 + K2530+Age+gender','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_30_age_gender

TIM2_mdl_0_25 = LinearModel.fit(TIM2,...
    'K25Fin ~ K05 + K510 + K1015 + K1520 + K2025','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_25

TIM2_mdl_0_25_age_gender = LinearModel.fit(TIM2,...
    'K25Fin ~ K05 + K510 + K1015 + K1520 + K2025 +Age+gender','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_25_age_gender

TIM2_mdl_0_20 = LinearModel.fit(TIM2,...
    'K20Fin ~ K05 + K510 + K1015 + K1520 ','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_20

TIM2_mdl_0_20_age_gender = LinearModel.fit(TIM2,...
    'K20Fin ~ K05 + K510 + K1015 + K1520  +Age+gender','Exclude',isnan(TIM2.K40Fin));
TIM2_mdl_0_20_age_gender

%% Save results
cd ../Models
savefilename=['LM_TIM2'];
save(savefilename, 'TIM2_mdl*') %
cd ../Code

%% Notes

%%
% 
% * The folder structure used in this code is BostonAnalysis/Code,
% BostonAnalysis/Data, and BostonAnalysis/Models. 
% * Additional models were fit including year and stratification by time.
% Please contact Dorit Hammerling(hammerling@samsi.info) for details. 
% 

