%% Plot Distributions to Separate Figures
% Consider the regression model in Plot Prior and Posterior Distributions.
% Load the Nelson-Plosser data set, create a default conjugate prior model, and then estimate the posterior using the first 75% of the data. Turn off the estimation display.

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

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

d = 0.75;
PosteriorMdlFirst = estimate(PriorMdl,X(1:floor(d*end),:),y(1:floor(d*end)),...
    'Display',false);

%
% Plot the prior distribution and the posterior distribution of the disturbance variance. Return the figure handle.

[~,~,~,h] = plot(PosteriorMdlFirst,PriorMdl,'VarNames','Sigma2');

% h is the figure handle for the distribution plot. If you change the tag name of the figure  by changing the Tag property, then the next plot call places all new distribution plots on a different figure.
%
% Change the name of the figure handle to FirstHalfData using dot notation.

h.Tag = 'FirstHalfData';

%
% Estimate the posterior distribution using the rest of the data. Specify the posterior distribution based on the final 25% of the data as the prior distribution.

PosteriorMdl = estimate(PosteriorMdlFirst,X(ceil(d*end):end,:),...
    y(ceil(d*end):end),'Display',false);

%
% Plot the posterior of the disturbance variance based on half of the data and all the data to a new figure.

plot(PosteriorMdl,PosteriorMdlFirst,'VarNames','Sigma2');

% This type of plot shows the evolution of the posterior distribution when you incorporate new data.
%
% Copyright 2012 The MathWorks, Inc.