%% Replace Removed Syntax When Estimating Conditional Posterior
% This example shows how to replace the removed syntax of estimate when it returns a conditional 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. Specify 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; the latter five summarize the conditional posterior distribution. The previously supported syntax is:
% [~,estBeta,EstBetaCov,estSigma2,estSigma2Var,Summary] = estimate(PriorMdl,X,y);
% For R2019b, estimate supports returning only two outputs. The conditional posterior estimates are in the estimation summary table in the second output argument position.
% Estimate the conditional posterior distribution of the regression coefficients given that the disturbance variance is 10. Return the estimation summary table by using the updated syntax.

[~,Summary] = estimate(PriorMdl,X,y,'Sigma2',10);

% Compute posterior estimates by using the new procedure.

estBeta = Summary.Mean(1:end - 1) % Posterior mean of coefficients
EstBetaCov = Summary.Covariances(1:end - 1,1:end - 1) % Posterior covariance of coefficients

% Copyright 2019 The MathWorks, Inc.