如何在R中创建唯一的交互回归模型?
通常,我们从创建模型开始,包括在因变量上包含单个自变量,然后继续进行交互。但是,如果我们确定变量之间存在某种交互作用,并且正在寻找交互作用效果,那么只能创建交互回归模型。可以通过在变量之间使用冒号来表示交互来完成此操作,如以下示例所示。
例1
请看以下数据帧:
> x1<-rpois(20,1) > x2<-rpois(20,2) > x3<-rpois(20,5) > y<-rpois(20,10) > df1<-data.frame(x1,x2,x3,y) > df1
输出结果
x1 x2 x3 y 1 1 3 10 8 2 0 3 9 11 3 1 1 6 5 4 1 1 2 8 5 3 3 5 5 6 0 2 5 10 7 0 1 4 14 8 0 0 0 8 9 1 4 4 7 10 2 3 5 8 11 2 2 6 5 12 1 0 6 11 13 1 2 7 7 14 0 1 3 7 15 2 2 3 5 16 1 1 6 13 17 3 0 5 6 18 0 2 6 8 19 1 1 2 8 20 0 1 7 10
创建仅具有变量交互作用的回归模型:
示例
> Model1<-lm(y~x1:x2+x1:x3+x2:x3,data=df1) > summary(Model1)
输出结果
Call: lm(formula = y ~ x1:x2 + x1:x3 + x2:x3, data = df1) Residuals: Min 1Q Median 3Q Max -3.2093 -1.2583 -0.6080 0.5682 4.7907 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 9.19978 0.90379 10.179 2.14e-08 *** x1:x2 -0.39247 0.30978 -1.267 0.223 x1:x3 -0.14849 0.13996 -1.061 0.304 x2:x3 0.04883 0.06907 0.707 0.490 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 2.323 on 16 degrees of freedom Multiple R-squared: 0.3316, Adjusted R-squared: 0.2063 F-statistic: 2.646 on 3 and 16 DF, p-value: 0.08444
例2
> IV_1<-rnorm(20,5,1) > IV_2<-rnorm(20,5,0.98) > IV_3<-rnorm(20,20,3.25) > D<-rnorm(20,1,0.004) > df2<-data.frame(IV_1,IV_2,IV_3,D) > df2
输出结果
IV_1 IV_2 IV_3 D 1 3.827016 4.760877 17.64892 0.9960714 2 4.623803 5.152936 21.48162 1.0013076 3 5.783128 5.100011 16.10671 1.0017913 4 5.991171 4.718291 13.44091 1.0047349 5 5.229284 4.712669 22.01410 0.9996455 6 5.336851 5.460088 19.56821 1.0045880 7 4.352768 4.350663 18.58638 1.0003473 8 4.556793 2.853435 18.30430 0.9988200 9 6.161752 5.003778 23.22494 1.0020493 10 5.065051 5.845684 16.47238 1.0011333 11 4.317532 5.960619 23.11946 1.0015382 12 6.634342 4.714110 16.62322 1.0040192 13 4.415151 4.940207 19.55201 0.9988561 14 6.129936 6.481631 19.30220 0.9990506 15 5.581201 4.369263 21.86459 0.9991853 16 5.379293 4.871669 14.22654 1.0042899 17 4.689259 5.475507 19.16673 1.0078011 18 5.886390 4.367721 19.54971 1.0038578 19 3.358135 4.545323 21.81248 1.0041536 20 4.011436 5.555905 23.64763 1.0070755
示例
> Model2<-lm(D~IV_1:IV_2+IV_1:IV_3+IV_2:IV_3+IV_1:IV_2:IV_3,data=df2) > summary(Model2)
输出结果
Call: lm(formula = D ~ IV_1:IV_2 + IV_1:IV_3 + IV_2:IV_3 + IV_1:IV_2:IV_3, data = df2) Residuals: Min 1Q Median 3Q Max -0.0037361 -0.0021382 -0.0000464 0.0019465 0.0054880 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 9.516e-01 1.914e-02 49.720 <2e-16 *** IV_1:IV_2 1.981e-03 7.314e-04 2.709 0.0162 * IV_1:IV_3 4.910e-04 2.055e-04 2.389 0.0305 * IV_2:IV_3 5.106e-04 1.925e-04 2.652 0.0181 * IV_1:IV_2:IV_3 -1.989e-04 7.677e-05 -2.590 0.0205 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.002737 on 15 degrees of freedom Multiple R-squared: 0.3487, Adjusted R-squared: 0.175 F-statistic: 2.008 on 4 and 15 DF, p-value: 0.1451