lassoPlot
Trace plot of lasso fit
Description
lassoPlot(
creates a trace plot of the
values in B
)B
against the
L^{1} norm of B
.
lassoPlot(
creates a plot with additional options specified by one or more namevalue arguments.B
,FitInfo
,Name=Value
)
Examples
Lasso Plot with Default Plot Type
Load the sample data.
load acetylene
Prepare the design matrix for lasso fit with interactions. The x2fx
function returns the quadratic model in the order of a constant term, linear terms and interaction terms: constant term, x1
, x2
, x3
, x1.*x2
, x1.*x3
, and x2.*x3.
X = [x1 x2 x3]; D = x2fx(X,"interaction"); D(:,1) = []; % No constant term
Fit a regularized model of the data using the lasso
function.
B = lasso(D,y);
Plot the lasso fits with labeled coefficients by using the PredictorNames
namevalue argument. Each line represents a trace of the values in B for a single predictor variable: x1
, x2
, x3
, x1.*x2
, x1.*x3
, and x2.*x3
.
lassoPlot(B,PredictorNames=["x1" "x2" "x3" "x1.*x2" "x1.*x3" "x2.*x3"]); legend("show",Location="NorthWest")
Lasso Plot with Lambda Plot Type
Load the sample data.
load acetylene
Prepare the data for lasso fit with interactions.
X = [x1 x2 x3]; D = x2fx(X,"interaction"); D(:,1) = []; % No constant term
Fit a regularized model of the data using the lasso
function.
[B,FitInfo] = lasso(D,y);
Plot the fits with the Lambda
plot type and logarithmic scaling.
lassoPlot(B,FitInfo,PlotType="Lambda",XScale="log");
Lasso Plot with CrossValidated Fits
Visually examine the crossvalidated error of various levels of regularization.
Load the sample data.
load acetylene
Create a design matrix with interactions and no constant term.
X = [x1 x2 x3]; D = x2fx(X,"interaction"); D(:,1) = []; % No constant term
Construct the lasso fit using 10fold crossvalidation. Include the FitInfo
output so you can plot the result.
rng default % For reproducibility [B,FitInfo] = lasso(D,y,CV=10);
Plot the crossvalidated fits. The green circle and dotted line locate the Lambda
with minimum crossvalidation error. The blue circle and dotted line locate the point with minimum crossvalidation error plus one standard error.
lassoPlot(B,FitInfo,PlotType="CV"); legend("show")
Input Arguments
B
— Coefficients of sequence of regression fits
numeric matrix
FitInfo
— Fit information of models
structure  numeric vector
Fit information of the generalized linear models, specified as a structure or a numeric vector.
When
FitInfo
is a structure, such as returned from thelasso
orlassoglm
function, thelassoPlot
function creates a plot based on thePlotType
namevalue argument.When
FitInfo
is a vector, thelassoPlot
function forms the xaxis of the plot from the values inFitInfo
. The length ofFitInfo
must equal the number of columns ofB
.
NameValue Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Namevalue arguments must appear after other arguments, but the order of the
pairs does not matter.
Example: lassoPlot(B,XScale="log")
uses a logarithmic scaled
xaxis.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: lassoPlot(B,"XScale","log")
uses a logarithmic scaled
xaxis.
Parent
— Parent axes
Axes
object
Parent axes in which to draw the plot, specified as an Axes
object. If you do not specify Parent
, then
lassoPlot
creates the plot using the current axes. For more
information on creating an Axes
object, see axes
.
PlotType
— Plot type
"L1"
(default)  "Lambda"
 "CV"
Plot type, specified as "L1"
, "Lambda"
, or
"CV"
. The PlotType
argument applies when you
specify the FitInfo
argument.
PlotType  Plot 

"L1"  lassoPlot creates the xaxis from
the L^{1} norm of the coefficients
in B . The xaxis at the top of the
plot contains the degrees of freedom (df ), meaning the
number of nonzero coefficients of B . 

When
you choose this value, 

When you choose this value, 
If you include a crossvalidated FitInfo
structure,
lassoPlot
also indicates two specific Lambda
values with green and blue dashed lines.
A green, dashed line indicates the value of
Lambda
with a minimum crossvalidated mean squared error (MSE).A blue, dashed line indicates the greatest
Lambda
that is within one standard error of the minimum MSE. This Lambda value makes the sparsest model with relatively low MSE.
To display the label for each plot in the legend of the figure, type
legend("show")
in the Command Window.
PredictorNames
— Predictor names
{"B1","B2",...}
(default)  string scalar  character vector
Predictor names, specified as a string array or cell array of character vectors.
The predictor names label each coefficient of B
. If the length of
the PredictorNames
argument is less than the number of rows of
B
, the remaining labels are padded with default values.
If you do not specify this argument, but you specify the
FitInfo
argument as a structure and the
PredictorNames
field of the structure is not empty
({}
), then the lassoPlot
function uses those
predictor names instead of default values.
To display the legend with the predictor name as the label for each plot, type
legend("show")
in the Command Window.
XScale
— Scale of values along xaxis
"linear"
 "log"
Scale of values along the xaxis, specified as one of these values.
"linear"
— Linear xaxis. This is the default value when thePlotType
argument is"L1"
or"Lambda"
."log"
— Logarithmic scaled xaxis. This is the default value when thePlotType
argument is"CV"
.
Data Types: string
 char
Output Arguments
ax
— Axes of plot
Axes
object
Axes of the plot, returned as an Axes
object. For more information,
see Axes Appearance.
figh
— Figure window of plot
Figure
object
Figure window of the plot, returned as a Figure
object. For more
information, see Special Object Identifiers.
Version History
Introduced in R2011b
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
 América Latina (Español)
 Canada (English)
 United States (English)
Europe
 Belgium (English)
 Denmark (English)
 Deutschland (Deutsch)
 España (Español)
 Finland (English)
 France (Français)
 Ireland (English)
 Italia (Italiano)
 Luxembourg (English)
 Netherlands (English)
 Norway (English)
 Österreich (Deutsch)
 Portugal (English)
 Sweden (English)
 Switzerland
 United Kingdom (English)