# Problem 15.2 speed=read.table("speed.txt") speed names(speed)=c("mpg","mph","V3") attach(speed) plot(mph,mpg) speed.lm=lm(mpg~mph) summary(speed.lm) anova(speed.lm) plot(fitted(speed.lm),resid(speed.lm)) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 12.74637 2.49877 5.101 2.57e-05 *** mph 0.03908 0.05312 0.736 0.469 Residual standard error: 5.666 on 26 degrees of freedom Multiple R-Squared: 0.02039, Adjusted R-squared: -0.01729 F-statistic: 0.5411 on 1 and 26 DF, p-value: 0.4686 Response: mpg Df Sum Sq Mean Sq F value Pr(>F) mph 1 17.37 17.37 0.5411 0.4686 Residuals 26 834.63 32.10 speed.lm2=lm(mpg~mph+I(mph^2)) summary(speed.lm2) anova(speed.lm2) plot(mph,mpg) lines(mph[order(mph)],fitted(speed.lm2)[order(mph)]) plot(fitted(speed.lm2),resid(speed.lm2)) plot(mph,resid(speed.lm2)) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -7.5555495 1.4241091 -5.305 1.69e-05 *** mph 1.2716937 0.0757321 16.792 3.99e-15 *** I(mph^2) -0.0145014 0.0008719 -16.633 4.97e-15 *** Residual standard error: 1.663 on 25 degrees of freedom Multiple R-Squared: 0.9188, Adjusted R-squared: 0.9123 F-statistic: 141.5 on 2 and 25 DF, p-value: 2.338e-14 Response: mpg Df Sum Sq Mean Sq F value Pr(>F) mph 1 17.37 17.37 6.2775 0.01911 * I(mph^2) 1 765.46 765.46 276.6417 4.973e-15 *** Residuals 25 69.17 2.77 speed.lm3=lm(mpg~mph+I(mph^2)+I(mph^3)+I(mph^4)) summary(speed.lm3) anova(speed.lm3) plot(mph,mpg) lines(mph[order(mph)],fitted(speed.lm3)[order(mph)]) plot(fitted(speed.lm3),resid(speed.lm3)) plot(mph,resid(speed.lm3)) vote=read.table("vote.txt") attach(vote) plot(Dem1980,Dem1984) vote.lm=lm(Dem1984~Dem1980) summary(vote.lm) plot(fitted(vote.lm),resid(vote.lm)) plot(Dem1980,Dem1984,pch=Southern) vote.lm2=lm(Dem1984~Dem1980+Southern) summary(vote.lm2) plot(fitted(vote.lm2),resid(vote.lm2)) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 12.09531 2.24456 5.389 2.24e-06 *** Dem1980 0.72250 0.05842 12.366 < 2e-16 *** Southern -7.78197 1.04467 -7.449 1.71e-09 *** Residual standard error: 2.856 on 47 degrees of freedom Multiple R-Squared: 0.7688, Adjusted R-squared: 0.7589 F-statistic: 78.13 on 2 and 47 DF, p-value: 1.134e-15 vote.lm3=lm(Dem1984~Dem1980*Southern) summary(vote.lm3) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 9.94338 2.30167 4.320 8.25e-05 *** Dem1980 0.77978 0.06011 12.973 < 2e-16 *** Southern 9.52243 7.05922 1.349 0.1840 Dem1980:Southern -0.38663 0.15616 -2.476 0.0170 * Residual standard error: 2.712 on 46 degrees of freedom Multiple R-Squared: 0.796, Adjusted R-squared: 0.7827 F-statistic: 59.82 on 3 and 46 DF, p-value: 6.548e-16 plot(Dem1980,Dem1984) lines(Dem1980[Southern==1],fitted(vote.lm3)[Southern==1]) lines(Dem1980[Southern==0],fitted(vote.lm3)[Southern==0])