# [R] plotting gam models

Simon Wood s.wood at bath.ac.uk
Wed Jul 25 12:40:28 CEST 2007

```What's wrong with the predict.gam(...,type="terms") suggestion (see
yesterday)?

Here's example code:
## example data from ?gam
n<-400;sig<-2
x <- runif(n, 0, 1);z <- runif(n, 0, 1)
x2 <- runif(n, 0, 1);x3 <- runif(n, 0, 1)
f0 <- function(x) 2 * sin(pi * x)
f1 <- function(x) exp(2 * x)
f2 <- function(x) 0.2*x^11*(10*(1-x))^6+10*(10*x)^3*(1-x)^10
f3 <- function(x) 0*x
f <- f0(x) + f1(z) + f2(x2)
e <- rnorm(n, 0, sig)
y <- f + e
b<-gam(y~s(x,z)+s(x2)+s(x3))

## make a grid of data over which to predict
zm <- xm <- seq(0,1,length=40)
pd <- data.frame(x=rep(xm,40),z=rep(zm,rep(40,40)),
x2=rep(.5,40^2),x3=rep(.5,40^2))
## predict, termwise
pv <- predict(b,newdata=pd,type="terms")
colnames(pv)
## plot result
contour(zm,xm,matrix(pv[,1],40,40))

On Wednesday 25 July 2007 11:43, Lucia Zarauz wrote:
> Hi again,
>
> I have found a post in R.help archives made from someone who had the same
> problem when exporting the smooth function estimate got from a GAM, to plot
> it into another graphic software
> (http://finzi.psych.upenn.edu/R/Rhelp02a/archive/23136.html)
>
> However, I haven't found any reply to this post.
>
> She suggested the function preplot() and I have hound that function in gam
> package, but not in mgcv package. Is it possible to do this in mgcv package
> or do I have to move to gam package?
>
> Thank you very much
>
> Lucía zarauz
>
>
> _____________________________________________
> AZTI - TecnaliaLucia Zarauz
> AZTI - Tecnalia / Unidad de Investigación Marina
> Herrera kaia portualdea z/g
> 20110 Pasaia (Gipuzkoa)
> Tel: 943 004 800 - Fax: 943 004 801
> e-mail: lzarauz at pas.azti.es
> www.azti.es ; www.tecnalia.info
>
> _____________________________________________
>
>
>
> **************** LEGE OHARRA **************** AVISO LEGAL ****************
> DISCLAIMER ****************
>
>
> Mezu hau pertsonala eta isilpekoa da eta baimenik gabeko erabilera
> debekatua dago legalki. Jasotzailea ez bazara ezabatu mezua, bidali eta
> kontserbatu gabe.
>
>
> Este mensaje es personal y confidencial y su uso no autorizado está
> prohibido legalmente. Si usted no es el destinatario, proceda a borrarlo,
> sin reenviarlo ni conservarlo.
>
>
> This message is personal and confidential, unauthorised use is legally
> prohibited. If you are not the intended recipient, delete it without
> resending or backing it.
>
> -----Mensaje original-----
> De: Lucia Zarauz
> Enviado el: martes, 24 de julio de 2007 13:53
> Para: 'Henrique Dallazuanna'
> CC: r-help at stat.math.ethz.ch
> Asunto: RE: [R] plotting gam models
>
> Hi Henrique,
>
> Thank you for your suggestion.
>
> Actually, I have already tried it, but I am confused because the plot I get
> is not the same as the output of plot(model) or plot.gam(model). The yaxis
> is different
>
> On the other hand, if I build a more complex model, as for example:
>
> model<- gam(x ~ s(lat,long) + s(temperature))
>
> I would like to extract the information to build the effects for each
> explanatory factor (one graph for s(lat,long) and another for
> s(temperature)), as R does when you use 'plot(model)' and you press return
> for subsequent pages.
>
> My final aim is to plot the influence of s(lat,long) as a contourplot
> superposed on a geographical map. Maybe there is an easier way to do it...
>
> Thank you very much
>
> lucía
>
>
>
> _____________________________________________
> Lucia Zarauz
> AZTI - Tecnalia / Unidad de Investigación Marina
> Herrera kaia portualdea z/g
> 20110 Pasaia (Gipuzkoa)
> Tel: 943 004 800 - Fax: 943 004 801
> e-mail: lzarauz at pas.azti.es
> www.azti.es ; www.tecnalia.info
> _____________________________________________
>
> **************** LEGE OHARRA **************** AVISO LEGAL ****************
> DISCLAIMER **************** Mezu hau pertsonala eta isilpekoa da eta
> baimenik gabeko erabilera debekatua dago legalki. Jasotzailea ez bazara
> ezabatu mezua, bidali eta kontserbatu gabe. Este mensaje es personal y
> confidencial y su uso no autorizado está prohibido legalmente. Si usted no
> es el destinatario, proceda a borrarlo, sin reenviarlo ni conservarlo. This
> message is personal and confidential, unauthorised use is legally
> prohibited. If you are not the intended recipient, delete it without
> resending or backing it.
>
> ________________________________________
> De: Henrique Dallazuanna [mailto:wwwhsd at gmail.com]
> Enviado el: martes, 24 de julio de 2007 13:36
> Para: Lucia Zarauz
> CC: r-help at stat.math.ethz.ch
> Asunto: Re: [R] plotting gam models
>
> see ?predict.gam

--
> Simon Wood, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK
> +44 1225 386603  www.maths.bath.ac.uk/~sw283

```