Skip to content

MGCV:: boundary conditions in gam

3 messages · Mark Payne, Simon Wood

#
Dear R-help,

I have a problem where I am using the mgcv package to in a situation where
I am fitting a gam model with a 1-D spline smoother model over a domain
[a,b] but then need to make predictions and extrapolate beyond b. Is there
anyway where I force the first derivative of the spline to be zero at
boundaries, so that I simply get a constant value outside the domain?

Best wishes,

Mark
#
This first derivative penalty spline will do it, but the price paid is 
that the curves are often quite wiggly.


library(mgcv); set.seed(5)

x <- runif(100); y <- x^4 + rnorm(100)*.1

b <- gam(y~s(x,m=1))

pd <- data.frame(x=seq(-.5,1.5,length=200))

ff <- predict(b,pd,se=TRUE)

plot(x,y,xlim=c(-.5,1.5));lines(pd$x,ff$fit)

lines(pd$x,ff$fit+2*ff$se.fit,lty=2)

lines(pd$x,ff$fit-2*ff$se.fit,lty=2)
On 08/11/2018 15:26, Mark R Payne wrote:
4 days later
#
Perfect! This might be a good example to add to the documentation of mgcv
somewhere....

Thanks.

Mark
On Thu, 8 Nov 2018 at 22:08, Simon Wood <simon.wood at bath.edu> wrote: