%% Directly Forecast Observations Using Posterior
% Consider the regression model in Forecast Responses Using Posterior Predictive Distribution.
% Create a normal-inverse-gamma semiconjugate prior model for the linear regression parameters. Specify the number of predictors p and the names of the regression coefficients.

p = 3;
PriorMdl = bayeslm(p,'ModelType','semiconjugate','VarNames',["IPI" "E" "WR"]);

%
% Load the Nelson-Plosser data set. Create variables for the response and predictor series.

load Data_NelsonPlosser
X = DataTable{:,PriorMdl.VarNames(2:end)};
y = DataTable{:,'GNPR'};

%
% Hold out the last 10 periods of data from estimation so you can use them to forecast real GNP. Turn off the estimation display.

fhs = 10; % Forecast horizon size
X = DataTable{1:(end - fhs),PriorMdl.VarNames(2:end)}; 
y = DataTable{1:(end - fhs),'GNPR'};
XF = DataTable{(end - fhs + 1):end,PriorMdl.VarNames(2:end)}; % Future predictor data
yFT = DataTable{(end - fhs + 1):end,'GNPR'};                  % True future responses

%
% Forecast responses by using the posterior predictive distribution and the future predictor data XF. Specify the in-sample observations X and y (the observations from which MATLAB® composes the posterior).

yF = forecast(PriorMdl,XF,X,y)

% Copyright 2012 The MathWorks, Inc.