gammgcvbrms

mgcv::gam, Error in names(dat) <- object$term : attribut 'names' [2] as to be same length as vector [1]


I want to run a hieratchical GAM in the mgcv package using the gam function. I used the same form of model in brms without problem and I will eventually re-run the same model in brms, but the deadline for an abstract submission in Sunday so I want to try the model in mgcv to have quicker results.

My formula:

f = MDS1 ~ 1 + exposed + s(YEAR,bs = "tp")+ s(LEVEL, bs = "tp") +
t2(YEAR, SITE, bs = c("tp","re")) + s(INTERTIDAL_TRANSECT, bs = "re",
m = 1)

My data:

Classes ‘data.table’ and 'data.frame':  3992 obs. of  9 variables:
 $ unique_id          : chr  "Babb's Cove-1-0-1988" "Babb's Cove-1-0-1989" "Babb's Cove-1-0-1990" "Babb's Cove-1-0-1992" ...
 $ MDS1               : num  -0.607 -0.607 -0.607 -0.607 -0.607 ...
 $ MDS2               : num  0.19 0.19 0.19 0.19 0.19 ...
 $ MDS3               : num  0.36 0.36 0.36 0.36 0.36 ...
 $ SITE               : chr  "Babb's Cove" "Babb's Cove" "Babb's Cove" "Babb's Cove" ...
 $ INTERTIDAL_TRANSECT: Factor w/ 21 levels "1","2","5","7",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ LEVEL              : num  0 0 0 0 0 0 0 1 1 1 ...
 $ YEAR               : num  1988 1989 1990 1992 1994 ...
 $ exposed            : Factor w/ 2 levels "1","2": 2 2 2 2 2 2 2 2 2 2 ...
 - attr(*, ".internal.selfref")=<externalptr> 
 - attr(*, "sorted")= chr "unique_id"

I have 2 questions: a)

When I try to fit the model with fit_count <- gam(f, data = count_merge, method = "REML", family = gaussian()) I get :

Error in names(dat) <- object$term : 
  attribut 'names' [2] doit être de même longueur que le vecteur [1]

I think it's something with the t2() argument of the formula.

b) I usually run GAM with brms and my formula for that model was :

MDS1 ~ 1 + exposed + s(YEAR,bs = "tp")+ s(LEVEL, bs = "tp") + t2(YEAR, SITE, bs = c("tp","re"), full = T) +(1|r|INTERTIDAL_TRANSECT),
          family = gaussian()

Is my way to adapt the formula to mgcv::gam OK?


Solution

  • Q a)

    Your SITE vector is a character vector and it is required to be a factor.

    Q b)

    That looks OK, but you don't need the m = 1 in the s(INTERTIDAL_TRANSECT, bs = "re") term.

    You should also use the full = TRUE option on the t2() term if you want the parameterisation to be the same between your {brms} call the {mgcv} one.