使用 gratia::draw() 在 R 中的偏移量的 GAM 中运行固定效果时出错

发布于 01-13 06:08 字数 2957 浏览 6 评论 0原文

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

    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函数中,所以我想知道为什么这个模型不适用于draw命令?如果我去掉偏移量,模型也会运行,但是此信息对于分析至关重要。我无法运行带有偏移量的固定效果是否有原因?

I have a data set that looks like this:

    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")

I am running a GAM that looks like this:

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

With a random effect for the name variable and a fixed effect for the Season variable.

I am able to get all the outputs using the summary() command for this model, however when I try to display the partial effect plots with the gratia::draw() command, I get the following error:

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!

The partial effect plots show up with the plot.gam function, so I am wondering why this model will not work for the draw command? Also the model will run if I take the offset out, however this information is crucial to the analysis. Is there a reason why I can't run a fixed effect with an offset?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹿!2025-01-20 06:08:21

我无法使用版本 0.7.1(在 GitHub 上,但即将提交给 CRAN)重现此错误,并且我认为在 0.7.0(当前的 CRAN 版本)和此版本之间没有与所报告的问题相关的任何内容发生变化修补版本。

使用 tmp 中的数据:

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

rgt; draw(gam1)                                                           

我得到:

在此处输入图像描述

I can't reproduce this error with version 0.7.1 (on GitHub, but about to be submitted to CRAN) and I don't think anything of relevance to the reported problem changed between 0.7.0 (the current CRAN version) and this patched version.

With your data in tmp:

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

rgt; draw(gam1)                                                           

I get:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文