Christina
Christina

Reputation: 935

Lasso regression in matlab

I am using lasso function in matlab 2013a. It works as follows:

    X = randn(100,5);
    r = [0;2;0;-3;0];
    Y = X*r + randn(100,1)*.1; 
%Construct the lasso fit using ten-fold cross validation. Include the FitInfo 
%output so you can plot the result.
    [B FitInfo] = lasso(X,Y,'CV',10); %B is a p-by-L matrix, where p is the %number of predictors (columns) in X, and L is the number of Lambda values

%Plot the cross-validated fits.
    lassoPlot(B,FitInfo,'PlotType','CV');

enter image description here

The green circle and dashed line locate the Lambda with minimal cross-validation error. The blue circle and dashed line locate the point with minimal cross-validation error plus one standard deviation.

So what I understand is that the green circle corresponds to the best value of lambda which minimizes the error. But how I can find "automatically" (without need of drawing the figure) the vector B which corresponds to the value of lambda shown as green circle in the figure?.

Any help will be very appreciated!

Upvotes: 0

Views: 5408

Answers (1)

Adriaan
Adriaan

Reputation: 18177

According to the documentation it should be in FitInfo.Lambda, which is a 1xL vector containing the lambdas. You can probably find it using min(FitInfo.Lambda). If you set the CV name-value pair to cross validate, the FitInfo structure contains additional fields: FitInfo.LambdaMinMSE which is the exact value you're looking for.

Thanks to @Christina, this is a slightly compacter way of writing it:

bestValue = find(FitInfo.Lambda == FitInfo.LambdaMinMSE)

This will give you the index where the minimum lambda is located in the array L

Upvotes: 1

Related Questions