clear; tic; %%%%%%%%%%%%%%%%%%The actual data first%%%%%%%%%%%%%%%%%%%% %load the data load matlabres.out %make the dataset gmmatid=matlabres(:,1); op_gmmatid=matlabres(:,2); coachmatid=matlabres(:,3); op_coachmatid=matlabres(:,4); teammatid=matlabres(:,5); op_teammatid=matlabres(:,6); spellmatid=matlabres(:,7); op_spellmatid=matlabres(:,8); spellfe=matlabres(:,9); op_spellfe=matlabres(:,10); ivspellfe=matlabres(:,11); op_ivspellfe=matlabres(:,12); resid=matlabres(:,13); ivresid=matlabres(:,14); game_id=matlabres(:,15); home=matlabres(:,16); ten=matlabres(:,17); op_ten=matlabres(:,18); tensq=matlabres(:,19); op_tensq=matlabres(:,20); tentrip=matlabres(:,21); op_tentrip=matlabres(:,22); %generate the dependent variable y=spellfe+op_spellfe+resid; ivy=ivspellfe+op_ivspellfe+ivresid; %Put data in table Data=table(y,coachmatid,op_coachmatid,gmmatid,op_gmmatid,teammatid,... op_teammatid,spellmatid,op_spellmatid,ten,op_ten,tensq,op_tensq,tentrip,op_tentrip); IVdata=table(ivy,coachmatid,op_coachmatid,gmmatid,op_gmmatid,teammatid,... op_teammatid,spellmatid,op_spellmatid,ten,op_ten,tensq,op_tensq,tentrip,op_tentrip); % the model nospellmodel=fitlme(Data,'y ~ -1+(1|coachmatid)+(1|op_coachmatid)+(1|gmmatid)+(1|op_gmmatid)+(1|teammatid)+(1|op_teammatid)') [~,~,nospellrefull]=randomEffects(nospellmodel); residnospell=residuals(nospellmodel); covnospell=covarianceParameters(nospellmodel); spellmodel=fitlme(Data,'y ~ -1+(1|coachmatid)+(1|op_coachmatid)+(1|gmmatid)+(1|op_gmmatid)+(1|teammatid)+(1|op_teammatid)+(1|spellmatid)+(1|op_spellmatid)') [~,~,spellrefull]=randomEffects(spellmodel); residspell=residuals(spellmodel); covspell=covarianceParameters(spellmodel); ivspellmodel=fitlme(IVdata,'ivy ~ -1+(1|coachmatid)+(1|op_coachmatid)+(1|gmmatid)+(1|op_gmmatid)+(1|teammatid)+(1|op_teammatid)+(1|spellmatid)+(1|op_spellmatid)') [~,~,ivspellrefull]=randomEffects(ivspellmodel); residivspell=residuals(ivspellmodel); covivspell=covarianceParameters(ivspellmodel); tenmodel=fitlme(Data,'y ~ -1+ten+op_ten+tensq+op_tensq+tentrip+op_tentrip+(1|coachmatid)+(1|op_coachmatid)+(1|gmmatid)+(1|op_gmmatid)+(1|teammatid)+(1|op_teammatid)+(1|spellmatid)+(1|op_spellmatid)') [~,~,tenspellrefull]=randomEffects(tenmodel); %Read the resulting re coachrenospell=double(nospellrefull(1:max(coachmatid),4)); gmrenospell=double(nospellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),4)); teamrenospell=double(nospellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),4)); coachre=double(spellrefull(1:max(coachmatid),4)); gmre=double(spellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),4)); teamre=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),4)); spellre=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),4)); coachivre=double(ivspellrefull(1:max(coachmatid),4)); gmivre=double(ivspellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),4)); teamivre=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),4)); spellivre=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),4)); coachtenre=double(tenspellrefull(1:max(coachmatid),4)); gmtenre=double(tenspellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),4)); teamtenre=double(tenspellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),4)); spelltenre=double(tenspellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),4)); %Read the resulting s.e. coachrenospellse=double(nospellrefull(1:max(coachmatid),5)); gmrenospellse=double(nospellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),5)); teamrenospellse=double(nospellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),5)); coachrese=double(spellrefull(1:max(coachmatid),5)); gmrese=double(spellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),5)); teamrese=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),5)); spellrese=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),5)); coachivrese=double(ivspellrefull(1:max(coachmatid),5)); gmivrese=double(ivspellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),5)); teamivrese=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),5)); spellivrese=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),5)); %Read the resulting t-stat coachrenospelltscore=double(nospellrefull(1:max(coachmatid),6)); gmrenospelltscore=double(nospellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),6)); teamrenospelltscore=double(nospellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),6)); coachretscore=double(spellrefull(1:max(coachmatid),6)); gmretscore=double(spellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),6)); teamretscore=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),6)); spellretscore=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),6)); coachivretscore=double(ivspellrefull(1:max(coachmatid),6)); gmivretscore=double(ivspellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),6)); teamivretscore=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),6)); spellivretscore=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),6)); %Read the resulting prob. coachrenospellprob=double(nospellrefull(1:max(coachmatid),8)); gmrenospellprob=double(nospellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),8)); teamrenospellprob=double(nospellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),8)); coachreprob=double(spellrefull(1:max(coachmatid),8)); gmreprob=double(spellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),8)); teamreprob=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),8)); spellreprob=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),8)); coachivreprob=double(ivspellrefull(1:max(coachmatid),8)); gmivreprob=double(ivspellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),8)); teamivreprob=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),8)); spellivreprob=double(ivspellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),8)); %read the covariance coachnospellcovar=repmat(covnospell(1,1),max(coachmatid),1); coachcovar=repmat(covspell(1,1),max(coachmatid),1); coachivcovar=repmat(covivspell(1,1),max(coachmatid),1); gmnospellcovar=repmat(covnospell(3,1),max(gmmatid),1); gmcovar=repmat(covspell(3,1),max(gmmatid),1); gmivcovar=repmat(covivspell(3,1),max(gmmatid),1); teamnospellcovar=repmat(covnospell(5,1),max(teammatid),1); teamcovar=repmat(covspell(5,1),max(teammatid),1); teamivcovar=repmat(covivspell(5,1),max(teammatid),1); spellcovar=repmat(covspell(7,1),max(spellmatid),1); spellivcovar=repmat(covivspell(7,1),max(spellmatid),1); %Redo the id variables gmiddecomp=(1:max(gmmatid))'; coachiddecomp=(1:max(coachmatid))'; teamiddecomp=(1:max(teammatid))'; spelliddecomp=(1:max(spellmatid))'; %Export Outputfull=table(game_id,home,residspell,residivspell,residnospell); Outputgm=table(gmiddecomp,gmre,gmrese,gmretscore,gmreprob,gmcovar,gmtenre,... gmivre,gmivrese,gmivretscore,gmivreprob,gmivcovar,... gmrenospell,gmrenospellse,gmrenospelltscore,gmrenospellprob,gmnospellcovar); Outputcoach=table(coachiddecomp,coachre,coachrese,coachretscore,coachreprob,coachcovar,coachtenre,... coachivre,coachivrese,coachivretscore,coachivreprob,coachivcovar,... coachrenospell,coachrenospellse,coachrenospelltscore,coachrenospellprob,coachnospellcovar); Outputteam=table(teamiddecomp,teamre,teamrese,teamretscore,teamreprob,teamcovar,teamtenre,... teamivre,teamivrese,teamivretscore,teamivreprob,teamivcovar,... teamrenospell,teamrenospellse,teamrenospelltscore,teamrenospellprob,teamnospellcovar); Outputspell=table(spelliddecomp,spellre,spellrese,spellretscore,spellreprob,spellcovar,spelltenre,... spellivre,spellivrese,spellivretscore,spellivreprob,spellivcovar); writetable(Outputfull,'resfull.csv','WriteRowNames',true); writetable(Outputteam,'resteam.csv','WriteRowNames',true); writetable(Outputcoach,'rescoach.csv','WriteRowNames',true); writetable(Outputgm,'resgm.csv','WriteRowNames',true); writetable(Outputspell,'resspell.csv','WriteRowNames',true); toc %% %%%%%%%%%%%%%%%%%%%%%%%%%Repeat for the random samples%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tic randgm=std(gmre); randcoach=std(coachre); randteam=std(teamre); randtengm=std(gmtenre); randtencoach=std(coachtenre); randtenteam=std(teamtenre); randtenspell=std(spelltenre); randgmnospell=std(gmrenospell); randcoachnospell=std(coachrenospell); randteamnospell=std(teamrenospell); randspell=std(spellre); for q=1:500 %load the data matlabrand=load(['matlabrandres' num2str(q) '.out']); %make the dataset spellfe=matlabrand(:,9); op_spellfe=matlabrand(:,10); resid=matlabrand(:,11); %generate the dependent variable y=spellfe+op_spellfe+resid; %Put data in table Data=table(y,gmmatid,op_gmmatid,coachmatid,op_coachmatid,teammatid,op_teammatid,spellmatid,op_spellmatid,... spellfe,op_spellfe,ten, op_ten,tensq,op_tensq,tentrip,op_tentrip); % the model nospellmodel=fitlme(Data,'y ~-1+(1|coachmatid)+(1|op_coachmatid)+(1|gmmatid)+(1|op_gmmatid)+(1|teammatid)+(1|op_teammatid)'); [~,~,nospellrefull]=randomEffects(nospellmodel); spellmodel=fitlme(Data,'y ~-1+(1|coachmatid)+(1|op_coachmatid)+(1|gmmatid)+(1|op_gmmatid)+(1|teammatid)+(1|op_teammatid)+(1|spellmatid)+(1|op_spellmatid)'); [~,~,spellrefull]=randomEffects(spellmodel); tenmodel=fitlme(Data,'y ~-1+ten+op_ten+tensq+op_tensq+tentrip+op_tentrip+(1|coachmatid)+(1|op_coachmatid)+(1|gmmatid)+(1|op_gmmatid)+(1|teammatid)+(1|op_teammatid)+(1|spellmatid)+(1|op_spellmatid)'); [~,~,tenre]=randomEffects(tenmodel); %Read the resulting re coachrenospell=double(nospellrefull(1:max(coachmatid),4)); gmrenospell=double(nospellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),4)); teamrenospell=double(nospellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),4)); coachre=double(spellrefull(1:max(coachmatid),4)); gmre=double(spellrefull(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),4)); teamre=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),4)); spellre=double(spellrefull(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),4)); coachtenre=double(tenre(1:max(coachmatid),4)); gmtenre=double(tenre(2*max(coachmatid)+1:2*max(coachmatid)+max(gmmatid),4)); teamtenre=double(tenre(2*max(coachmatid)+2*max(gmmatid)+1:2*max(coachmatid)+2*max(gmmatid)+max(teammatid),4)); spelltenre=double(tenre(2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+1:2*max(coachmatid)+2*max(gmmatid)+2*max(teammatid)+max(spellmatid),4)); %Calculate the standard deviation coachrenospell=std(coachrenospell); gmrenospell=std(gmrenospell); teamrenospell=std(teamrenospell); coachre=std(coachre); gmre=std(gmre); teamre=std(teamre); spellre=std(spellre); coachtenre=std(coachtenre); gmtenre=std(gmtenre); teamtenre=std(teamtenre); spelltenre=std(spelltenre); %Export randgm=[randgm gmre]; randcoach=[randcoach coachre]; randteam=[randteam teamre]; randspell=[randspell spellre]; randtengm=[randtengm gmtenre]; randtencoach=[randtencoach coachtenre]; randtenteam=[randtenteam teamtenre]; randtenspell=[randtenspell spelltenre]; randgmnospell=[randgmnospell gmrenospell]; randcoachnospell=[randcoachnospell coachrenospell]; randteamnospell=[randteamnospell teamrenospell]; end Outputrandteam=table(randteam', randtenteam', randteamnospell'); Outputrandcoach=table(randcoach', randtencoach', randcoachnospell'); Outputrandgm=table(randgm', randtengm', randgmnospell'); Outputrandspell=table(randspell', randtenspell'); writetable(Outputrandteam,'resrandteam.csv','WriteRowNames',true); writetable(Outputrandcoach,'resrandcoach.csv','WriteRowNames',true); writetable(Outputrandgm,'resrandgm.csv','WriteRowNames',true); writetable(Outputrandspell,'resrandspell.csv','WriteRowNames',true); toc