[R] 3d plot of regression squared error

From: Ross Clement <R.P.Clement_at_westminster.ac.uk>
Date: Fri 04 Mar 2005 - 06:04:15 EST


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 )



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Mar 04 09:34:03 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:40 EST