How to get the predicted variables name in LASSO function?

Hi there,
I am currently using the function [ba,fitinfoa] = lasso() as a feature selection. And, in the Matlab documentation for this function (https://www.mathworks.com/help/stats/lasso.html) says that the output [fitinfoa] gives these variables in the structure: Intercept, Lambda, Alpha, DF, MSE, PredictorNames, UseCovariance, SE, LambdaMinMSE, Lambda1SE, IndexMinMSE, and Index1SE.
So, here is my question: How exactly should be in the function for the fitinfoa displays the actual features chosen by the model in the PredictorNames.
Here is my code:
%% LASSO
var_names_test = {'a1','a2','a3','a4','a5','a6','a7','a8','a9','a10'};
test_array = rand(10:10);
predictor_array = [1 0 1 0 0 0 1 0 1 1].';
[ba,fitinfoa] = lasso(test_array,double(predictor_array),'CV',5,'Alpha',1);
Lasso_all = lassoPlot(ba,fitinfoa,'PlotType','L1','XScale','linear',...
'PredictorNames',var_names_test);
set(findall(gcf,'-property','FontSize'),'FontSize',9)
set(findall(gcf,'-property','LineWidth'),'LineWidth',1)
box off
xlabel('L1 norm')
ylabel('Regression coefficients')
legend('show')
%% Structured output (call fitinfoa)
fitinfoa
%should display something like this
%struct with fields:
%Intercept: [2.393890179365678 … ]
%Lambda: [0.001605722707983 … ]
%Alpha: 1
%DF: [9 9 9 9 9 9 9 9 9 9 9 8 8 8 … ]
%MSE: [0.877111834461079 … ]
%PredictorNames: {}
%UseCovariance: 0
%SE: [0.245298269937858 … ]
%LambdaMinMSE: 0.127250778557508
%Lambda1SE: 0.354082850313371
%IndexMinMSE: 48
%Index1SE: 59
As you can see, PredictorNames: {} is empty, although a few variables were selected!
No idea why. Can someone help me, please?

 Accepted Answer

You only supplied the PredictorNames input in your call to LassoPlot, not to lasso itself.
var_names_test = {'a1','a2','a3','a4','a5','a6','a7','a8','a9','a10'};
test_array = rand(10:10);
predictor_array = [1 0 1 0 0 0 1 0 1 1].';
[ba,fitinfoa] = lasso(test_array,double(predictor_array),'CV',5,'Alpha',1,'PredictorNames',var_names_test);
fitinfoa.PredictorNames
ans = 1×10 cell array
{'a1'} {'a2'} {'a3'} {'a4'} {'a5'} {'a6'} {'a7'} {'a8'} {'a9'} {'a10'}

3 Comments

Thanks! Helped a lot!
On the same topic, do you know how I can check for the variables that had non-zero coefficients in the model without the plot?
In example: in the case, that if only variables {'a1'}, {'a2'} and {'a3'} were considered "not irrelevant", and confirmed that by looking at the plot. How could I get an array of these variables?
I saw a video in python, but I do not know how to do it in Matlab.
You can infer that from the first output, ba.
There is one column of ba for each value of Lambda. The zero coefficients are the "not relevant" ones, as you call them. (The number of zero coefficients depends on the value of Lambda, of course.)

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!