# [R] GAM, GAMM and numerical integration, help please

Simon Wood s.wood at bath.ac.uk
Fri May 14 17:36:34 CEST 2010

```
On Thursday 13 May 2010 21:59, Claudia Penaloza wrote:
> I am trying to apply methods used by Chaloupka & Limpus (1997) (
> http://www.int-res.com/articles/meps/146/m146p001.pdf) to my own turtle
> growth data.
>
> I am having trouble with two things...
>
> 1) After the GAM is fit, the residuals are skewed.
>
> >m1 <- gam(growth~s(mean.size,
>
> bs="cr")+s(year,bs="cr",k=7)+s(cohort,bs="cr")+s(age,bs="cr"),
> data=grow,
>
> >plot(m1, scale=0) #figure attached
>
> Should I try fitting a GAMM to model the tendencies in the residuals?
> How?
-- This is a bit hard to say without seeing the residual plots.

>
>
> 2) Using the GAM, I am able to fit a cubic-b spline but I do not know how
> to determine the equation for this spline nor how to integrate
> it/differentiate it (to obtain plots similar to 3c & f in Chaloupka &
> Limpus (1997)). What should I do next?
>
> >size <- seq(100,600,along.with=fitted(m1))
> >
> >grw <- fitted(m1)
> >
> >bspline3 <- gam(grw~s(size, bs="cr", k=3))
> >
> >plot(grw~size,ylab="Growth (mm/yr)", xlab="Curved Carapace Length
>
> (mm)")         #figure attached
>
> >lines(fitted(bspline3)~size, lwd=2)
>
It's best to do the integration and differentiation numerically. See the
example at the end of ?predict.gam, for a differentiation example. To
integrate I usually use predict.gam to evaluate the smooth on a fine mesh,
and then just sum the values and multiply by the mesh spacing (this is the
midpoint rule, you can also use the trapezoidal rule, simpson's rule or
something higher order, if you  prefer).

best,
simon
> Thank you very much for any help!
> Claudia

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

```