Main Content

plotSlice

Plot of slices through fitted linear regression surface

Description

plotSlice(mdl) creates a figure containing one or more plots, each representing a slice through the regression surface predicted by mdl. Each plot shows the fitted response values as a function of a single predictor variable, with the other predictor variables held constant.

plotSlice also displays the 95% confidence bounds for the response values. You can specify the type of confidence bounds, and select which predictors to use for each slice plot. For more information, see Tips.

example

plotSlice(mdl,Xnew) uses the predictor data in Xnew to create the figure.

example

plotSlice(fig,___) plots into the figure fig instead of the current figure (gcf) using any of the input argument combinations in the previous syntaxes. (since R2024a)

Examples

collapse all

Load the carsmall sample data set.

load carsmall

The variables Weight and Model_Year contain data for the car weight and year of manufacture. The variable MPG contains data for miles per gallon.

Create a table from the data in Weight, Model_Year, and MPG by using the categorical and table functions. Fit a linear regression model of MPG as a function of Model_Year and Weight.

Model_Year = categorical(Model_Year);
tbl = table(MPG,Weight,Model_Year);
mdl = fitlm(tbl,"MPG ~ Model_Year + Weight^2");

mdl is a LinearModel object that contains the results of fitting a linear regression model to the data.

Create a slice plot for mdl.

plotSlice(mdl)

Figure Prediction Slice Plots contains 2 axes objects and another object of type uigridlayout. Axes object 1 contains 4 objects of type line, patch. Axes object 2 contains 3 objects of type errorbar, line.

Each plot shows MPG as a function of a single predictor variable, Weight in the plot on the left and Model_Year in the plot on the right. The predictor is fixed at the value shown in the x-axis of each plot. The 95% confidence intervals for the continuous predictor Weight are represented by a blue shaded region. The 95% confidence intervals for the categorical predictor Model_Year are represented by error bars. The dashed lines indicate a selected point, and the table to the left of the plots shows the response value and confidence bounds for the selected point. You can select a different point by changing the predictor values in the x-axis labels or by dragging the dashed lines to a different location.

Change the value of Weight to 4000.

linearmodel.plotSlice.png

The right plot shows that MPG is smaller for cars with a weight of 4000, relative to cars with a weight of 3264, across all values for Model_Year.

Load the carsmall sample data set.

load carsmall

The variables Weight and Model_Year contain data for the car weight and year of manufacture. The variable MPG contains data for miles per gallon.

Create a table from the data in Weight, Model_Year, and MPG by using the categorical and table functions. Fit a linear regression model of MPG as a function of Model_Year and Weight.

Model_Year = categorical(Model_Year);
tbl = table(MPG,Weight,Model_Year);
mdl = fitlm(tbl,"MPG ~ Model_Year + Weight^2");

mdl is a LinearModel object that contains the results of fitting a linear regression model to the data.

Generate new predictor data for car weight and year of manufacture by using the linspace and ones functions.

Weight_New = linspace(2500,3500,100)';
Model_Year_New = categorical([ones(50,1).*70; ones(50,1).*76]);
Xnew = table(Weight_New,Model_Year_New,VariableNames=["Weight","Model_Year"]);

Create a slice plot for mdl using the new predictor data.

plotSlice(mdl,Xnew)

Figure Prediction Slice Plots contains 2 axes objects and another object of type uigridlayout. Axes object 1 contains 4 objects of type line, patch. Axes object 2 contains 3 objects of type errorbar, line.

The left plot shows MPG as a function of Weight with Model_Year fixed at 76. The right plot shows MPG as a function of Model_Year with Weight fixed at 3000. The Confidence Interval Type options indicate that the confidence intervals are simultaneous and for the fitted responses. For more information, see Tips.

Load the census sample data set.

load census

The variables pop and cdate contain data for the population size and the year the census was taken, respectively.

Fit a linear regression model using cdate as the predictor and pop as the response.

mdl = fitlm(cdate,pop);

mdl is a LinearModel object that contains the fitted linear regression model.

Create a slice plot for mdl.

plotSlice(mdl)

Figure Prediction Slice Plots contains an axes object and another object of type uigridlayout. The axes object contains 5 objects of type line, patch. One or more of the lines displays its values using only markers

Because mdl has only one predictor, the slice plot displays a single set of axes containing a scatter plot of the training data. The regression line for mdl is shown in red together with a shaded region representing the 95% confidence intervals. The Confidence Interval Type options indicate that the confidence interval is simultaneous and for the fitted responses. For more information, see Tips.

Input Arguments

collapse all

Linear regression model object, specified as a LinearModel object created by using fitlm or stepwiselm, or a CompactLinearModel object created by using compact.

New predictor input values, specified as a table or numeric matrix. If Xnew is a table, its columns must have the same names as the predictors in mdl.VariableNames. If Xnew is a matrix, its columns must correspond to the predictors in mdl.VariableNames. Xnew cannot contain categorical predictor values not used during training.

Data Types: single | double | table

Since R2024a

Target figure, specified as a Figure object. You must create fig using the uifigure function. If you do not specify fig, then plotSlice creates a new figure.

Tips

  • Use the Confidence Interval Type menu in the figure window to choose the type of confidence bounds. You can choose simultaneous or non-Simultaneous, and curve or observation.

    • Simultaneous or Non-Simultaneous

      • Simultaneous (default) — plotSlice computes confidence bounds for the curve of the response values using Scheffé's method. The range between the upper and lower confidence bounds contains the curve consisting of true response values with 95% confidence.

      • Non-SimultaneousplotSlice computes confidence bounds for the response value at each observation. The confidence interval for a response value at a specific predictor value contains the true response value with 95% confidence.

      With simultaneous bounds, the entire curve of true response values is within the bounds at high confidence. By contrast, non-simultaneous bounds require only the response value at a single predictor value to be within the bounds at high confidence. Therefore, simultaneous bounds are wider than non-simultaneous bounds.

      If you do not want the plot to display confidence bounds, you can uncheck the Show Confidence Intervals in Plot box.

    • Curve or Observation

      A regression model for the predictor variables X and the response variable y has the form

      y = f(X) + ε,

      where f is a function of X and ε is a random noise term.

      • Curve (default) — plotSlice predicts confidence bounds for the fitted responses f(X).

      • ObservationplotSlice predicts confidence bounds for the response observations y.

      The bounds for y are wider than the bounds for f(X) because of the additional variability of the noise term.

  • Use the Predictors menu to select which predictors to use for each slice plot. If the regression model mdl includes more than eight predictors, plotSlice creates plots for the first five predictors by default.

Alternative Functionality

  • Use predict to return the predicted response values and confidence bounds. You can also specify the confidence level for confidence bounds by using the 'Alpha' name-value pair argument of the predict function. Note that predict finds nonsimultaneous bounds by default whereas plotSlice finds simultaneous bounds by default.

  • A LinearModel object provides multiple plotting functions.

    • When creating a model, use plotAdded to understand the effect of adding or removing a predictor variable.

    • When verifying a model, use plotDiagnostics to find questionable data and to understand the effect of each observation. Also, use plotResiduals to analyze the residuals of the model.

    • After fitting a model, use plotAdjustedResponse, plotPartialDependence, and plotEffects to understand the effect of a particular predictor. Use plotInteraction to understand the interaction effect between two predictors. Also, use plotSlice to plot slices through the prediction surface.

Extended Capabilities

Version History

Introduced in R2012a

expand all