# Various regression techniques for the 'cars' data # Y = braking distance, X = speed # Data already in R; called 'cars' x = cars$speed y = cars$dist par(mfrow=c(2,2)) # Sets the plotting function to give a 2 by 2 panel of plots ## Two Least Squares fits plot(x, y, xlab="speed", ylab="dist", title(sub="LS fit")) # Fit a straight line fit1 = lm(dist ~ speed, data = cars) yhat = predict.lm(fit1) lines(x,yhat) # Fit a quadratic fit2 = lm(dist ~ speed + I(speed^2), data = cars) #omit the I() - what happens? yhat = predict.lm(fit2) lines(x,yhat) legend(x=2, y=125, legend = paste("lin.",1:2," = ", round(as.numeric(fit1$coef),2))) legend(x=2, y=100, legend = paste("quad.",1:3," = ", round(as.numeric(fit2$coef),2))) ## Two L1 - fits ## Here the sums of the ABSOLUTE VALUES of the residuals (not their SQUARES) is minimized ## A special package for this has to be loaded: # First go, in the menu, to Packages -> Set CRAN mirror (use HTTP mirrors, then Canada (BC)) # Then Packages -> Install Packages -> quantreg library(quantreg) plot(x, y, xlab="speed", ylab="dist", title(sub="L1 fit")) # Fit a straight line fit3 = rq(dist ~ speed, data = cars) yhat = predict(fit3) lines(x,yhat) # Fit a quadratic fit4 = rq(dist ~ speed + I(speed^2), data = cars) yhat = predict(fit4) lines(x,yhat) legend(x=2, y=125, legend = paste("lin.",1:2," = ", round(as.numeric(fit3$coef),2))) legend(x=2, y=100, legend = paste("quad.",1:3," = ", round(as.numeric(fit4$coef),2))) ## Smoothing spline fit (will be discussed later) plot(x, y, xlab="speed", ylab="dist", title(sub="spline fit")) fit5 = smooth.spline(x,y) yhat = predict(fit5, x=cars$speed)$y lines(x, yhat) ## Loess fit (will be discussed later) plot(x, y, xlab="speed", ylab="dist", title(sub="loess fit")) fit6 = loess(y~x) yhat = predict(fit6) lines(x, yhat)