How to get the predicted variables name in LASSO function?
5 views (last 30 days)
Show older comments
Guilherme de Freitas
on 14 Oct 2022
Commented: Guilherme de Freitas
on 14 Oct 2022
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?
0 Comments
Accepted Answer
the cyclist
on 14 Oct 2022
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
3 Comments
the cyclist
on 14 Oct 2022
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.)
More Answers (0)
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!