%% Include Exogenous Predictor Variables
% Estimate a VAR(4) model of consumer price index (CPI), the unemployment rate, and real gross domestic product (GDP). Include a linear regression component containing the current quarter and the last four quarters of government consumption expenditures and investment (GCE).
% Load the Data_USEconModel data set. Compute the real GDP.

load Data_USEconModel
DataTable.RGDP = DataTable.GDP./DataTable.GDPDEF*100;

%
% Plot all variables on separate plots.

figure;
subplot(2,2,1)
plot(DataTable.Time,DataTable.CPIAUCSL);
ylabel('Index');
title('Consumer Price Index');
subplot(2,2,2)
plot(DataTable.Time,DataTable.UNRATE);
ylabel('Percent');
title('Unemployment Rate');
subplot(2,2,3)
plot(DataTable.Time,DataTable.RGDP);
ylabel('Output');
title('Real Gross Domestic Product')
subplot(2,2,4)
plot(DataTable.Time,DataTable.GCE);
ylabel('Billions of $');
title('Government Expenditures')

%
% Stabilize the CPI, GDP, and GCE series by converting each to a series of growth rates. Synchronize the unemployment rate series with the others by removing its first observation.

inputVariables = {'CPIAUCSL' 'RGDP' 'GCE'};
Data = varfun(@price2ret,DataTable,'InputVariables',inputVariables);
Data.Properties.VariableNames = inputVariables;
Data.UNRATE = DataTable.UNRATE(2:end);

%
% Expand the GCE rate series to a matrix that includes its current value and up through four lagged values. Remove the GCE variable from Data.

rgcelag4 = lagmatrix(Data.GCE,0:4);
Data.GCE = [];

%
% Create a default VAR(4) model using the shorthand syntax.  You do not have to specify the regression component when creating the model.

Mdl = varm(3,4);

%
% Estimate the model using the entire sample. Specify the GCE rate matrix as data for the regression component. Extract standard errors and the loglikelihood value.

[EstMdl,EstSE,logL] = estimate(Mdl,Data.Variables,'X',rgcelag4);

%
% Display the regression coefficient matrix.

EstMdl.Beta

% EstMdl.Beta is a 3-by-5 matrix. Rows correspond to response series, and columns correspond to predictors.
%
%  Display the matrix of standard errors corresponding to the coefficient estimates.

EstSE.Beta

% EstSE.Beta is commensurate with EstMdl.Beta.
%
% Display the loglikelihood value.

logL

% Copyright 2012 The MathWorks, Inc.