mgcv gam: imposing constraints on coefficients I want to use pcls to constrain beta coefficient on a linear term in my gam.
Specifically, I have a model: gam(y ~ s(x1, ...) + x2, data=dat), and I want to make sure that x2's coefficient lies between -1 and 1. How can I do it?

The code below should do the job for you.  It uses BIC as its model selection criterion; you can substitute AIC or whatever else you want, of course.
library(mgcv)
library(data.table)

# Sample data that (almost certainly) violates the constraint
fitting_data <- data.table(x1 = rnorm(100),
                           x2 = rnorm(100),
                           x3 = rnorm(100))
fitting_data[, y := 2*x1 + x2 + sin(x3) + rnorm(100)]

G_gam = bam(y ~ x1 + x2 + s(x3),  data = fitting_data)

if (coef(G_gam)["x1"] < -1 | coef(G_gam)["x1"] > 1) {
  # G_gam_p1 -> the coefficient of x1 = 1
  # G_gam_m1 -> the coefficient of x1 = -1
  G_gam_p1 <- bam((y-x1) ~ x2 + s(x3), data=fitting_data)
  G_gam_m1 <- bam((y+x1) ~ x2 + s(x3), data=fitting_data)
  
  if (BIC(G_gam_p1) < BIC(G_gam_m1)) {
    final_model <- G_gam_p1
  } else {
    final_model <- G_gam_m1
  }

} else {
  final_model <- G_gam
}

Running this with the sample data above results in:
> final_model$formula
(y - x1) ~ x2 + s(x3)

from which it is easily deduced that the coefficient on x1 has been set to 1, which is as hoped for given that the actual coefficient is 2.
