%% Fit VAR Model of CPI and Unemployment Rate
% This example shows how to estimate the parameters of a VAR(4) model. The response series are quarterly measures of the consumer price index (CPI) and the unemployment rate.
% Load the Data_USEconModel data set.

load Data_USEconModel

%
% Plot the two series on separate plots.

figure;
plot(DataTable.Time,DataTable.CPIAUCSL);
title('Consumer Price Index');
ylabel('Index');
xlabel('Date');
figure;
plot(DataTable.Time,DataTable.UNRATE);
title('Unemployment rate');
ylabel('Percent');
xlabel('Date');

% The CPI appears to grow exponentially.
%
% Stabilize the CPI by converting it to a series of growth rates. Synchronize the two series by removing the first observation from the unemployment rate series.

rcpi = price2ret(DataTable.CPIAUCSL);
unrate = DataTable.UNRATE(2:end);

%
% Create a default VAR(4) model using the shorthand syntax.

Mdl = varm(2,4)

% Mdl is a varm model object. It serves as a template for model estimation. MATLABï¿½ considers any NaN values as unknown parameter values to be estimated. For example, the Constant property is a 2-by-1 vector of NaN values. Therefore, model constants are model parameters to be estimated.
%
% Fit the model to the data.

EstMdl = estimate(Mdl,[rcpi unrate])

% EstMdl is a varm model object.  EstMdl is structurally the same as Mdl, but all parameters are known. To inspect the estimated parameters, you can display them using dot notation.
%
% Display the coefficient of the first lag term.

EstMdl.AR{1}

%
% Display an estimation summary including all parameters, standard errors, and p-values for testing the null hypothesis that the coefficient is 0.

summarize(EstMdl)

% Copyright 2012 The MathWorks, Inc.