Re: [R] Fitting generalized additive models with constraints?

From: Simon Wood <>
Date: Tue 05 Sep 2006 - 09:57:07 GMT

> I am trying to fit a GAM for a simple model, a simple model, y ~ s(x0) +
> s(x1) ; with a constraint that the fitted smooth functions s(x0) and s(x1)
> have to each always be >0.
> >From the library documentation and a search of the R-site and R-help
> archives I have not been able to decipher whether the following is possible
> using this, or other GAM libraries, or whether I will have to try to "roll
> my own". I see from the mgcv docs that GAMs need to be constrained such
> that the smooth functions have zero mean. Is there a way around this?
> Is such a constraint possible?
It is possible to estimate a GAM subject to this constraint, but be aware that the mean levels of your component smooths are not identifiable, so there is an unavoidable abitrariness in the estimate....

You have to have some sort of constraint on the smooths in a GAM to ensure identifiability, and a convenient way to set the model up is to write it as e.g.

E(y) = a + f0(x0) + f1(x1)

where `a' is the intercept and f0 and f1 are smooth functions which sum to zero over their respective covariate values. In this parameterization your constraint implies that

a + f0(x0) + f1(x1) > 0

for all x0, x1. If this constraint is met then you can find constants b and c such that b+c=a such that f0(x0)+b>0 and f1(x1)+c>0 for all x0,x1. i.e. you redefine f0 as f0+b and f1 as f1+c, and you have a fitted model meeting the constraints.

To fit the GAM subject to the constraints you can use mgcv:::pcls... ?pcls has some examples, but it does involve moderately low level programming. It's hard to impose the constraint exactly, so the usual approach would be to impose the constraint over a fairly fine grid of x0, x1 values. Also, you'll need to figure out how to select smoothing parameters. For many problems it suffices to estimate smoothing parameters on the unconstrained fit, and then use these to fit subject to constraints, but it depends on the problem....

Hope that's some use.


> thanks very much for any advice or pointers.
> -David
> [[alternative HTML version deleted]]
> ______________________________________________
> mailing list
> PLEASE do read the posting guide
> and provide commented, minimal,
> self-contained, reproducible code.


> Simon Wood, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK
> +44 1225 386603
______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.
Received on Tue Sep 05 19:59:31 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 07 Sep 2006 - 07:51:18 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.