function macra(X); %This is a GUI shell for running a bidirectional multivariate auto- and % cross-regressive analysis on two simultaneous, continuous time series. % X is a rectangular matrix with two columns. value=[]; inputPrompt=[]; colname=[]; R=[]; b=[]; TM=[]; G=[]; H=[]; C=[]; P=[]; len=length(X); userInput=[]; inputPrompt={'Please assign a short name to the first data column: '}; value=inputdlg(inputPrompt); c1name=value; inputPrompt={'Please assign a short name to the second data column: '}; value=inputdlg(inputPrompt); c2name=value; mm=mean(X); sd=std(X); C=ones(1,len); C=C'; D=C*mm; S=C*sd; P=X-D; R=P./S; plot(R(:,1),'b-'); hold on plot(R(:,2),'r-'); xlabel('Time'); ylabel('Standard scores'); title(['Plot of ' char(c1name) ' (blue) and ' char(c2name) ' (red) versus time']); hold off fprintf(' \n'); userInput=input('Hit when ready to view serial correlation plots...'); ok=userInput; fprintf(' \n'); userInput=input('Enter number of lags to compute for correlation plots (Å N/4): '); la=userInput; pacorr(X(:,1),la,c1name); pacorr(X(:,2),la,c2name); fprintf(' \n'); userInput=input('Move the 4 stacked plots around to view, hit when finished...'); ok=userInput; fprintf(' \n'); userInput=input('Select the number of lags to include in regression model (Å 5 recommended): '); lr=userInput; %set up lags for j=1:lr for i=1:len if j>=i G(i,j)=0; else G(i,j)=X((i-j),1); end end end for j=1:lr for i=1:len if j>=i H(i,j)=0; else H(i,j)=X((i-j),2); end end end G=G((lr+1):len,:); H=H((lr+1):len,:); X=X((lr+1):len,:); newl=length(X); TM=[G,X(:,2),H];%matrix of independent variables for i=1:lr %set up array of column names j=num2str(i); tx1=strcat(c1name,'-L',j); colname{i}=(tx1); end for i=(lr+1):(2*lr+1) j=num2str(i-lr-1); tx2=strcat(c2name,'-L',j); colname{i}=(tx2); end colname=colname'; fprintf(' \n'); userInput=input(['Analysis with ' char(c1name) ' as dependent variable, independent variables listed below ']); ok=userInput; cnum=1:(2*lr+1); for i=1:(2*lr+1) C=char(colname{i}); fprintf('%d\t=\t%s\n',cnum(i),C); end stpwise(TM,X(:,1)); fprintf(' \n'); userInput=input('Remove lag terms to find the best minimax model and enter RMSE: '); rmse1=userInput; fprintf(' \n'); userInput=input('Enter number of terms in minimax model: '); terms1=userInput; mse1=rmse1*rmse1; sse1=mse1*(newl-terms1-1); Tln1=newl*log(sse1/newl); fprintf(' \n'); userInput=input('Remove cross-regressive terms for auto-regressive model and enter RMSE: '); rmse2=userInput; fprintf(' \n'); userInput=input('Enter number of terms in auto-regressive model: '); terms2=userInput; mse2=rmse2*rmse2; sse2=mse2*(newl-terms2-1); Tln2=newl*log(sse2/newl); Q1=Tln2-Tln1; df1=(terms1-terms2); pdf1=1-(chi2cdf(Q1,df1)); fprintf(' \n'); userInput=input(['Analysis with ' char(c2name) ' as dependent variable, independent variables listed below ']); TM=[H,X(:,1),G]; for i=1:lr %set up array of column names j=num2str(i); tx1=strcat(c2name,'-L',j); colname{i}=(tx1); end for i=(lr+1):(2*lr+1) j=num2str(i-lr-1); tx2=strcat(c1name,'-L',j); colname{i}=(tx2); end colname=colname'; ok=userInput; cnum=1:(2*lr+1); for i=1:(2*lr+1) C=char(colname{i}); fprintf('%d\t=\t%s\n',cnum(i),C); end stpwise(TM,X(:,2)); fprintf(' \n'); userInput=input('Remove lag terms to find the best minimax model and enter RMSE: '); rmse3=userInput; fprintf(' \n'); userInput=input('Enter number of terms in minimax model: '); terms3=userInput; mse3=rmse3*rmse3; sse3=mse3*(newl-terms3-1); Tln3=newl*log(sse3/newl); fprintf(' \n'); userInput=input('Remove cross-regressive terms for auto-regressive model and enter RMSE: '); rmse4=userInput; fprintf(' \n'); userInput=input('Enter number of terms in auto-regressive model: '); terms4=userInput; mse4=rmse4*rmse4; sse4=mse4*(newl-terms4-1); Tln4=newl*log(sse4/newl); Q2=(Tln4-Tln3); df2=(terms3-terms4); pdf2=1-(chi2cdf(Q2,df2)); fprintf(' \n'); %fprintf('Effect of\t on\t\t Q\t\t df\t\t P\n'); fprintf('Results: \n'); fprintf(' \n'); fprintf('Effect of %s on %s: Q(%d) = %6.3f, P = %6.4f\n',char(c2name),char(c1name),df1,Q1,pdf1); fprintf(' \n'); fprintf('Effect of %s on %s: Q(%d) = %6.3f, P = %6.4f\n',char(c1name),char(c2name),df2,Q2,pdf2); %fprintf('%s\t\t%s\t%6.3f\t%3.0f\t%8.4f\n',char(c2name),char(c1name),Q2,df2,pdf2); fprintf(' \n'); fprintf('ALL DONE!\n');