%% Estimate Conditional Posterior Distributions
% Consider the regression model in docid:econ_ug.bvmabyl-1. This example uses the same data and context, but assumes a diffuse prior model instead.
% Create a diffuse 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','diffuse','VarNames',["IPI" "E" "WR"])

% PriorMdl is a diffuseblm model object.
%
% 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 that \sigma^2 = 2, and return the estimation summary table to access the estimates.

[Mdl,SummaryBeta] = 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 = SummaryBeta.Mean(1:(end - 1))
CondPostCovBeta = SummaryBeta.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.
%
% Estimate the conditional posterior distributions of \sigma^2 given that \beta is condPostMeanBeta.

[~,SummarySigma2] = estimate(PriorMdl,X,y,'Beta',condPostMeanBeta);

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

condPostMeanSigma2 = SummarySigma2.Mean(end)
CondPostVarSigma2 = SummarySigma2.Covariances(end,end)

%
% Copyright 2018 The MathWorks, Inc.