Main Content

plotInteraction

Plot interaction effects of two predictors in linear regression model

Description

example

plotInteraction(MDL,var1,var2)创建一个情节主要影响的the two selected predictorsvar1var2和他们conditional effectsin the linear regression modelMDL。通过效应值的水平线表示其95%的置信区间。

example

plotInteraction(MDL,var1,var2,PTYPE)specifies the plot typePTYPE。For example, ifPTYPEis'predictions', thenplotInteraction将调整后的响应函数绘制为第二个预测因子的函数,第一个预测变量固定在特定值下。有关详细信息,请参阅条件效应

h= plotInteraction(___)returns line objects using any of the input argument combinations in the previous syntaxes. Usehto modify the properties of a specific line after you create the plot. For a list of properties, see线属性

Examples

collapse all

拟合具有交互项的模型,并创建一个交互图,以显示主要效果和条件效果。

Using the data in the汽车舞数据集,创建包括交互项的响应值。首先,加载数据集并标准化预测变量数据。

load汽车舞Acceleration = normalize(Acceleration); Horsepower = normalize(Horsepower); Displacement = normalize(Displacement);

定义一个包括交互项的响应变量加速度*马力

y = Acceleration + 4*Horsepower + Acceleration.*Horsepower + Displacement;

在响应值中添加一些噪音。

rng('default')% For reproducibilityy = y + normrnd(10,0.25*std(y,'omitnan'),size(y));

Create a table that includes the predictor data and response values.

tbl = table(Acceleration,Horsepower,Displacement,y);

Fit a linear regression model.

MDL= fitlm(tbl,'y ~ Acceleration + Horsepower + Acceleration*Horsepower + Displacement + Horsepower*Displacement')
mdl = Linear regression model: y ~ 1 + Acceleration*Horsepower + Horsepower*Displacement Estimated Coefficients: Estimate SE tStat pValue __________ _______ _________ __________ (Intercept) 9.8652 0.16177 60.982 8.587e-77 Acceleration 0.63726 0.1626 3.9191 0.00016967 Horsepower 3.6168 0.34 10.638 9.273e-18 Displacement 0.95032 0.31828 2.9858 0.0036144 Acceleration:Horsepower 0.60108 0.1851 3.2473 0.0016209 Horsepower:Displacement -0.0096069 0.20947 -0.045863 0.96352 Number of observations: 99, Error degrees of freedom: 93 Root Mean Squared Error: 1.07 R-squared: 0.93, Adjusted R-Squared:0.927 F统计与常数模型:249,p值= 3.3E-52

pValue的the interaction term加速度*马力很小,这意味着相互作用项具有统计学意义。

创建一个互动图,以显示的主要影响和条件效应马力Acceleration

plotInteraction(mdl,'马力','Acceleration')

图包含一个轴对象。与标题相互作用和加速度的标题相互作用的轴对象包含12个类型线的对象。

For each predictor, the main effect point and its conditional effect points are not vertically aligned. Therefore, you cannot find any vertical lines that pass through the confidence intervals of the main and conditional effect points for each predictor. This plot indicates the existence of interaction effects on the response variable.

为了进行比较,创建一个互动图Displacement马力。Thisp- 此交互项的价值(Displacement*Horsepower) is large, meaning that the interaction term is not statistically significant.

plotInteraction(mdl,'Displacement','马力')

图包含一个轴对象。The axes object with title Interaction of Displacement and Horsepower contains 12 objects of type line.

对于每个预测指标,主要效应点及其条件效应点垂直对齐。该图表示没有相互作用。

Fit a model with an interaction term and create an interaction plot of adjusted response curves.

Using the data in the汽车舞数据集,创建包括交互项的响应值。首先,加载数据集并标准化预测变量数据。

load汽车舞Acceleration = normalize(Acceleration); Horsepower = normalize(Horsepower); Displacement = normalize(Displacement);

定义一个包括交互项的响应变量加速度*马力

y = Acceleration + 4*Horsepower + Acceleration.*Horsepower + Displacement;

在响应值中添加一些噪音。

rng('default')% For reproducibilityy = y + normrnd(10,0.25*std(y,'omitnan'),size(y));

Create a table that includes the predictor data and response values.

tbl = table(Acceleration,Horsepower,Displacement,y);

Fit a linear regression model.

MDL= fitlm(tbl,'y ~ Acceleration + Horsepower + Acceleration*Horsepower + Displacement + Horsepower*Displacement')
mdl = Linear regression model: y ~ 1 + Acceleration*Horsepower + Horsepower*Displacement Estimated Coefficients: Estimate SE tStat pValue __________ _______ _________ __________ (Intercept) 9.8652 0.16177 60.982 8.587e-77 Acceleration 0.63726 0.1626 3.9191 0.00016967 Horsepower 3.6168 0.34 10.638 9.273e-18 Displacement 0.95032 0.31828 2.9858 0.0036144 Acceleration:Horsepower 0.60108 0.1851 3.2473 0.0016209 Horsepower:Displacement -0.0096069 0.20947 -0.045863 0.96352 Number of observations: 99, Error degrees of freedom: 93 Root Mean Squared Error: 1.07 R-squared: 0.93, Adjusted R-Squared:0.927 F统计与常数模型:249,p值= 3.3E-52

pValue的the interaction term加速度*马力很小,这意味着相互作用项具有统计学意义。

Create an interaction plot that shows the adjusted response function as a function ofAcceleration, with马力固定在特定值。

plotInteraction(mdl,'马力','Acceleration','predictions')

图包含一个轴对象。The axes object with title Interaction of Horsepower and Acceleration contains 4 objects of type line. These objects represent Horsepower, -1.4506, 0.51529, 2.4812.

The curves are not parallel. This plot indicates interactions between the predictors.

为了进行比较,创建一个互动图Displacement马力。Thep- 此交互项的价值(Displacement*Horsepower) is large, meaning that the interaction term is not statistically significant.

plotInteraction(mdl,'Displacement','马力','predictions')

图包含一个轴对象。带排量和马力标题相互作用的轴对象包含4个类型线对象。这些对象表示位移,-1.0969,0.55827,2.2134。

The curves are parallel, indicating no interaction.

Input Arguments

collapse all

线性回归模型对象, specified as aLinearModelobject created by usingfitlm或者stepwiselm,或compactlinearmodelobject created by usingcompact

First variable for the plot, specified as a character vector or string array of the variable name inMDL。VariableNames(VariableNamesproperty ofMDL),或者一个正整数代表resenting the index of a variable inMDL。VariableNames

Data Types:char|string|单身的|double

图的第二个变量,在变量名称的字符向量或字符串数​​组中指定MDL。VariableNames(VariableNamesproperty ofMDL),或者一个正整数代表resenting the index of a variable inMDL。VariableNames

Data Types:char|string|单身的|double

Plot type, specified as one of these values:

  • 'effects'plotInteraction创建一个情节主要影响的the two selected predictorsvar1var2和他们conditional effects. Horizontal lines through the effect values indicate their 95% confidence intervals.

  • 'predictions'plotInteractionplots the adjusted response function as a function ofvar2, withvar1固定在特定值。

有关详细信息,请参阅主要效果条件效应

Output Arguments

collapse all

线对象,返回为向量。使用点符号查询并设置行对象的属性。有关详细信息,请参阅线属性

如果the plot type is'effects'(默认),h(1)corresponds to the circles that represent the main effect estimates, andh(2)h(3)correspond to the 95% confidence intervals for the two main effects. The remaining entries inhcorrespond to the conditional effects and their confidence intervals. The line objects associated with the main effects have the tag'main'。The line objects associated with the conditional effects ofvar1var2有标签'conditional1''conditional2', respectively.

如果the plot type is'predictions',每个条目hcorresponds to each curve on the plot.

More About

collapse all

主要效果

An effect, or main effect, of a predictor represents an effect of one predictor on the response from changing the predictor value while averaging out the effects of the other predictors.

对于预测变量xs, the effect is defined by

g(xsi) -g(xsj),

在哪里gis anAdjusted Responsefunction. TheplotEffectsfunction chooses the observationsij如下。对于不是序数的分类变量xsixsjare the predictor values that produce the maximum and minimum adjusted responses, respectively, so that the effect value is always positive. For a numeric variable or an ordinal categorical variable, the function chooses two predictor values that produce the minimum and maximum adjusted responses wherexsi<xsj

plotEffectsplots the effect value and the 95% confidence interval of the effect value for each predictor variable.

Adjusted Response

An adjusted response function describes the relationship between the fitted response and a single predictor, with the other predictors averaged out by averaging the fitted values over the data used in the fit.

预测变量的回归模型(x1,x2, …,xp)和the response variableyhas the form

yi=f(x1i,x2i, …,xpi) +ri,

在哪里fis a fitted regression function andris a residual. The subscriptirepresents the observation number.

第一个预测变量的调整后响应函数x1, for example, is defined as

g ( x 1 ) = 1 n i = 1 n f ( x 1 , x 2 i , x 3 i , ... , x p i ) ,

在哪里nis the number of observations. The adjusted response data value is the sum of the adjusted fitted value and the residual for each observation.

y ˜ i = g ( x 1 i ) + r i

plotAdjustedResponseplots the adjusted response function and the adjusted response data values for a selected predictor variable.

条件效应

当模型包含一个交互项时,一个预测变量的主要效果取决于另一个与之相互作用的预测变量的值。在这种情况下,一个预测因子给定特定值的条件效应有助于理解两个预测因子的实际效果。您可以通过使用条件效应值检查一个预测变量的效果是否取决于另一个预测变量的值。

To define a conditional effect, define the adjusted response function as a function of two predictor variables. For example, the adjusted response function ofx1x2is

h ( x 1 , x 2 ) = 1 n i = 1 n f ( x 1 , x 2 , x 3 i , ... , x p i ) ,

在哪里f是一个拟合的回归函数,nis the number of observations.

The conditional effect of one predictor (x2) given a specific value of another predictor (x1k)定义

h(x1k,x2i) -h(x1k,x2j).

To compute conditional effect values,plotInteraction选择观察结果ijx2in the same way as when the function computes the主要效果和chooses thex1kvalues. Ifx1是一个分类变量,然后plotInteraction计算所有级别的条件效应x1。如果x1is a numeric variable, thenplotInteractioncomputes the conditional effect for three values ofx1:最低值x1, the maximum value ofx1, 和the average value of the minimum and maximum.

如果the plot type is'effects'(默认),plotInteraction绘制了两个选定预测因子的主要影响,其条件效应以及效应值的95%置信界。

如果the plot type is'predictions',plotInteraction将调整后的响应函数绘制为第二个预测因子的函数,第一个预测变量固定在特定值下。For example,plotInteraction(mdl,'x1','x2','predictions')绘制曲线h(x1k,x2)for eachx1k价值。

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 thex- 轴和y-axis values for the selected point, along with the observation name or number.

替代功能

扩展功能

Introduced in R2012a