Consider the simple GAM fit as below:
library(mgcv)
my.gam <- gam(y~s(x), data=mydata)
- Is there anyway to return the estimated smoothing parameter (lambda) so that I can save it? I know that lambda is given in output as 'GCV score', but I need a specific code for returning it.
- How can I set lambda to a desired value?
summary()
does not return smoothing parameters. You have mixed up GCV score with smoothing parameter. Consult a local statistician if you don't understand those concepts, or raise a question on Cross Validated. I will only show you how to extract and set smoothing parameters.Consider an example:
You can get internal smoothing parameters from:
But these are not
lambda
. They differ fromlambda
by some positive scaling factors. It is usually sufficient to usesp
for smoothing parameters. If you want to set it to a fixed value, do for example:This essentially disables penalization for all smooth terms. Note that setting
sp
to a negative value implies auto-selection ofsp
.lambda
andsp
:Sometimes getting
lambda
is necessary. When considering smooth functions as random effects or random fields, there iswhere the scale parameter can be found in
b$scale
(for Gaussian model this is alsob$sig2
). See a related question: GAM with "gp" smoother: how to retrieve the variogram parameters?Follow-up
Have a read on
?smoothCon
:In the source code of
smoothCon
, there is:Briefly speaking, for a model matrix
X
and raw penalty matrixS
, scaling factormaS
is: