%% Update Marginal Posterior Distribution for New Data
% Consider the linear regression model in Create Empirical Prior Model.
% 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. Partition the data by reserving the last five periods in the series.

load Data_NelsonPlosser
X0 = DataTable{1:(end - 5),PriorMdl.VarNames(2:end)};
y0 = DataTable{1:(end - 5),'GNPR'};
X1 = DataTable{(end - 4):end,PriorMdl.VarNames(2:end)};
y1 = DataTable{(end - 4):end,'GNPR'};

%
% Estimate the marginal posterior distributions of \beta and \sigma^2.

rng(1); % For reproducibility
PosteriorMdl0 = estimate(PriorMdl,X0,y0);

% PosteriorMdl0 is an empiricalblm model object storing the Gibbs-sampling draws from the posterior distribution.
%
% Update the posterior distribution based on the last 5 periods of data by passing those observations and the posterior distribution to estimate.

PosteriorMdl1 = estimate(PosteriorMdl0,X1,y1);

% To update the posterior distributions based on draws, estimate uses sampling importance resampling.
% Copyright 2012 The MathWorks, Inc.