roffseteffectgammgcv

Error when running a fixed effect in a GAM with an offset in R with gratia::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?


Solution

  • 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:

    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)                                                           
    

    I get:

    enter image description here