%% Test Multiple Time Series for Cointegration Using egcitest
% Load data on term structure of interest rates in Canada.

load Data_Canada
Y = Data(:,3:end);
names = series(3:end);
plot(dates,Y)
legend(names,'location','NW')
grid on

%
% Test for cointegration (and reproduce row 1 of Table II in docid:econ_ug#bsw6k0a-11).

[h,pValue,stat,cValue,reg] = egcitest(Y,'test',...
    {'t1','t2'});
h,pValue

%
% Plot the estimated cointegrating relation y_1 -Y_2 b-\textrm{Xa}.

a = reg(2).coeff(1);
b = reg(2).coeff(2:3);
plot(dates,Y*[1;-b]-a)
grid on

% Copyright 2020 The MathWorks, Inc.