Skip to content

Plotting GAM fit using RGL

4 messages · Lucas Holland, Duncan Murdoch, David Winsemius

#
Hello all,

I?ve fitted a bivariate smoothing model (with GAM) to some data, using two explanatory variables, x and y.  Now I?d like to add the surface corresponding to my fit to a 3D scatterplot generated using plot3d(). 

My approach so far is to create a grid of x and y values and the corresponding predicted values and to try to use surface3d with that grid.

grid <- expand.grid(x = seq(-1,1,length=20),
                    y = seq(-1,1, length=20))

grid$z <- predict(fit.nonparametric, newdata=grid)

surface3d(grid$x, grid$y, matrix(grid$z, nrow=length(grid$x), ncol=length(grid$y)))

This however plots a number of surfaces that do not look like the fitted surface obtained by vis.gam(fit.nonparametric which actually looks a lot like the ?truth? (I?m using simulated data so I know the true regression surface). 

I think I?m using surface3d wrong but I can?t seem to spot my mistake. 

Thanks!
#
On Aug 15, 2013, at 2:23 AM, Lucas Holland wrote:

            
?surface3d
# Should be:

 surface3d( unique(grid$x), unique(grid$y),
                    z= matrix(grid$z, nrow=length(grid$x), ncol=length(grid$y)))
Always look at the Arguments section of help pages carefully.
1 day later
#
On 13-08-15 1:15 PM, David Winsemius wrote:
Or you could make x and y into matrices as well.  In this case you'll 
get the same result, but if x or y weren't strictly increasing 
sequences, there'd be a difference.

Duncan Murdoch
#
On Aug 16, 2013, at 10:55 AM, Duncan Murdoch wrote:

            
Thanks for increasing my knowledge on this point. And for providing rgl to the world. After looking at the Details section of the help page more carefully than I had previously, I wondered: Has anyone ever done a projection of a Klein bottle into rgl?

I didn't find one and my initial efforts with surface3d failed. (I managed to crash that seesion with a misguided call to the global replace function.)

I did get success with misc3d's parameteric3d with a parametrisation attributed to Robert Israel:

require(rgl); require(misc3d)

x = function(u,v){-(2/15)*cos(u)*(3*cos(v)-30*sin(u)+90*cos(u)^4*sin(u)- 60*cos(u)^6*sin(u)+5*cos(u)*cos(v)*sin(u))}

y = function(u,v){-(1/15)*sin(u)*(3*cos(v)-3*cos(u)^2*cos(v)-48*cos(u)^4*cos(v)+48*cos(u)^6*cos(v)-60*sin(u)+5*cos(u)*cos(v)*sin(u) 
    -5*cos(u)^3*cos(v)*sin(u) -80*cos(u)^5*cos(v)*sin(u)+80*cos(u)^7*cos(v)*sin(u))}

z = function(u,v){ (2/15)*(3+5*cos(u)*sin(u))*sin(v) }

parametric3d(x,y,z, seq(0,pi,length=100), seq(0,2*pi,length=100) )