%% Replace Removed Syntax When Estimating Analytical Marginal Posterior
% This example shows how to replace the removed syntax of estimate when it returns an analytical marginal posterior.
% Consider a multiple linear regression model that predicts US real gross national product (GNPR) using a linear combination of industrial production index (IPI), total employment (E), and real wages (WR). Assume the following:
% The intercept and three regression coefficients are random variables with a multivariate normal prior distribution conditional on the disturbance variance. The prior mean is a 4-D vector of zeros, and the prior covariance matrix is the 4-by-4 identity matrix scaled by 10,000.
% The disturbance variance is a random variable with an inverse gamma prior distribution. The shape and scale parameter values are 3 and 1, respectively.
% Create a normal-inverse-gamma conjugate prior model for the linear regression parameters. Set the number of predictors p. Set the regression coefficient names to the corresponding variable names.

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'};

% Before R2019b, estimate could return up to six outputs, each summarizing the posterior distribution. The previously supported syntax is:
% [PosteriorMdl,estBeta,EstBetaCov,estSigma2,estSigma2Var,Summary] = estimate(PriorMdl,X,y);
% For R2019b, estimate supports returning only two outputs: the posterior model PosteriorMdl and the estimation summary table Summary. Estimate the marginal posterior distribution by using the updated syntax. Return the posterior model and estimation summary table.

[PosteriorMdl,Summary] = estimate(PriorMdl,X,y);

% Compute posterior estimates by using the new procedure.

estBeta = PosteriorMdl.Mu % Posterior mean of coefficients
EstBetaCov = Summary.Covariances(1:(end - 1),1:(end - 1)) % Posterior covariance of coefficients
estSigma2 = Summary.Mean(end) % Posterior mean of disturbance variance
estSigma2Var = Summary.Covariances(end,end) % Posterior variance of disturbance variance

%
% Copyright 2019 The MathWorks, Inc.