[R] 3d plot of regression squared error

Ross Clement R.P.Clement at westminster.ac.uk
Thu Mar 3 20:04:15 CET 2005


Hi. I'm trying to create a 3d plot for a teaching example of finding a
least-squares estimate of the parameters to fit a line to some data. I
was hoping to get a nice plot with a clear, single minima where the
derivative of the surface is zero. No matter how much I tinker, I can't
seem to get a simple straightforward plot. Am I doing something wrong?

Thanks in anticipation,

Ross-c

x <- c( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
y <- c( 3, 4.2, 8.7, 11.7, 13.2, 19.1, 21, 25, 26.1, 29.8 )

sqe <- function( a, b ) {
   total <- 0
   for ( i in 1:length(x) ) {
       diff <- y[i] - a * x[i] + b
       total <- total + diff * diff
   }
   return( total )
}

df <- data.frame( x=x, y=y )

lm( y ~ x, df )

a.axis <- seq( -5, 10, length=20 )
b.axis <- seq( -20, 20, length=30 )

z <- outer( a.axis, b.axis, sqe )

persp( a.axis, b.axis, z, col="light grey", xlab="a", ylab="b",
zlab="sum.squared.error", theta=45 )




More information about the R-help mailing list