%% Estimate Conditional Posterior Distribution
% Consider the linear regression model in docid:econ_ug.bvj_bzk-1.
% 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'};

%
% Estimate the conditional posterior distribution of \beta given the data and \sigma^2 = 2, and return the estimation summary table to access the estimates.

[Mdl,Summary] = estimate(PriorMdl,X,y,'Sigma2',2);

% estimate displays a summary of the conditional posterior distribution of \beta. Because \sigma^2 is fixed at 2 during estimation, inferences on it are trivial.
% Extract the mean vector and covariance matrix of the conditional posterior of \beta from the estimation summary table.

condPostMeanBeta = Summary.Mean(1:(end - 1))
CondPostCovBeta = Summary.Covariances(1:(end - 1),1:(end - 1))

%
% Display Mdl.

Mdl

% Because estimate computes the conditional posterior distribution, it returns the original prior model, not the posterior, in the first position of the output argument list.
%
% Copyright 2019 The MathWorks, Inc.