%% Select Variables Using SSVS
% Consider the regression model in docid:econ_ug.mw_33fe5bc0-0b85-4cc1-9474-d2497f40714b.
% Create a prior model for performing stochastic search variable selection (SSVS). Assume that \beta and \sigma^2 \;are dependent (a conjugate mixture model). Specify the number of predictors p and the names of the regression coefficients.

p = 3;
PriorMdl = mixconjugateblm(p,'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'};

% Implement SSVS by estimating the marginal posterior distributions of \beta and \sigma^2. Because SSVS uses Markov chain Monte Carlo for estimation, set a random number seed to reproduce the results.

rng(1);
PosteriorMdl = estimate(PriorMdl,X,y);

% PosteriorMdl is an empiricalblm model object that stores draws from the posterior distributions of \beta and \sigma^2 given the data. estimate displays a summary of the marginal posterior distributions in the command line. Rows of the summary correspond to regression coefficients and the disturbance variance, and columns correspond 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 E (standardized) is in [0.000, 0.0.002] is 0.95.
% Regime, which contains the marginal posterior probability of variable inclusion (\gamma =1 for a variable). For example, the posterior probability E that should be included in the model is 0.0925.
% Assuming that variables with Regime < 0.1 should be removed from the model, the results suggest that you can exclude the unemployment rate from the model.
% By default, estimate draws and discards a burn-in sample of size 5000. However, a good practice is to inspect a trace plot of the draws for adequate mixing and lack of transience. Plot a trace plot of the draws for each parameter. You can access the draws that compose the distribution (the properties BetaDraws and Sigma2Draws) using dot notation.

figure;
for j = 1:(p + 1)
    subplot(2,2,j);
    plot(PosteriorMdl.BetaDraws(j,:));
    title(sprintf('%s',PosteriorMdl.VarNames{j}));
end
figure;
plot(PosteriorMdl.Sigma2Draws);
title('Sigma2');

% The trace plots indicate that the draws seem to mix well. The plots show no detectable transience or serial correlation, and the draws do not jump between states.
% Copyright 2018 The MathWorks, Inc.
%
%
%