簡體   English   中英

為什么在“gam(y~mgcv :: s ...)”中使用“mgcv :: s”會導致錯誤?

[英]Why does using “mgcv::s” in “gam(y ~ mgcv::s…)” result in an error?

我想清楚並mgcv::gam使用::符號來擬合mgcv::gam 當在mgcv::s的模型調用中使用符號時,我偶然發現了一件事。 具有可重現的示例/錯誤的代碼如下所示。

原因可能是因為我在模型公式中使用了這種表示法,但我無法弄清楚為什么這不起作用/不允許。 這可能是關於語法的非常具體的東西(我猜可能不是特定於mgcv),但也許有人可以幫助我理解這個和我對R的理解。先謝謝你。

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源代碼的第4行揭示了這個問題:

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"來解決這個extra.special

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

只是在高級例程中這不是用戶可控的:

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的觀點。 挖掘內部發生的事情是一個很好的練習,但過度反應將此視為一個錯誤並修復它。


更多見解:

mgcv中的ste等與stats::polysplines::bs邏輯不同。

  • 例如,當您執行X <- splines::bs(x, df = 10, degree = 3) ,它會計算 x並直接創建設計矩陣X
  • 當你做s(x, bs = 'cr', k = 10) ,不進行評估; 它被解析了

mgcv平滑構造需要幾個階段:

  1. mgcv::interpret.gam解析/解釋,生成更平滑的配置文件;
  2. mgcv::smooth.construct初始構建,建立基礎/設計矩陣和懲罰矩陣(主要在C級完成);
  3. mgcv::smoothCon構建的二級構造,它拾取“by”變量(例如,復制平滑因子“by”),線性函數項,零空間罰分(如果使用select = TRUE ),懲罰重新縮放,居中約束,等等;
  4. mgcv:::gam.setup最終集成,它將所有平滑器組合在一起,返回模型矩陣等。

所以,這是一個復雜得多的過程。

這看起來像是mgcv問題。 例如, lm()函數接受poly()stats::poly()並給出相同的結果(除了事物的名稱):

> x <- 1:100
> y <- rnorm(100)
> lm(y ~ poly(x, 3))

Call:
lm(formula = y ~ poly(x, 3))

Coefficients:
(Intercept)  poly(x, 3)1  poly(x, 3)2  poly(x, 3)3  
    0.07074      0.13631     -1.52845     -0.93285  

> lm(y ~ stats::poly(x, 3))

Call:
lm(formula = y ~ stats::poly(x, 3))

Coefficients:
       (Intercept)  stats::poly(x, 3)1  stats::poly(x, 3)2  stats::poly(x, 3)3  
           0.07074             0.13631            -1.52845            -0.93285  

它也適用於splines::bs函數,因此這不是特定於poly()

您應該聯系mgcv維護者並在該包中指出此錯誤。 我猜它是專門針對s而不是像mgcv::s這樣的表達式來評估相同的東西。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM