tags:

views:

67

answers:

2

What packages and functions in R can perform a two dimensional non-additive local regression/smooth. For example consider

b<-seq(-6*pi,6*pi,length=100)
xy<-expand.grid(b,b) 
x=xy[[1]]
y=xy[[2]]
z= sin(x)+cos(y) + 2*sin(x)*cos(y)
contour(b,b,matrix(z,100,100))

alt text

What functions could estimate this?

+1  A: 

you can do this with loess:

fit <- loess( z ~ x+ y, span=0.01 )
dev.new()

contour( b, b, matrix( predict(fit), 100, 100 ) )
Greg Snow
+1  A: 

mgcv has a variety of 2-D spline options.

Ben Bolker
That was going to be my suggestion after I saw Greg's answer. However, when I tried this I needed to fit a very complex surface using a large number of knots and even then, the fit was not as good as the `loess()` one in this case. I used something like `gam( z ~ s(x, y, k = 200)` to get a reasonable fit. I didn't explore much further as my old laptop wasn't up to the task of fitting these models quickly.
Gavin Simpson