lassoPlot - Trace plot of lasso fit - MATLAB (original) (raw)

Syntax

Description

lassoPlot([B](#bs25x%5Fb-1-B)) creates a trace plot of the values in B against the_L_1 norm of B.

lassoPlot([B](#bs25x%5Fb-1-B),[FitInfo](#bs25x%5Fb-1-FitInfo)) creates a plot with type depending on the data type of FitInfo and the value, if any, of the PlotType name-value argument.

lassoPlot([B](#bs25x%5Fb-1-B),[FitInfo](#bs25x%5Fb-1-FitInfo),[Name=Value](#namevaluepairarguments)) creates a plot with additional options specified by one or more name-value arguments.

example

[[ax](#bs25x%5Fb-1-ax),[figh](#bs25x%5Fb-1-figh)] = lassoPlot(___), for any previous input syntax, returns the axesax and the figure window figh that contain the plot of the lasso fit.

Examples

collapse all

Load the sample data.

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.

Plot the lasso fits with labeled coefficients by using the PredictorNames name-value 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(Location="NorthWest")

MATLAB figure

Load the sample data.

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");

MATLAB figure

Visually examine the cross-validated error of various levels of regularization.

Load the sample data.

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 10-fold cross-validation. Include the FitInfo output so you can plot the result.

rng default % For reproducibility [B,FitInfo] = lasso(D,y,CV=10);

Plot the cross-validated fits. The green circle and dotted line locate the Lambda with minimum cross-validation error. The blue circle and dotted line locate the point with minimum cross-validation error plus one standard error.

lassoPlot(B,FitInfo,PlotType="CV"); legend("show")

Figure contains an axes object. The axes object with title Cross-Validated MSE of Lasso Fit, xlabel Lambda, ylabel MSE contains 5 objects of type errorbar, line. One or more of the lines displays its values using only markers These objects represent MSE with Error Bars, LambdaMinMSE, Lambda1SE.

Input Arguments

collapse all

Coefficients of a sequence of regression fits, specified as a numeric matrix of size_p_-by-L, where p is the number of predictors and L is the number of regularization coefficientsLambda. You can calculate B using thelasso or lassoglm function.

Fit information of the generalized linear models, specified as a structure or a numeric vector.

Name-Value Arguments

collapse all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: lassoPlot(B,XScale="log") uses a logarithmic scaled x-axis.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: lassoPlot(B,"XScale","log") uses a logarithmic scaled x-axis.

Parent axes in which to draw the plot, specified as an Axes object. If you do not specify Parent, thenlassoPlot creates the plot using the current axes. For more information on creating an Axes object, see axes.

Plot type, specified as "L1", "Lambda", or"CV". The PlotType argument applies when you specify the FitInfo argument.

PlotType Plot
"L1" lassoPlot creates the _x_-axis from the _L_1 norm of the coefficients in B. The _x_-axis at the top of the plot contains the degrees of freedom (df), meaning the number of nonzero coefficients of B.
"Lambda" lassoPlot creates the_x_-axis from the Lambda field ofFitInfo. The _x_-axis 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, FitInfo must be a structure.
"CV" For each Lambda, lassoPlot plots an estimate of the mean squared prediction error on new data for the model fitted by lasso with that value ofLambda.lassoPlot plots error bars for the estimates.When you choose this value, FitInfo must be a cross-validated structure.

If you include a cross-validated FitInfo structure,lassoPlot also indicates two specific Lambda values with green and blue dashed lines.

To display the label for each plot in the legend of the figure, typelegend("show") in the Command Window.

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 ofB, the remaining labels are padded with default values.

If you do not specify this argument, but you specify theFitInfo argument as a structure and thePredictorNames 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, typelegend("show") in the Command Window.

Scale of values along the x-axis, specified as one of these values.

Data Types: string | char

Version History

Introduced in R2011b