# lassoPlot

Trace plot of lasso fit

## Syntax

```ax = lassoPlot(B)ax = lassoPlot(B,FitInfo)ax = lassoPlot(B,FitInfo,Name,Value)[ax,figh] = lassoPlot(B,...)```

## Description

`ax = lassoPlot(B)` creates a trace plot of the values in `B` against the L1 norm of `B`. `ax` is a handle to the plot axis.

`ax = lassoPlot(B,FitInfo)` creates a plot with type depending on the data type of `FitInfo` and the value, if any, of the `plotType` name-value pair.

`ax = lassoPlot(B,FitInfo,Name,Value)` creates a plot with additional options specified by one or more `Name,Value` pair arguments.

```[ax,figh] = lassoPlot(B,...)``` returns a handle to the figure window.

## Input Arguments

 `B` Coefficients of a sequence of regression fits, as returned from the `lasso` or `lassoglm` functions. `B` is a `p`-by-`NLambda` matrix, where `p` is the number of predictors, and each column of `B` is a set of coefficients `lasso` calculates using one `Lambda` penalty value. `FitInfo` Information controlling the plot: `FitInfo` is a structure, especially as returned from `lasso` or `lassoglm` — `lassoPlot` creates a plot based on the `PlotType` name-value pair.`FitInfo` is a vector — `lassoPlot` forms the x-axis of the plot from the values in `FitInfo`. The length of `FitInfo` must equal the number of columns of `B`.

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

`'Parent'`

Axis in which to draw the plot.

Default: New plot

`'PlotType'`

Choose the plot type when you give a `FitInfo` vector or structure:

FitInfo TypePlotTypePlot
Vector or Structure`'L1'``plotLasso` creates the x-axis from the L1 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`.

Structure`'Lambda'``plotLasso` creates the x-axis from the `Lambda` field of `FitInfo`. The x-axis at the top of the plot contains the degrees of freedom (`df`), meaning the number of nonzero coefficients of `B`.

Cross-Validated Structure`'CV'`
• For each `Lambda`, plots an estimate of the mean squared prediction error on new data for the model fitted by `lasso` with that value of `Lambda`.

• Plots error bars for the estimates.

• Plots the value of `Lambda` with minimum cross-validated MSE.

• Plots the greatest `Lambda` that is within one standard error of minimum MSE (so makes the sparsest model within that region).

Default: `'L1'`

`'PredictorNames'`

Cell array of strings to label each coefficient of `B`. If the length of `PredictorNames` is less than the number of rows of `B`, the remaining labels are padded with default values.

`lassoPlot` uses the predictor names in `FitInfo` only if:

• You created `FitInfo` with a call to `lasso` that included a `PredictorNames` name-value pair.

• You call `lassoPlot` without a `PredictorNames` name-value pair.

• You include `FitInfo` in your `lassoPlot` call.

Default: `{'B1','B2',...}`

`'XScale'`

• `'linear'` for linear x-axis

• `'log'` for logarithmic scaled x-axis

Default: `'linear'`, except `'log'` for the `'CV'` plot type

## Output Arguments

 `ax` Handle to the axis of the plot (see Coordinate System). `figh` Handle to the figure window (see Special Object Identifiers).

## Examples

collapse all

### Lasso Plot with Default Plot Type

```load acetylene ```

Prepare the design matrix 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 `lasso` .

```B = lasso(D,y); ```

Plot the fits with the default plot type.

```lassoPlot(B); ```

### Lasso Plot with Lambda Plot Type

```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 with `lasso` .

```[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 Cross-Validated Fits

```load acetylene ```

Prepare the design matrix for a lasso fit with interactions.

```X = [x1 x2 x3]; D = x2fx(X,'interaction'); D(:,1) = []; % No constant term ```

Fit a regularized model of the data with `lasso` and cross validation.

```[B FitInfo] = lasso(D,y,'CV',10); ```

Plot the cross-validated fits.

```lassoPlot(B,FitInfo,'PlotType','CV'); ```