Yan Li
2009-Oct-13 02:10 UTC
[R] How to choose a proper smoothing spline in GAM of mgcv package?
Hi, there, 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. 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? Thanks a lot. Lee [[alternative HTML version deleted]]
David Winsemius
2009-Oct-13 02:46 UTC
[R] How to choose a proper smoothing spline in GAM of mgcv package?
On Oct 12, 2009, at 10:10 PM, Yan Li wrote:> Hi, there, > > 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.It is possible to specify the knots for cr splines in the gam function (... not GAM). You have not described the sampling process that resulted in these 5 datasets, but the naive approach would be to cbind them and estimate for the super-group as a whole. After finding knots for a pooled dataset you could perhaps refit individually with the knots being fixed at the group values, assuming this makes sense for you sampling design.> > 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?Multiple terms in the model require vectors for the argument to sp or else inclusion within each smoothing term. See the docs and examples in: library(mgcv) ?gam Have you yet looked at: ?te -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Simon Wood
2009-Oct-13 15:28 UTC
[R] How to choose a proper smoothing spline in GAM of mgcv package?
> 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