Main Content

plot

Scatter plot or added variable plot of linear regression model

Description

plot(mdl) creates a plot of the linear regression model mdl. The plot type depends on the number of predictor variables.

  • If mdl includes multiple predictor variables, plot creates an Added Variable Plot for the whole model except the constant (intercept) term, equivalent to plotAdded(mdl).

  • If mdl includes a single predictor variable, plot creates a scatter plot of the data along with a fitted curve and confidence bounds.

  • If mdl does not include a predictor, plot creates a histogram of the residuals, equivalent to plotResiduals(mdl).

example

plot(ax,mdl) creates the plot in the axes specified by ax instead of the current axes.

h = plot(___) returns graphics objects for the lines or patch in the plot, using any of the input argument combinations in the previous syntaxes. Use h to modify the properties of a specific line or patch after you create the plot. For a list of properties, see Line Properties and Patch Properties.

Examples

collapse all

Create a linear regression model of car mileage as a function of weight and model year. Then create an added variable plot to see the significance of the model.

Create a linear regression model of mileage from the carsmall data set.

load carsmall
Year = categorical(Model_Year);
tbl = table(MPG,Weight,Year);
mdl = fitlm(tbl,'MPG ~ Year + Weight^2');

Create an added variable plot of the model.

plot(mdl)

Figure contains an axes object. The axes object with title Added Variable Plot for Whole Model, xlabel Adjusted Whole Model, ylabel Adjusted MPG contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Adjusted data, Fit: y = 8.44866*x, 95% conf. bounds.

The plot illustrates that the model is significant because a horizontal line does not fit between the confidence bounds.

Create the same plot by using the plotAdded function.

plotAdded(mdl)

Figure contains an axes object. The axes object with title Added Variable Plot for Whole Model, xlabel Adjusted Whole Model, ylabel Adjusted MPG contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Adjusted data, Fit: y = 8.44866*x, 95% conf. bounds.

Create a scatter plot of data along with a fitted curve and confidence bounds for a simple linear regression model. A simple linear regression model includes only one predictor variable.

Create a simple linear regression model of mileage from the carsmall data set.

load carsmall
tbl = table(MPG,Weight);
mdl = fitlm(tbl,'MPG ~ Weight')
mdl = 
Linear regression model:
    MPG ~ 1 + Weight

Estimated Coefficients:
                    Estimate        SE         tStat       pValue  
                   __________    _________    _______    __________

    (Intercept)        49.238       1.6411     30.002    2.7015e-49
    Weight         -0.0086119    0.0005348    -16.103    1.6434e-28


Number of observations: 94, Error degrees of freedom: 92
Root Mean Squared Error: 4.13
R-squared: 0.738,  Adjusted R-Squared: 0.735
F-statistic vs. constant model: 259, p-value = 1.64e-28

pValue of the Weight variable is very small, which means that the variable is statistically significant in the model. Visualize this result by creating a scatter plot of the data, along with a fitted curve and its 95% confidence bounds, using the plot function.

plot(mdl)

Figure contains an axes object. The axes object with title MPG vs. Weight, xlabel Weight, ylabel MPG contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Data, Fit, 95% conf. bounds.

The plot illustrates that the model is significant because a horizontal line does not fit between the confidence bounds, which is consistent with the pValue result.

Create the same plot by using the plotAdded function.

plotAdded(mdl)

Figure contains an axes object. The axes object with title Added Variable Plot for Weight, xlabel Adjusted Weight, ylabel Adjusted MPG contains 3 objects of type line. One or more of the lines displays its values using only markers These objects represent Adjusted data, Fit: y = -0.00861193*x, 95% conf. bounds.

When a model includes only one term in addition to the constant term, an adjusted value is equivalent to its original value. Therefore, this added variable plot is the same as the scatter plot created by the plot function.

Input Arguments

collapse all

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

Target axes, specified as an Axes object.

If you do not specify the axes and the current axes are Cartesian, then plot uses the current axes (gca). For more information on creating an Axes object, see axes and gca.

Output Arguments

collapse all

Graphics objects corresponding to the lines or patch in the plot, returned as a graphics array. Use dot notation to query and set properties of graphics objects. For details, see Line Properties and Patch Properties.

If mdl includes one or more predictors, then h(1), h(2), and h(3) correspond to adjusted data points, the fitted line, and the lower and upper bounds of the fitted line, respectively.

If mdl does not include a predictor, then h corresponds to the histogram of residuals.

More About

collapse all

Tips

  • The data cursor displays the values of the selected plot point in a data tip (small text box located next to the data point). The data tip includes the x-axis and y-axis values for the selected point, along with the observation name or number.

Alternative Functionality

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

  • The plot function creates an added variable plot for the model as a whole (except a constant term) if the model includes multiple terms. Use plotAdded to select particular predictors for an added variable plot.

Extended Capabilities

expand all

Version History

Introduced in R2012a