[R] 3d plot of regression squared error
Deepayan Sarkar
deepayan at stat.wisc.edu
Thu Mar 3 17:18:07 CET 2005
On Thursday 03 March 2005 13:04, Ross Clement wrote:
> 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 )
Well, you need to go far enough away to see the curve. Centering the
x-variable is important (at least with your parametrization of straight
lines). E.g., the following combination gives a surface that looks nice
enough:
x <- 1:10 - 5.5
a.axis <- seq( 0, 10, length=20 )
b.axis <- seq( -30, 0, length=20 )
-Deepayan
> 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