问题描述
我不明白为什么以下两种gam模型会产生不同的结果.唯一的区别是在其中一个模型中,我在函数gam
和s
之前添加了名称空间说明符gam::
.
I don't understand why the below two gam models produce different results. The only difference is in one of the models I added the namespace specifier gam::
before the functions gam
and s
.
我要这样做是因为我正在探索在gam程序包和mgcv程序包中运行gam函数之间的区别.
I want to do this because I am exploring the differences between running the gam function in the gam package and in the mgcv package.
library(ISLR)
library(gam)
gam.m3 <- gam::gam(wage ~ gam::s(year,4) + gam::s(age,5) + education,data=Wage)
gam.m3.orig <- gam(wage ~ s(year,4) + s(age,5) + education, data=Wage)
#Coefficients are different
coef(gam.m3)[1]; coef(gam.m3.orig)[1]
#Models are different
gam.m3$df.residual; gam.m3.orig$df.residual
这是输出.似乎系数和自由度应该没有什么不同;实际上,这两个模型应该完全相同.但是,它们是不同的,我不明白为什么.欢迎任何建议,我现在有点不知所措.
Here is the output. It seems like the coefficients and degrees of freedom should not be different; in fact the two models should be exactly the same. But, they are different and I don't understand why. Any suggestions welcome, I'm kind of at a loss right now.
> library(ISLR)
> library(gam)
Loading required package: splines
Loading required package: foreach
Loaded gam 1.16
> gam.m3 <- gam::gam(wage ~ gam::s(year,4) + gam::s(age,5) + education, data=Wage)
Warning message:
In model.matrix.default(mt, mf, contrasts) :
non-list contrasts argument ignored
> gam.m3.orig <- gam(wage ~ s(year,4) + s(age,5) + education, data=Wage)
Warning message:
In model.matrix.default(mt, mf, contrasts) :
non-list contrasts argument ignored
>
> #Coefficients are different
> coef(gam.m3)[1]; coef(gam.m3.orig)[1]
(Intercept)
-2058.077
(Intercept)
-2339.364
>
> #Models are different
> gam.m3$df.residual; gam.m3.orig$df.residual
[1] 2993
[1] 2986
推荐答案
gam
调用gam.fit
,而gam.fit
具有用于处理平滑器的特定代码.仅当model.frame的"terms"属性在其"specials"属性中正确指定了这些代码后,此代码才能正确运行.否则,将像其他任何功能一样处理平滑器,这显然会产生不同的结果.如果您想知道平滑器的处理方式有何不同,则需要详细研究gam.fit
的源代码.
gam
calls gam.fit
and gam.fit
has specific code for handling smoothers. This code only works correctly, if the model.frame's "terms" attribute has them correctly specified in its "specials" attribute. Otherwise, the smoothers are handled like any other function, which apparently gives a different result. If you want to know how exactly the smoothers are handled differently, you'll need to study the source code of gam.fit
in detail.
基本上,这表明您两次调用gam
的关键区别:
Basically, this shows the crucial difference between your two calls to gam
:
gam.smoothers()$slist
#[1] "s" "lo" "random"
attr(terms(wage ~ s(year,4) + s(age,5) + education,
specials = gam.smoothers()$slist), "specials")
#$s
#[1] 2 3
#
#$lo
#NULL
#
#$random
#NULL
attr(terms(wage ~ gam::s(year,4) + gam::s(age,5) + education,
specials = gam.smoothers()$slist), "specials")
#$s
#NULL
#
#$lo
#NULL
#
#$random
#NULL
为什么需要使用gam::s
?调用gam::gam
应该足以确保调用正确的平滑函数(通过名称空间查找):
Why do you need to use gam::s
? Calling gam::gam
should be sufficient to ensure that the correct smoother function is called (via the namespace lookup):
gam::gam(wage ~ s(year,4) + s(age,5) + education,data=Wage)
好,mgcv::s
实际上在搜索路径上掩盖了gam::s
.解决该问题的一种方法可以找到有.
OK, mgcv::s
actually masks gam::s
on the search path. One approach to solve that problem can be found there.
这篇关于gam软件包上的命名空间说明符不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!