# Part (a): # DF = data.frame( x=seq( 1, 5 ), y=c( 5.1, 2.2, 0.9, 1.9, 5.2 ) ) # Build some linear models: # m0 = lm( y ~ 1, data=DF ) summary(m0) m1 = lm( y ~ x, data=DF ) summary(m1) m2 = lm( y ~ x + I(x^2), data=DF ) summary(m2) # Compare the models sequentially using the anova command: # anova( m0, m1, m2 ) # Lets plot the data and models: # #postscript("../../WriteUp/Graphics/Chapter2/ex_2_21_part_a.eps", onefile=FALSE, horizontal=FALSE) plot( DF$x, DF$y, type='p', pch=19, cex=1.5, xlab='x', ylab='y' ) abline(m0, col='blue') abline(m1, col='red') points( DF$x, m2$fitted, type='l', col='green') grid() #dev.off() # Part (b): # DF = data.frame( x = seq( 1, 10 ), y = c( 15.5, 11.0, 13.5, 18.0, 24.5, 28.0, 32.0, 40.0, 43.0, 44.0 ) ) m0 = lm( y ~ 1, data=DF ) m1 = lm( y ~ x, data=DF ) m2 = lm( y ~ x + I(x^2), data=DF ) m3 = lm( y ~ x + I(x^2) + I(x^3), data=DF ) anova( m0, m1, m2, m3 ) # Lets plot the data and models: # #postscript("../../WriteUp/Graphics/Chapter2/ex_2_21_part_b.eps", onefile=FALSE, horizontal=FALSE) plot( DF$x, DF$y, type='p', pch=19, cex=1.5, xlab='x', ylab='y' ) abline(m1, col='blue') points( DF$x, m2$fitted, type='l', col='green') points( DF$x, m3$fitted, type='l', col='red') grid() #dev.off()