本文介绍了为什么使用"mgcv :: s"?在"gam(y〜mgcv :: s ...)"中导致错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想清楚一点,并在各行中使用::表示法来适合mgcv::gam.在模型调用mgcv::s中使用表示法时,我偶然发现了一件事情.带有可复制示例/错误的代码如下所示.

I wanted to be clear and use the :: notation in the lines for fitting an mgcv::gam. I stumbled over one thing when using the notation within the model call for mgcv::s. The code with a reproducible example / error is shown below.

原因可能是因为我在模型公式中使用了这种表示法,但是我无法弄清楚为什么它不起作用/不允许这样做.这可能是有关语法的非常具体的内容(我想可能不是特定于mgcv的内容),但是也许有人可以帮助我理解这一点以及我对R的理解.

The reason is probably because I am using this notation within the model formula, but I could not figure out why this does not work / is not allowed. This is probably something quite specific concerning syntax (probably not mgcv specific, I guess), but maybe somebody can help me in understanding this and my understanding of R. Thank you in advance.

library(mgcv)
dat <- data.frame(x = 1:10, y = 101:110)
# this results in an error: invalid type (list)...
mgcv::gam(y ~ mgcv::s(x, bs = "cs", k = -1), data = dat)
# after removing the mgcv:: in front of s everything works fine
mgcv::gam(y ~ s(x, bs = "cs", k = -1), data = dat)

# outside of the model call, both calls return the desired function
class(s)
# [1] "function"
class(mgcv::s)
# [1] "function"

推荐答案

说明

library(mgcv)
#Loading required package: nlme
#This is mgcv 1.8-24. For overview type 'help("mgcv-package")'.

f1 <- ~ s(x, bs = 'cr', k = -1)
f2 <- ~ mgcv::s(x, bs = 'cr', k = -1)

OK <- mgcv:::interpret.gam0(f1)$smooth.spec
FAIL <- mgcv:::interpret.gam0(f2)$smooth.spec

str(OK)
# $ :List of 10
#  ..$ term   : chr "x"
#  ..$ bs.dim : num -1
#  ..$ fixed  : logi FALSE
#  ..$ dim    : int 1
#  ..$ p.order: logi NA
#  ..$ by     : chr "NA"
#  ..$ label  : chr "s(x)"
#  ..$ xt     : NULL
#  ..$ id     : NULL
#  ..$ sp     : NULL
#  ..- attr(*, "class")= chr "cr.smooth.spec"

str(FAIL)
# list()

interpret.gam0源代码的第四行揭示了该问题:

The 4th line of the source code of interpret.gam0 reveals the issue:

head(mgcv:::interpret.gam0)

1 function (gf, textra = NULL, extra.special = NULL)              
2 {                                                               
3     p.env <- environment(gf)                                    
4     tf <- terms.formula(gf, specials = c("s", "te", "ti", "t2", 
5         extra.special))                                         
6     terms <- attr(tf, "term.labels") 

由于不匹配"mgcv::s",因此出现了问题.但是mgcv确实为您提供了解决此问题的空间,方法是通过参数extra.special传递"mgcv::s":

Since "mgcv::s" is not to be matched, you get the problem. But mgcv does allow you the room to work around this, by passing "mgcv::s" via argument extra.special:

FIX <- mgcv:::interpret.gam0(f, extra.special = "mgcv::s")$smooth.spec
all.equal(FIX, OK)
# [1] TRUE

在高级例程中这不是用户可控制的:

It is just that this is not user-controllable at high-level routine:

head(mgcv::gam, n = 10)

#1  function (formula, family = gaussian(), data = list(), weights = NULL, 
#2      subset = NULL, na.action, offset = NULL, method = "GCV.Cp",        
#3      optimizer = c("outer", "newton"), control = list(), scale = 0,     
#4      select = FALSE, knots = NULL, sp = NULL, min.sp = NULL, H = NULL,  
#5      gamma = 1, fit = TRUE, paraPen = NULL, G = NULL, in.out = NULL,    
#6      drop.unused.levels = TRUE, drop.intercept = NULL, ...)             
#7  {                                                                      
#8      control <- do.call("gam.control", control)                         
#9      if (is.null(G)) {                                                  
#10         gp <- interpret.gam(formula)  ## <- default to extra.special = NULL

我同意本·博克(Ben Bolker)的观点.找出内部发生的事情是一个很好的练习,但是将其视为错误并修复它是一种过度反应.

I agree with Ben Bolker. It is a good exercise to dig out what happens inside, but is an over-reaction to consider this as a bug and fix it.

更多见解:

s,te等与stats::poly和splines::bs的逻辑不同.

s, te, etc. in mgcv does not work in the same logic with stats::poly and splines::bs.

  • 例如,当您执行X <- splines::bs(x, df = 10, degree = 3)时,它将评估 x并直接创建设计矩阵X.
  • 执行s(x, bs = 'cr', k = 10)时,不会进行评估;它被解析.
  • When you do for example, X <- splines::bs(x, df = 10, degree = 3), it evaluates x and create a design matrix X directly.
  • When you do s(x, bs = 'cr', k = 10), no evaluation is made; it is parsed.

mgcv中的平滑构建过程分为几个阶段:

Smooth construction in mgcv takes several stages:

  1. 由mgcv::interpret.gam进行解析/解释,从而生成更平滑的轮廓;
  2. 由mgcv::smooth.construct进行的初始构造,它建立了基础/设计矩阵和惩罚矩阵(主要在C级完成);
  3. mgcv::smoothCon的二次构造,它拾取"by"变量(例如,将因子"by"平滑复制),线性函数项,空空间罚分(如果使用select = TRUE),罚分重定比例,居中约束等;
  4. 由mgcv:::gam.setup进行的最终积分,它将所有平滑器组合在一起,返回模型矩阵等.
  1. parsing / interpretation by mgcv::interpret.gam, which generates a profile for a smoother;
  2. initial construction by mgcv::smooth.construct, which sets up basis / design matrix and penalty matrix (mostly done at C-level);
  3. secondary construction by mgcv::smoothCon, which picks up "by" variable (duplicating smooth for factor "by", for example), linear functional terms, null space penalty (if you use select = TRUE), penalty rescaling, centering constraint, etc;
  4. final integration by mgcv:::gam.setup, which combines all smoothers together, returning a model matrix, etc.

所以,这是一个复杂得多的过程.

So, it is a far more complicated process.

这篇关于为什么使用"mgcv :: s"?在"gam(y〜mgcv :: s ...)"中导致错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:44