当 运行 GAM 中的固定效应在 R 中带有 gratia::draw() 的偏移时出错

Error when running a fixed effect in a GAM with an offset in R with gratia::draw()

我有一个如下所示的数据集:

    structure(list(n = c(236896L, 73258L, 75570L, 5684L, 10242L, 
2037L, 74194L, 41764L, 288115L, 6728L, 18964L, 5395L, 23192L, 
12575L, 39591L, 12566L, 44458L, 126957L, 47316L, 152175L, 92913L, 
81229L, 29622L, 1708L, 8526L, 52117L, 95385L, 22480L, 30521L, 
51660L, 74320L, 273107L, 58L, 59686L, 77454L, 51471L, 66610L, 
232321L, 53435L, 45270L), name = structure(c(9L, 9L, 6L, 5L, 
2L, 5L, 6L, 9L, 6L, 4L, 4L, 4L, 4L, 2L, 9L, 2L, 6L, 1L, 4L, 6L, 
4L, 9L, 2L, 5L, 3L, 4L, 6L, 2L, 9L, 2L, 4L, 4L, 7L, 9L, 1L, 6L, 
6L, 6L, 8L, 2L), .Label = c("Ami", "Cho", "Fal", "For", "Ric", 
"Sam", "Taw", "Tex", "Tol"), class = "factor"), change2 = c(0.0753607803884176, 
-0.08058465598786, -0.00410425493512865, -0.0220964428266722, 
0.0629320532004209, -0.0797306134519322, 0.0660481799732004, 
-0.0572995403797303, -0.00713582946272, 0.00756646981276647, 
0.032732914683994, -0.00632056690250293, 0.050358229187504, 0.0265162711945312, 
0.0218803226963826, -0.0508818612242459, 0.00485925918649957, 
0.0315158006542641, -0.0315622434590242, -0.0602515470219345, 
-0.0409479919129347, 0.111224942380013, 0.00704490808823113, 
0.0236731452544392, -0.0811686305416274, -0.0274692750452023, 
0.00160881330548216, -0.0211269729894635, -0.0377625466699325, 
-0.0311273993307701, -0.0118001904995042, 0.0023179680499073, 
0.0263453251509878, 0.0767020512037913, -0.0113771665605732, 
-0.0428469659333539, 0.0714746847470087, 0.10720066191237, 0.0153144105362596, 
-0.109538998188302), Season = structure(c(2L, 1L, 4L, 3L, 3L, 
1L, 4L, 3L, 2L, 4L, 4L, 4L, 1L, 4L, 3L, 1L, 3L, 4L, 1L, 1L, 3L, 
2L, 2L, 2L, 3L, 3L, 3L, 1L, 3L, 4L, 1L, 2L, 3L, 2L, 2L, 3L, 4L, 
2L, 3L, 2L), .Label = c("fall", "spring", "summer", "winter"), class = "factor"), 
    off = c(230915, 57957, 85583, 10526, 35316.6, 4851, 87287, 
    48226, 198700, 42050.6, 46252.8, 29974, 56566, 20959, 43175, 
    10385, 56997, 208126, 100672, 80516, 244507, 128730, 38470, 
    5177, 22435.6, 121202, 114234, 26140, 24693, 53812.6, 124281, 
    666114, 583, 76915, 140824.7, 91912, 78828, 219171, 95419, 
    33783.9)), row.names = c(NA, -40L), class = "data.frame")

我正在 运行创建一个如下所示的 GAM:

gam1<-gam(n~Season+s(change2, by=Season, k=5)+
                       s(name, bs="re")+
                       offset(log(off)),
                       data=data,family=nb,method="REML")

name 变量具有随机效应,Season 变量具有固定效应。

我能够使用此模型的 summary() 命令获得所有输出,但是当我尝试使用 gratia::draw() 命令显示部分效果图时,我收到以下错误:

library(gratia)
draw(gam1)
    Error in eval(predvars, data, env) : object 'off' not found
In addition: Warning message:
In predict.gam(object, newdata = pred_data, type = "terms", terms = term,  :
  not all required variables have been supplied in  newdata!

部分效果图用plot.gam函数显示,所以我想知道为什么这个模型不适用于绘制命令?如果我去掉偏移量,模型也会 运行,但是此信息对分析至关重要。为什么我不能 运行 具有偏移的固定效果?

我无法在 0.7.1 版(在 GitHub,但即将提交给 CRAN)中重现此错误,而且我认为与报告的问题相关的任何内容在 0.7 版之间都没有变化。 0(当前 CRAN 版本)和这个补丁版本。

你的数据在 tmp:

r$> gam1 <- gam(n ~ Season + s(change2, by = Season, k = 5)+ 
                  s(name, bs = "re") + 
                  offset(log(off)), 
                data = tmp, family = nb, method = "REML")             

r$> draw(gam1)                                                           

我得到: