# R Code that makes a scatterplot for the DVD data with a fitted # line and computes the sum of squared errors (SSE). #Load DVD Sales y<-c(11.77, 11.64, 10.23, 9.99, 9.9, 9.89, 9.58, 9.47, 9.45, 9.43, 9.35, 9.15, 8.76, 8.47, 8.09, 7.92, 7.71, 7.7, 7.63, 7.56, 7.46, 7.34, 7.28, 7.18, 6.88, 6.42, 6.23, 5.92, 5.8, 5.7, 5.66, 5.54, 5.33, 5.13, 5.12, 5, 4.94, 4.47, 4.39, 4.38, 4.35, 4.35, 4.34, 4.27, 2.87, 2.83, 2.54, 2.3, 2.28, 2.26, 2.25, 1.92, 1.83, 1.37, 1.34, 1.3, 1.29, 1.23, 1.1, 1.05, 1.03, 0.94, 0.84, 0.76, 0.73, 0.68, 0.63, 0.62, 0.6, 0.58, 0.55) #Load Box Office Sales x<-c(57.65, 88.72, 50.82, 48.75, 82.23, 88.51, 48.55, 62.32, 36.9, 70.51, 41.78, 54.1, 12.51, 42.65, 81.61, 35.43, 61.12, 60.06, 47.4, 25.88, 47.86, 12.59, 82.57, 18.5, 33.74, 38.4, 17.13, 63.26, 5.85, 19.4, 70.17, 43, 12.71, 6.86, 14.73, 23.15, 18.6, 11.3, 21.17, 15.68, 9.02, 10.28, 11.75, 51.39, 4.24, 5.03, 6.09, 1.17, 47.14, 1.5, 4.84, 0.67, 2.08, 7.42, 2.91, 0.44, 11.47, 2.41, 0.11, 3.65, 0.74, 0.35, 1.67, 1.32, 5.13, 2.48, 11.72, 1.25, 1.44, 0.87, 0.67) scatterplot<-function(x,y,beta0,beta1){ #compute regression line: Ey<-beta0+beta1*x #compute SSE: SSE<-round(sum((y-Ey)^2),3) #make scatterplot: plot(x,y,ylim=range(c(y,Ey)),ylab=expression(beta[0]+beta[1]*x), main=paste("The line E(y) =",beta0,"+",beta1,"x","gives SSE =", SSE)) #Draw the regression lines: lines(x[order(x)],Ey[order(x)],lwd=2,col=3) #Draw the residuals: for(j in 1:length(y)){lines(rep(x[j],2),c(y[j],Ey[j]),col=4)} } #Plot an initial guess: scatterplot(x,y,2,0) #Compute the least squares fit bestfit<-round(lm(y~x)$coef,2) scatterplot(x,y,bestfit[1],bestfit[2])