source('../../Data/data_loaders.R') DF = load_exercise_2_31_data() # Part (a): # plot( DF$YEAR, DF$TIME, type='p', pch=19, cex=1.5, xlab='YEAR', ylab='TIME' ) grid() # Part (b): # m = lm( TIME ~ YEAR, data=DF ) summary(m) # Based on the residuals vs. fitted plot it looks like we should add a quadradic term to our model: plot( m, which=1 ) m2 = lm( TIME ~ YEAR + I(YEAR^2), data=DF ) summary(m2) anova( m, m2 ) # Part (c): # years = c( 2050, 2100, 2200, 2400 ) years_transformed = years - 1900 time_prediction = predict( m2, newdata=data.frame( YEAR=years_transformed ) ) print(time_prediction) # Notice that these years have predictions that are far from realistic # Part (g): # all_xs = c( DF$YEAR, years_transformed ) y_proposed = ( 38504.5 + 44.38 * all_xs^2 ) / ( 1 + all_xs + all_xs^2 ) # preposed rational model all_ys = c( DF$TIME, y_proposed ) # plot the data: # #postscript("../../WriteUp/Graphics/Chapter2/ex_2_31_plot.eps", onefile=FALSE, horizontal=FALSE) plot( DF$YEAR, DF$TIME, type='p', pch=19, cex=1.5, xlab='YEAR (0=>1900)', ylab='TIME', xlim=range(all_xs), ylim=range(all_ys) ) # plot our linear model: points( all_xs, predict(m2, newdata=data.frame(YEAR=all_xs)), type='b', pch=19, cex=0.75, col='red' ) # plot the rational model: points( all_xs, y_proposed, type='l', col='blue' ) grid() #dev.off()