[R] Plotting GAM fit using RGL

Duncan Murdoch murdoch.duncan at gmail.com
Fri Aug 16 19:55:25 CEST 2013


On 13-08-15 1:15 PM, David Winsemius wrote:
>
> 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)))

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

>
>
>> 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.
>



More information about the R-help mailing list