[R] How to choose knots for GAM?
Simon Wood
s.wood at bath.ac.uk
Mon Sep 21 10:05:56 CEST 2009
Have you been plotting the columns of the model matrix against age, and
finding differences between replicates? If so, the differences that you see
are the differences induced by the *identifiability constraints* applied to
the different replicates. The identifiability constraint is that the smooth
should sum to zero over the covariate values, but the covariate values differ
between replicates. (Identifiability constraints are applied even if there is
only one smooth, but an intercept coefficient is always included, by default)
The space of representable functions is identical between the replicates,
since the identifiability constraints do no more than remove confounding with
the intercept. Here is a small example illustrating what's happening
library(mgcv)
set.seed(0) ## simulate some data...
## replicate 1
dat <- gamSim(1,n=400,dist="normal",scale=2)
b <- gam(y~s(x2,bs='cr'),data=dat,knots=list(x2=seq(-.1,1.1,length=10)))
## replicate 2
dat <- gamSim(1,n=400,dist="normal",scale=2)
b1 <- gam(y~s(x2,bs='cr'),data=dat,knots=list(x2=seq(-.1,1.1,length=10)))
par(mfrow=c(2,2))
## model matrix columns differ between replicates.
## because of constraint on smooth...
plot(b$model$x2,model.matrix(b)[,2],pch=".")
points(b1$model$x2,model.matrix(b1)[,2],pch=".",col=2)
plot(b$model$x2,model.matrix(b)[,3],pch=".")
points(b1$model$x2,model.matrix(b1)[,3],pch=".",col=2)
## underlying basis functions do not differ...
## get *unconstrained* model matrix for smooth
Xp <- Predict.matrix2(b$smooth[[1]],data=b$model)
Xp1 <- Predict.matrix2(b1$smooth[[1]],data=b1$model)
plot(b$model$x2,Xp[,1],pch=".")
points(b1$model$x2,Xp1[,1],pch=".",col=2)
plot(b$model$x2,Xp[,2],pch=".")
points(b1$model$x2,Xp1[,2],pch=".",col=2)
best,
Simon
On Sunday 20 September 2009 21:59, Yan Li wrote:
> Hi, all
>
> I want to choose same knots in GAM for 10 different studies so that they
> has the same basis function. Even though I choose same knots and same
> dimensions of basis smoothing, the basis representations are still not
> same.
>
> My command is as follows:
> data.gam<-gam(y~s(age,bs='cr',k=10)+male,family=binomial,knots=list(age=seq
>(45,64,length=10)))
>
>
> What is my mistake for choice of knots and dimension? Thank you very much.
>
> 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