[R] Plotting GAM fit using RGL

David Winsemius dwinsemius at comcast.net
Thu Aug 15 19:15:51 CEST 2013


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

> 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)))
> 
?surface3d
# Should be:

 surface3d( unique(grid$x), unique(grid$y),
                    z= 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. 


Always look at the Arguments section of help pages carefully.

-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list