%% Include Exogenous Predictor Variables
% Consider the model and data in Estimate VEC Model.
% Load the Data_USEconVECModel data set and preprocess the data.

load Data_USEconVECModel
FRED.GDP = 100*log(FRED.GDP);      
FRED.GDPDEF = 100*log(FRED.GDPDEF);
FRED.COE = 100*log(FRED.COE);       
FRED.HOANBS = 100*log(FRED.HOANBS); 
FRED.PCEC = 100*log(FRED.PCEC);     
FRED.GPDI = 100*log(FRED.GPDI);

%
% The Data_Recessions data set contains the beginning and ending serial dates of recessions. Load this data set. Convert the matrix of date serial numbers to a datetime array.

load Data_Recessions
dtrec = datetime(Recessions,'ConvertFrom','datenum');

%
% Create a dummy variable that identifies periods in which the U.S. was in a recession or worse. Specifically, the variable should be 1 if FRED.Time occurs during a recession, and 0 otherwise.

isin = @(x)(any(dtrec(:,1) <= x & x <= dtrec(:,2)));
isrecession = double(arrayfun(isin,FRED.Time));

%
% Create a VEC(1) model using the shorthand syntax. Assume that the appropriate cointegration rank is 4. You do not have to specify the presence of a regression component when creating the model. Specify the variable names.

Mdl = vecm(7,4,1);
Mdl.SeriesNames = FRED.Properties.VariableNames;

%
% Estimate the model using the entire sample. Specify the predictor identifying whether the observation was measured during a recession. Return the standard errors.

[EstMdl,EstSE] = estimate(Mdl,FRED.Variables,'X',isrecession);

%
% Display the regression coefficient for each equation and the corresponding standard errors.

EstMdl.Beta
EstSE.Beta

% EstMdl.Beta and EstSE.Beta are 7-by-1 vectors. Rows correspond to response variables in EstMdl.SeriesNames and columns correspond to predictors.
%
% To check whether the effects of recessions are significant, obtain summary statistics from summarize, and then display the results for Beta.

results = summarize(EstMdl);
isbeta = contains(results.Table.Properties.RowNames,'Beta');
betaresults = results.Table(isbeta,:)
whichsig = EstMdl.SeriesNames(betaresults.PValue < 0.05)

% All series except GDPDEF appear to have a significant recessions effect.
% Copyright 2012 The MathWorks, Inc.