[R] How to choose a proper smoothing spline in GAM of mgcv package?

Simon Wood s.wood at bath.ac.uk
Tue Oct 13 17:28:33 CEST 2009


> I have 5 datasets. I would like to choose a basis spline with same knots in
> GAM function in order to obtain same basis function for 5 datasets.
> Moreover, the basis spline is used to for an interaction of two covarites.
The `knots' argument to `gam' allows you to fix the knot locations used with a 
basis, and thereby obtain the same basis for each analysis. 

>
> I used "cr" in one covariate, but it can only smooth w.r.t 1 covariate. Can
> anyone give me some suggestion about how to choose a proper smoothing
> spline (bs='?') and knots for two covariates?
You can use the "tp" basis. Again use `knots' to supply the same set of knots 
for each dataset. For the "tp" basis I would pool you samples and take a 
largish (up to 1000) random sample of covariate pairs to use as the `knots'. 
The "tp" basis does not use the knot locations directly as knots, but rather 
as the starting point point for finding an optimal eigen-basis for the 
smoother (the only exception is if you supply exactly the same number of 
knots as the basis dimension).  

Alternatively use a tensor product of "cr" smooths for bivariate smoothing: 
see ?te. Again, supplying the same `knots' for all analyses fixes the basis 
used.


Finally, with some loss of computational efficiency, you can just fit all the 
data at once. Simply combine all the data frames, adding a column containing 
a five level factor variable indicating which original data set the data 
relate to (call it "set") then something like:

gam(y~s(x,z,by=set)+set)

will produce one smooth for each level of set. They will all use the same 
basis. You can force them to all have the same smoothing parameter as well 
with something like:

gam((y~s(x,z,by=set,id=1)+set)

The same thing works for `te' terms.

best,
Simon


>
> Thanks a lot.
>
> Lee
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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, Mathematical Sciences, University of Bath, Bath, BA2 7AY UK
> +44 1225 386603  www.maths.bath.ac.uk/~sw283




More information about the R-help mailing list