%% Estimate Parameters of a Regression Model with ARIMA Errors Using Initial Values
% Fit a regression model with ARMA(1,1) errors by regressing the log GDP onto the CPI and using initial values.
% Load the US Macroeconomic data set and preprocess the data.

load Data_USEconModel;
logGDP = log(DataTable.GDP);
dlogGDP = diff(logGDP);        % For stationarity
dCPI = diff(DataTable.CPIAUCSL); % For stationarity
T = length(dlogGDP);           % Effective sample size

%
% Specify a regression model with ARMA(1,1) errors in which all estimable parameters are unknown.

EstMdl = regARIMA(1,0,1);

%
% Fit the model to the first half of the data.

EstMdl0 = estimate(EstMdl,dlogGDP(1:ceil(T/2)),...
    'X',dCPI(1:ceil(T/2)),'Display','off');

% The result is a new regARIMA model with the estimated parameters.
%
% Use the estimated parameters as initial values for fitting the second half of the data.

Intercept0 = EstMdl0.Intercept;
AR0        = EstMdl0.AR{1};
MA0        = EstMdl0.MA{1};
Variance0  = EstMdl0.Variance;
Beta0      = EstMdl0.Beta;

[EstMdl,~,~,info] = estimate(EstMdl,dlogGDP(floor(T/2)+1:end),...
    'X',dCPI(floor(T/2)+1:end),'Display','params',...
   'Intercept0',Intercept0,'AR0',AR0,'MA0',MA0,...
   'Variance0',Variance0,'Beta0',Beta0);

%
% Display all of the parameter estimates using info.X.

info.X

% The order of the parameter estimates in info.X matches the order that estimate displays in its output table.
% Copyright 2015 The MathWorks, Inc.