%% Estimate ARIMAX Model
% Create an ARMAX(1,2) model for predicting changes in the US personal consumption expenditure based on changes in paid compensation of employees.
% Load the US macroeconomic data set.

load Data_USEconModel

% DataTable is a MATLAB® timetable containing quarterly macroeconomic measurements from 1947:Q1 through 2009:Q1. PCEC is the personal consumption expenditure series, and COE is the paid compensation of employees series.  Both variables are in levels.  For more details on the data, enter Description at the command line.
% The series are nonstationary. To avoid spurious regression, stabilize the variables by converting the levels to returns using price2ret. Compute the sample size.

pcecret = price2ret(DataTable.PCEC);
coeret = price2ret(DataTable.COE);
T = numel(pcecret);

% Because conversion from levels to returns involves applying the first difference, the transformation reduces the total sample size by one observation.
%
% Create an ARMA(1,2) model template using the shorthand syntax.

Mdl = arima(1,0,2);

% The exogenous component enters the model during estimation. Therefore, you do not need to set the Beta property of Mdl to a NaN so that estimate fits the model to the data with the other parameters.
% ARMA(1,2) process initialization requires Mdl.P = 1 observation. Therefore, the presample period is the first time point in the data (first row) and the estimation sample is the rest of the data. Specify variables identifying the presample and estimation periods.

idxpre = Mdl.P;
idxest = (Mdl.P + 1):T;

%
% Fit the model to the data.  Specify the presample by using the 'Y0' name-value pair argument, and specify the exogenous data by using the 'X' name-value pair argument.

EstMdl = estimate(Mdl,pcecret(idxest),'Y0',pcecret(idxpre),...
    'X',coeret(idxest));

% All estimates, except the lag 1 MA coefficient, are significant at 0.1 level.
% Display EstMdl.

EstMdl

% Like Mdl, EstMdl is an arima model object representing an ARMA(1,2) process. Unlike Mdl, EstMdl is fully specified because it is fit to the data, and EstMdl contains an exogenous component, so it is an ARMAX(1,2) model.
% Copyright 2019 The MathWorks, Inc.