%% Specify AR Lags When Estimating FGLS Coefficients and Standard Errors
% Suppose the sensitivity of the U.S. Consumer Price Index (CPI) to changes in the paid compensation of employees (COE) is of interest. This example enhances the analysis outlined in the example Estimate FGLS Coefficients Using Default Options.
% Load the U.S. macroeconomic data set.

load Data_USEconModel

%
% The series are nonstationary. Stabilize them by applying the log, and then the first difference.

CPI = diff(log(DataTable.CPIAUCSL));
COE = diff(log(DataTable.COE));

%
% Regress CPI onto COE including an intercept to obtain OLS estimates. Plot correlograms for the residuals.

Mdl = fitlm(COE,CPI);
u = Mdl.Residuals.Raw;

figure;
subplot(2,1,1)
autocorr(u);
subplot(2,1,2);
parcorr(u);

% The correlograms suggest that the innovations have significant AR effects. According to Box-Jenkins Methodology, the innovations seem to comprise an AR(3) series.
%
% Estimate the regression coefficients using FGLS. By default, fgls assumes that the innovations are autoregressive. Specify that the innovations are AR(3) using the 'arLags' name-value pair argument.

[coeff,se] = fgls(CPI,COE,'arLags',3,'display','final');

% If the COE series is exogenous with respect to the CPI, then the FGLS estimates (coeff) are consistent and asymptotically more efficient than the OLS estimates.
% Copyright 2015 The MathWorks, Inc.