![](/img/trans.png)
[英]Why does using cluster= argument in mgcv::bam() result in an error?
[英]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
中的s
, te
等與stats::poly
和splines::bs
邏輯不同。
X <- splines::bs(x, df = 10, degree = 3)
,它會計算 x
並直接創建設計矩陣X
s(x, bs = 'cr', k = 10)
,不進行評估; 它被解析了 。 mgcv
平滑構造需要幾個階段:
mgcv::interpret.gam
解析/解釋,生成更平滑的配置文件; mgcv::smooth.construct
初始構建,建立基礎/設計矩陣和懲罰矩陣(主要在C級完成); mgcv::smoothCon
構建的二級構造,它拾取“by”變量(例如,復制平滑因子“by”),線性函數項,零空間罰分(如果使用select = TRUE
),懲罰重新縮放,居中約束,等等; 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.