有交互项的多元线性回归

dom

若两个预测变量的交互项显著,说明响应变量与其中一个预测变量的关系依赖与另一个预测变量的水平函数

#以mtcars数据框中的汽车数据为例,查看汽车重量和马力对mpg的影响
> fit <- lm(mpg ~ hp + wt + hp:wt, data=mtcars)
> summary(fit)

Call:
lm(formula = mpg ~ hp + wt + hp:wt, data = mtcars)  # hp:wt 表示交互项

Residuals:
    Min      1Q  Median      3Q     Max 
-3.0632 -1.6491 -0.7362  1.4211  4.5513 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 49.80842    3.60516  13.816 5.01e-14 ***
hp          -0.12010    0.02470  -4.863 4.04e-05 ***
wt          -8.21662    1.26971  -6.471 5.20e-07 ***
hp:wt        0.02785    0.00742   3.753 0.000811 *** 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.153 on 28 degrees of freedom
Multiple R-squared:  0.8848,	Adjusted R-squared:  0.8724 
F-statistic: 71.66 on 3 and 28 DF,  p-value: 2.981e-13
  • effect()

effect包中effect()函数,能够用图形展现交互项的结果,格式为spa

plot(effect(term,mod,xlevels),mutilline = TRUE)

term:模型都要画的项code

mod:为经过lm()拟合的模型orm

xlevels:是一个列表,指定变量要设定的常量值ip

multiline:=TRUE选项表示添加相应的直线ci

it

#将上述例子中wt的值设置为2.2,3.2,4.2拟合成直线
> library(effects)
#Warning message:
#程辑包‘effects’是用R版本3.4.1 来建造的 
> plot(effect("hp:wt", fit,, list(wt=c(2.2, 3.2, 4.2))), multiline=TRUE)

 

相关文章
相关标签/搜索