Skip to content

3d plot of regression squared error

4 messages · Deepayan Sarkar, Ross Clement, Vivek Rao +1 more

#
On Thursday 03 March 2005 13:04, Ross Clement wrote:
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
#
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 )
#
The summary() function shows the min, median, mean,
max, and 25th and 75th percentiles, but not the
standard deviation, skew, and kurtosis (at least by
default). Is there are an option of summary() that
does this, or has someone written code for this?

Since the columns of my table are time series, I would
also like to display the autocrrelations of each
series up to a certain lag, as part of the summary.
Thanks.

Vivek Rao
Philadelphia, USA
#
Vivek Rao <vivekrao4 <at> yahoo.com> writes:

: 
: The summary() function shows the min, median, mean,
: max, and 25th and 75th percentiles, but not the
: standard deviation, skew, and kurtosis (at least by
: default). Is there are an option of summary() that
: does this, or has someone written code for this?
: 
: Since the columns of my table are time series, I would
: also like to display the autocrrelations of each
: series up to a certain lag, as part of the summary.
: Thanks.
: 

library(e1071) # to get the skewness and kurtosis functions
apply(x, 2, function(x) 
  c(sd = sd(x), skew = skewness(x), kurtosis = kurtosis(x), acf = acf(x)$acf))