# [R] [Rd] Formulas in gam function of mgcv package

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Aug 24 18:33:55 CEST 2009

```[Note R-Devel is the wrong list for such questions. R-Help is where this
should have been directed - redirected there now]

On Mon, 2009-08-24 at 17:02 +0100, Corrado wrote:
> Dear R-experts,
>
> I have a question on the formulas used in the gam function of the mgcv
> package.
>
> I am trying to understand the relationships between:
>
> y~s(x1)+s(x2)+s(x3)+s(x4)
>
> and
>
> y~s(x1,x2,x3,x4)
>
> Does the latter contain the former? what about the smoothers of all
> interaction terms?

I'm not 100% certain how this scales to smooths of more than 2
variables, but Sections 4.10.2 and 5.2.2 of Simon Wood's book GAM: An
Introduction with R (2006, Chapman Hall/CRC) discuss this for smooths of
2 variables.

Strictly y ~ s(x1) + s(x2) is not nested in y ~ s(x1, x2) as the bases
used to produce the smoothers in the two models may not be the same in
both models. One option to ensure nestedness is to fit the more
complicated model as something like this:

## if simpler model were: y ~ s(x1, k=20) + s(x2, k = 20)
y ~ s(x1, k=20) + s(x2, k = 20) + s(x1, x2, k = 60)
^^^^^^^^^^^^^^^^^
where the last term (^^^ above) has the same k as used in s(x1, x2)

Note that these are isotropic smooths; are x1 and x2 measured in the
same units etc.? Tensor product smooths may be more appropriate if not,
and if we specify the bases when fitting models s(x1) + s(x2) *is*
strictly nested in te(x1, x2), eg.

y ~ s(x1, bs = "cr", k = 10) + s(x2, bs = "cr", k = 10)

is strictly nested within

y ~ te(x1, x2, k = 10)
## is the same as y ~ te(x1, x2, bs = "cr", k = 10)

[Note that bs = "cr" is the default basis in te() smooths, hence we
don't need to specify it, and k = 10 refers to each individual smooth in
the te().]

HTH

G

>
>
> I have (tried to) read the manual pages of gam, formula.gam, smooth.terms,
> linear.functional.terms but could not understand properly.
>
> Regards
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```