[R] setting constraints on gam

Simon Wood simon.wood at bath.edu
Fri Jan 12 10:20:47 CET 2018

There probably is a way, but it involves some programming. You would 
need to clone a smooth constructor (e.g. for the "cr" class), and then 
modify it to add a linear constraint matrix C to the returned smooth 
object. If b are the smooth coefficients then C should  be the matrix 
such that s(0) = Cb (you can get this from the Predict.matrix method for 
the class). Then the constraint Cb=0 will be applied during basis setup, 
and is equivalent to s(0)=0.

Now you can use your cloned class in a tensor product smooth, using the 
'ti' constructor. Suppose your cloned smooth class is called "foo", then


will create a smooth for which s(x,0)=0. Your requirement that s(x,0)=k 
is then taken care of by the model intercept.

If you want to try something similar with the full nested structure it's 
more complicated still. Then I think you would need something like

s(x,by=as.numeric(z!=0)) + s(z) + ti(x,z,bs=c("cr","foo"))


On 11/01/18 22:33, Alejandra Martínez Blancas wrote:
> I am fitting a model in which the response variable y is a function of
> two independent, quantitative variables x1 and x2; thus: y = f(x1,
> x2). For reasons I do not believe to be important for the purpose of
> this post, I find it desirable to find f by means of GAM; also, I
> require principal effects and interactions to be specified separately,
> so I am using using te and ti tensors. Thus, I am using the following
> command:
> f = gam(y ~ te(x1) + te(x2) + ti(x1, x2))
> This results in a model that corresponds to one of the hypotheses I am
> testing. Nevertheless, another hypothesis requires that, when one of
> the independent variables (say x2) is zero, the value of y is
> unaffected by the other variable (in this example x1). In other words
> f(x1, 0) = k for every value of x1, where k is a constant to be
> estimated. For x2 values other than zero I would like to let GAM
> choose the appropriate function relating x1 and y. Is there a way to
> specify such model in mgcv?
> ______________________________________________
> 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.

Simon Wood, School of Mathematics, University of Bristol BS8 1TW UK
+44 (0)117 33 18273     http://www.maths.bris.ac.uk/~sw15190

More information about the R-help mailing list