Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

plotInteraction

Class: CompactLinearModel

Plot interaction effects of two predictors in linear regression model

Syntax

plotInteraction(mdl,var1,var2)
plotInteraction(mdl,var1,var2,ptype)
h = plotInteraction(___)

Description

plotInteraction(mdl,var1,var2) creates a plot of the interaction effects of the predictors var1 and var2 in mdl. The plot shows the estimated effect on the response from changing each predictor value, averaging out the effects of the other predictors. The plot also shows the estimated effect with the other predictor fixed at certain values. plotInteraction chooses values to produce a relatively large effect on the response. The plot lets you examine whether the effect of one predictor depends on the value of the other predictor.

plotInteraction(mdl,var1,var2,ptype) returns a plot of the type specified in ptype.

h = plotInteraction(___) returns handles to the lines in the plot, using any of the previous syntaxes.

Tips

  • For many plots, the Data Cursor tool in the figure window displays the x and y values for any data point, along with the observation name or number.

Input Arguments

expand all

Linear model object, specified as a full LinearModel object constructed using fitlm or stepwiselm, or a compacted CompactLinearModel object constructed using compact.

First variable for plot, specified as a variable name. plotInteraction chooses values of var1 to create relatively large changes in the response. If you set ptype equal to 'predictions', then the plot shows curves as a function of var2 with various fixed values of var1.

Second variable for plot, specified as a variable name. plotInteraction chooses values of var2 to create relatively large changes in the response. If you set ptype equal to 'predictions', then the plot shows curves as a function of var2 with various fixed values of var1.

Plot type, specified as one of the following.

  • 'effects' — The plot shows each effect as a circle, with a horizontal bar showing the confidence interval for the estimated effect. plotInteraction computes the effect values from the adjusted response curve, as shown by the plotAdjustedResponse function.

  • 'predictions' — The plot shows the adjusted response curve as a function of var2, with var1 fixed at certain values.

Output Arguments

expand all

Graphics handles, returned as a vector of graphics handles corresponding to the lines or patches in the plot.

Examples

expand all

Create a model of car mileage as a function of weight and model year. Then create a plot to see if the predictors have interactions.

Create a linear model of mileage from the carsmall data.

load carsmall
tbl = table(MPG,Weight);
tbl.Year = ordinal(Model_Year);
var1 = 'Year';
var2 = 'Weight';
mdl = fitlm(tbl,'MPG ~ Year * Weight^2');

Create an interaction plot.

plotInteraction(mdl,var1,var2)

The plot might show an interaction, because the groups of points are not perfectly vertical. But the error bars seem large enough that a vertical line could pass within all of the confidence intervals for each group, possibly indicating no interaction.

Create a model of car mileage as a function of weight and model year. Then create an interaction curve plot to see if the predictors have interactions.

Create a linear model of mileage from the carsmall data.

load carsmall
tbl = table(MPG,Weight);
tbl.Year = ordinal(Model_Year);
var1 = 'Year';
var2 = 'Weight';
mdl = fitlm(tbl,'MPG ~ Year * Weight^2');

Create an interaction plot with type 'predictions'.

plotInteraction(mdl,var1,var2,'predictions')

The curves are not parallel. This indicates interactions between the predictors. The effect is subtle enough not to definitively indicate a interaction.

Related Examples

Alternatives

Use plotEffects for an effects plot showing separate effects for all predictors.

Was this topic helpful?