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 [[alternative HTML version deleted]]
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:> 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
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:> 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: > > 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 > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]