0

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
IRTFM
  • 258,963
  • 21
  • 364
  • 487
Alex
  • 19,533
  • 37
  • 126
  • 195
  • Do you want it to be the nearest _knot_ value, or the nearest _fitted value_ within the range of the data? – Hong Ooi Sep 01 '13 at 21:10
  • Look at the `perimeter` function in pkg:rms. Does not do exactly what you want but does create a boundary for any two dimensions that strives to limit predictions outside of plausibility for 2 way interactions. – IRTFM Sep 01 '13 at 22:54
  • @HongOoi: nearest knot value – Alex Sep 01 '13 at 23:34

0 Answers0