here is some sample R code for computing the sum of absolute errors and making a contour plot in the beta_0, beta_1 plane. sentences preceded by % are desriptions of what each line does. ---------------------------------------------------------------- %load the data into variable a a<-read.table("C:/Documents and Settings/Mandy Jin/Desktop/Epilepsy/1.txt") y<-a$V1 %read the first column into variable y x<-a$V2 %read the second column into variable y b0<-seq(-10,10,0.1) %make a sequence from -10 to 10 in steps of 0.1 b1<-seq(-1,1,0.01) %make a sequence from -1 to 1 in steps of 0.01 z<-matrix(0,201,201) %make a 2-d array to put the function values into for (i in 1:201) { for (j in 1:201) z[i,j]<-sum(abs(y-b0[i]-b1[j]*x)) %compute the function for each of the values in b0, b1 } %make a contour plot contour(b0,b1,z,xlim=c(-10,10),ylim=c(-1,1),nlevels=40) ---------------------------------------------------------------- The next bit of code does exactly the same thing, but plots the log of the cost function instead, to expose a little more of the detail. As we've discussed in class, taking the log of a function might change its shape (by stretching it or compressing it in the "vertical" direction), but it won't change the shape of its contours, or the location of its minimum or maximum. ---------------------------------------------------------------- a<-read.table("C:/Documents and Settings/Mandy Jin/Desktop/Epilepsy/1.txt") y<-a$V1 x<-a$V2 b0<-seq(-100,100,1) b1<-seq(-10,10,0.1) z<-matrix(0,201,201) for (i in 1:201) { for (j in 1:201) z[i,j]<-log(sum((y-b0[i]-b1[j]*x)^2)) } contour(b0,b1,z,xlim=c(-100,100),ylim=c(-10,10),nlevels=20)