Main Content

plotSlice

Plot of slices through fitted generalized 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.mat

The variables Cylinders, MPG, and Weight contain data for the number of engine cylinders, miles per gallon, and weight for each car.

Create a table from the data in Cylinders, MPG, and Weight by using the categorical and table functions. Fit a generalized linear regression model using Cylinders and MPG as predictors and Weight as the response.

Cylinders = categorical(Cylinders);
tbl = table(Cylinders,MPG,Weight,VariableNames=["Cylinders" "MPG" "Weight"]);
mdl = fitglm(tbl,"Weight ~ MPG + Cylinders");

mdl is a GeneralizedLinearModel object that contains the result 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 3 objects of type errorbar, line. Axes object 2 contains 4 objects of type line, patch.

Each plot shows Weight as a function of a single predictor variable, Cylinders in the plot on the left and MPG 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 categorical predictor Cylinders are represented by error bars. The 95% confidence intervals for the continuous predictor MPG are represented by a blue shaded region. 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 Cylinders to 4.

GeneralizedLinearModel.plotSlice.png

The right plot shows that cars with four cylinders have smaller values for Weight, relative to cars with six cylinders, across all values of MPG.

Load the patients sample data set.

load patients.mat

The variables Age, Weight, and Smoker contain data for the age, weight, and smoking status of each patient.

Create a table from the data in Age, Weight, and Smoker by using the table function. Fit a generalized linear regression model to the data using Age and Weight as predictors and Smoker as the response.

tbl = table(Age,Weight,Smoker,VariableNames=["Age" "Weight" "Smoker"]);
mdl = fitglm(tbl,"Smoker ~ Age*Weight");

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

Generate new predictor data for patient age and weight.

Age_New = 25:35;
Weight_New = 100:10:200;
Xnew = [Age_New; Weight_New]';

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 4 objects of type line, patch.

The left plot shows Smoker as a function of Age with Weight fixed at 150. The right plot shows Smoker as a function of Weight with Age fixed at 30.

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 generalized linear regression model using cdate as the predictor and pop as the response.

mdl = fitglm(cdate,pop);

mdl is a GeneralizedLinearModel 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.

Input Arguments

collapse all

Generalized linear regression model, specified as a GeneralizedLinearModel object created using fitglm or stepwiseglm, or a CompactGeneralizedLinearModel object created 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

  • The Confidence Interval Type options in the figure window allow you to select simultaneous or non-simultaneous confidence bounds.

    • 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.

  • The Predictors list in the figure window lets you 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 GeneralizedLinearModel object provides multiple plotting functions.

    • 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 plotPartialDependence to understand the effect of a particular predictor. Also, use plotSlice to plot slices through the prediction surface.

Extended Capabilities

Version History

Introduced in R2012a

expand all