%% Partition Time Series Data for Estimation
% This example shows how to partition the time base of the monthly international airline passenger data set Data_Airline to initialize  estimation and assess the predictive performance of the estimated model.
%
%% Load and Preprocess Data
% Load the data.

load Data_Airline

% The variable DataTable is a timetable containing the time series PSSG.
% Plot the time series.

plot(DataTable.Time,DataTable.PSSG)
xlabel('Time (months)')
ylabel('Passenger Counts')

% The series exhibits seasonality and an exponential trend.
% Determine whether the data has any missing values.

anymissing = sum(ismissing(DataTable))

% No missing observations are present.
% Stabilize the series by applying the log transform.

StblTT = varfun(@log,DataTable);

%
%% Partition Time Base
% Consider a SARIMA\left(0,1,1\right)\times \left(0,1,1{\left.\right)}_{12} \right. model for the log of the monthly passenger counts from 1949 through 1960. The model requires p+D+p_s +s=0+1+0+12=13 presample responses. An arima model template for estimation stores the required number of presample responses in the property P.
% Create a SARIMA\left(0,1,1\right)\times \left(0,1,1{\left.\right)}_{12} \right. model template for estimation. Specify that the model constant is 0. Verify the required number of presample observations by displaying the value of P using dot notation.

Mdl = arima('Constant',0,'D',1,'MALags',1,'SMALags',12,...
    'Seasonality',12);
Mdl.P

% Consider a forecast horizon of two years (24 months). Partition the response data into presample, estimation, and forecast sample variables.

fh = 24;                % Forecast horizon
T = size(StblTT,1);     % Total sample size
eT = T - Mdl.P - fh;    % Effective sample size

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

y0 = StblTT.log_PSSG(idxpre);  % Presample responses
y = StblTT.log_PSSG(idxest);   % Estimation sample responses
yf = StblTT.log_PSSG(idxfor);  % Forecast sample responses

%
%% Estimate Model
% Fit the model to the estimation sample.  Specify the presample by using the 'Y0' name-value pair argument.

EstMdl = estimate(Mdl,y,'Y0',y0);

% EstMdl is a fully specified arima model representing the estimated SARIMA model
% (1-L)(1-L^{12})y_t=(1-0.18L)(1-0.18L^{12})\varepsilon_t,
% where \varepsilon_t is Gaussian with a mean of 0 and a variance of 0.0019.
% Because the constant is 0 in the model template, estimate treats it as an equality constraint during optimization. Therefore, inferences on the constant are irrelevant.
% You can forecast the model using the forecast function of arima by specifying EstMdl and the forecast horizon fh.  To initialize the model for forecasting, specify the estimation sample response data y by using the 'Y0' name-value pair argument.
% Copyright 2020 The MathWorks, Inc.
%