constrain predictions within outer knots in mgcv::gam using R

554 Views Asked by At

I would like to fit a cubic spline using the gam function in mgcv package in R. Furthermore, I would like to constrain values outside of the training set (beyond the outer knots) to be equal to the nearest knot value. That is, no model prediction should be done outside the range of the training data. I know I can do this simply by eliminating those points in the predict call and then setting them to the min and max of the training data. However, is there a built in method in gam to do this (just so it's a bit cleaner?)

Example code:

require(mgcv)
x = 10:90
y = x^2
mdl = gam(y ~ s(x, bs="cr"))

needed_x = 1:100
p = predict(mdl, newdata = list(x = needed_x)) #this returns model values form 1:9 and 91:100
0

There are 0 best solutions below