mgcv:如何返回估计的平滑参数? [英] mgcv: How to return estimated smoothing parameter?
问题描述
考虑简单的GAM拟合如下:
Consider the simple GAM fit as below:
library(mgcv)
my.gam <- gam(y~s(x), data=mydata)
- 总有没有要返回估计的平滑参数(lambda)以便保存吗?我知道lambda在输出中以"GCV分数"给出,但是我需要一个特定的代码来返回它.
- 如何将lambda设置为所需值?
推荐答案
summary()
不返回平滑参数.您已将GCV得分与平滑参数混合在一起.如果您不了解这些概念,请咨询当地的统计学家,或者对交叉验证"提出疑问.我只会向您展示如何提取和设置平滑参数.
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.
考虑一个例子:
library(mgcv)
set.seed(2)
dat <- gamSim(1, n=400, dist="normal", scale=2)
b <- gam(y ~ s(x0) + s(x1) + s(x2) + s(x3), data=dat)
您可以从以下位置获取内部平滑参数:
You can get internal smoothing parameters from:
b$sp
# s(x0) s(x1) s(x2) s(x3)
#3.648590e+00 3.850127e+00 1.252710e-02 4.986399e+10
但是这些不是lambda
.它们与lambda
的区别在于某些正比例因子.使用sp
平滑参数通常就足够了.如果要将其设置为固定值,请执行以下操作:
But these are not lambda
. They differ from lambda
by some positive scaling factors. It is usually sufficient to use sp
for smoothing parameters. If you want to set it to a fixed value, do for example:
b1 <- gam(y ~ s(x0, sp = 0) + s(x1, sp = 0) + s(x2, sp = 0) + s(x3, sp = 0),
data = dat)
这实际上禁用了所有平滑词的惩罚.请注意,将sp
设置为负值意味着sp
的自动选择.
This essentially disables penalization for all smooth terms. Note that setting sp
to a negative value implies auto-selection of sp
.
lambda
和sp
:
lambda
and sp
:
sapply(b$smooth, "[[", "S.scale") / b$sp
# s(x0) s(x1) s(x2) s(x3)
#6.545005e+00 5.326938e+00 1.490702e+03 4.097379e-10
有时需要获取lambda
.当将平滑函数视为随机效果或随机字段时,有
Sometimes getting lambda
is necessary. When considering smooth functions as random effects or random fields, there is
variance_parameter_of_random_effect = scale_parameter / lambda
在b$scale
中可以找到scale参数(对于高斯模型,它也是b$sig2
).查看相关问题:带有"gp"的GAM,更平滑:如何检索方差图参数?
where the scale parameter can be found in b$scale
(for Gaussian model this is also b$sig2
). See a related question: GAM with "gp" smoother: how to retrieve the variogram parameters?
跟进
是的,我需要
lambda
的确切值,因此感谢简洁的代码.但是我有兴趣进一步了解比例因子.除了包装手册外,我还能在哪里阅读更多有关它的信息?
Yes, I need the exact value of
lambda
, so thanks for the neat code. Yet I'm interested in knowing more about the scaling factor. Where can I read more about it in addition to the package manual?
请阅读?smoothCon
:
smoothCon(object,data,knots=NULL,absorb.cons=FALSE,
scale.penalty=TRUE,n=nrow(data),dataX=NULL,
null.space.penalty=FALSE,sparse.cons=0,
diagonal.penalty=FALSE,apply.by=TRUE,modCon=0)
scale.penalty: should the penalty coefficient matrix be scaled to have
approximately the same 'size' as the inner product of the
terms model matrix with itself? ...
在smoothCon
的源代码中,有:
if (scale.penalty && length(sm$S) > 0 && is.null(sm$no.rescale)) {
maXX <- norm(sm$X, type = "I")^2
for (i in 1:length(sm$S)) {
maS <- norm(sm$S[[i]])/maXX
sm$S[[i]] <- sm$S[[i]]/maS
sm$S.scale[i] <- maS
}
}
简而言之,对于模型矩阵X
和原始惩罚矩阵S
,缩放因子maS
为:
Briefly speaking, for a model matrix X
and raw penalty matrix S
, scaling factor maS
is:
norm(S) / norm(X, type = "I")^2
这篇关于mgcv:如何返回估计的平滑参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!