%% Attribute Different Shrinkage Values to Coefficients
% Consider the linear regression model in docid:econ_ug.mw_3c5ee5cc-859b-42d1-af6b-c8e525af7948 and its implementation in docid:econ_ug.mw_7e332876-b6b5-47dc-b08c-c74ec1859053.
% When you implement lasso regression, a common practice is to standardize variables. However, if you want to preserve the interpretation of the coefficients, but the variables have different scales, then you can perform differential shrinkage by specifying a different shrinkage for each coefficient.
% Create a prior model for performing Bayesian lasso regression. Specify the number of predictors p and the names of the regression coefficients.

p = 3;
PriorMdl = bayeslm(p,'ModelType','lasso','VarNames',["IPI" "E" "WR"]);

% Load the Nelson-Plosser data set. Create variables for the response and predictor series. Determine whether the variables have exponential trends by plotting each in separate figures.

load Data_NelsonPlosser
X = DataTable{:,PriorMdl.VarNames(2:end)};
y = DataTable{:,'GNPR'};

figure;
plot(dates,y)
title('GNPR')

for j = 1:3
    figure;
    plot(dates,X(:,j));
    title(PriorMdl.VarNames(j + 1));
end

% The variables GNPR, IPI, and WR appear to have an exponential trend.
% Remove the exponential trend from the variables GNPR, IPI, and WR.

y = log(y);
X(:,[1 3]) = log(X(:,[1 3]));

% All predictor variables have different scales (for more details, enter Description at the command line).  Display the mean of each predictor. Remove observations containing leading missing values from all predictors.

predmeans = mean(X,'omitnan')

% The values of the second predictor are much greater than those of the other two predictors and the response.  Therefore, the regression coefficient of the second predictor can appear close to zero.
% Using dot notation, attribute a very low shrinkage to the intercept, a shrinkage of 0.1 to the first and third predictors, and a shrinkage of 1000 to the second predictor.

PriorMdl.Lambda = [1e-5 0.1 1e4 0.1];

% Implement Bayesian lasso regression by estimating the marginal posterior distributions of \beta and \sigma^2. Because Bayesian lasso regression uses MCMC for estimation, set a random number seed to reproduce the results.

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

%
% Copyright 2018 The MathWorks, Inc.
%
%