%% Estimate Marginal Posterior Distributions
% Consider the linear regression model in Create Normal-Inverse-Gamma Conjugate Prior Model.
% Create a normal-inverse-gamma conjugate 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','conjugate','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 marginal posterior distributions of \beta and \sigma^2.

PosteriorMdl = estimate(PriorMdl,X,y);

% PosteriorMdl is a conjugateblm model object storing the joint marginal posterior distribution of \beta and \sigma^2 given the data. estimate displays a summary of the marginal posterior distributions to the command window. Rows of the summary correspond to regression coefficients and the disturbance variance, and columns to characteristics of the posterior distribution. The characteristics include:
% CI95, which contains the 95% Bayesian equitailed credible intervals for the parameters. For example, the posterior probability that the regression coefficient of WR is in [1.782, 3.154] is 0.95.
% Positive, which contains the posterior probability that the parameter is greater than 0. For example, the probability that the intercept is greater than 0 is 0.003.
% Distribution, which contains descriptions of the posterior distributions of the parameters. For example, the marginal posterior distribution of IPI is t with a mean of 4.39, a standard deviation of 0.14, and 68 degrees of freedom.
%
% Access properties of the posterior distribution using dot notation. For example, display the marginal posterior means by accessing the Mu property.

PosteriorMdl.Mu

% Copyright 2012 The MathWorks, Inc.