> ins.reg1 <- lm(Y ~ X1 + X2) > summary(ins.reg1) Call: lm(formula = Y ~ X1 + X2) Residuals: Min 1Q Median 3Q Max -5.6915 -1.7036 -0.4385 1.9210 6.3406 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 33.874069 1.813858 18.675 9.15e-13 *** X1 -0.101742 0.008891 -11.443 2.07e-09 *** X2 8.055469 1.459106 5.521 3.74e-05 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 3.221 on 17 degrees of freedom Multiple R-squared: 0.8951, Adjusted R-squared: 0.8827 F-statistic: 72.5 on 2 and 17 DF, p-value: 4.765e-09 > anova(ins.reg1) Analysis of Variance Table Response: Y Df Sum Sq Mean Sq F value Pr(>F) X1 1 1188.17 1188.17 114.51 5.683e-09 *** X2 1 316.25 316.25 30.48 3.742e-05 *** Residuals 17 176.39 10.38 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > drop1(ins.reg1) Single term deletions Model: Y ~ X1 + X2 Df Sum of Sq RSS AIC 176.39 49.539 X1 1 1358.61 1535.00 90.811 X2 1 316.25 492.63 68.081 > > x1 <- seq(0,310,1) # Note this is "lower case" x1 and does not "mess up" X1 (for data) > > yhat_m <- coef(ins.reg1)[1] + coef(ins.reg1)[2]*x1 > > yhat_s <- coef(ins.reg1)[1] + coef(ins.reg1)[2]*x1 + coef(ins.reg1)[3] > > plot(x1,yhat_s,xlim=c(0,310),type="l",lty=1,main="Additive Model") > lines(x1,yhat_m,type="l",lty=2) > points(X1[X2==1],Y[X2==1],pch=1) > points(X1[X2==0],Y[X2==0],pch=2) > > > ############ Fit interaction model > > ins.reg2 <- lm(Y ~ X1 + X2 + I(X1*X2)) > summary(ins.reg2) Call: lm(formula = Y ~ X1 + X2 + I(X1 * X2)) Residuals: Min 1Q Median 3Q Max -5.7144 -1.7064 -0.4557 1.9311 6.3259 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 33.8383695 2.4406498 13.864 2.47e-10 *** X1 -0.1015306 0.0130525 -7.779 7.97e-07 *** X2 8.1312501 3.6540517 2.225 0.0408 * I(X1 * X2) -0.0004171 0.0183312 -0.023 0.9821 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 3.32 on 16 degrees of freedom Multiple R-squared: 0.8951, Adjusted R-squared: 0.8754 F-statistic: 45.49 on 3 and 16 DF, p-value: 4.675e-08 > anova(ins.reg2) Analysis of Variance Table Response: Y Df Sum Sq Mean Sq F value Pr(>F) X1 1 1188.17 1188.17 107.7819 1.627e-08 *** X2 1 316.25 316.25 28.6875 6.430e-05 *** I(X1 * X2) 1 0.01 0.01 0.0005 0.9821 Residuals 16 176.38 11.02 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > drop1(ins.reg2) Single term deletions Model: Y ~ X1 + X2 + I(X1 * X2) Df Sum of Sq RSS AIC 176.38 51.538 X1 1 667.02 843.40 80.834 X2 1 54.59 230.97 54.931 I(X1 * X2) 1 0.01 176.39 49.539 > > x1 <- seq(0,310,1) > > yhat_m <- coef(ins.reg2)[1] + coef(ins.reg2)[2]*x1 > > yhat_s <- coef(ins.reg2)[1] + coef(ins.reg2)[2]*x1 + coef(ins.reg2)[3] + coef(ins.reg2)[4]*x1 > > plot(x1,yhat_s,xlim=c(0,310),type="l",lty=1,main="Interaction Model") > lines(x1,yhat_m,type="l",lty=2) > points(X1[X2==1],Y[X2==1],pch=1) > points(X1[X2==0],Y[X2==0],pch=2) > > ############## Compare Models (F-test equivalent to t-test for beta1) > > anova(ins.reg1,ins.reg2) Analysis of Variance Table Model 1: Y ~ X1 + X2 Model 2: Y ~ X1 + X2 + I(X1 * X2) Res.Df RSS Df Sum of Sq F Pr(>F) 1 17 176.39 2 16 176.38 1 0.0057084 5e-04 0.9821 > >