0

I am trying to execute nested random effects in R with the mgcv::gamm function. Specifically, this function is supposedly an extension of ANCOVA to GAMM, resulting in a GAMMCOVA.

The reasoning for random effects: the entire dataset is composed of multiple previously-separate datasets. Further a single siteID is temporally replicated anywhere from 3-40 times. Even more, some siteID's are replicated in different datasetIDs.

gamm(response ~ 
s(predictor1,bs="cr") + 
s(predictor2,bs="cr") + 
bs(covariate,degree=3,df=4), # covariate that makes this a gammcova
random=list(datasetID=~1, siteID=~1), # this works but is not nested
select=T, method="REML",
family=quasibinomial(link="logit"))

I also tried this with random=~(1|datasetID/siteID) but this returned an error

Error in mgcv::gamm(ci_est ~ s(predictor1, bs = "cr") + s(predictor2, bs = "cr") +  : 
  gamm() can only handle random effects defined as named lists

I know that s(siteID,datasetID,bs="re") is too many coefficients for the data sample size. But, I don't think this is specifying a nested random effect anyway, correct?

Error in lme.formula(fixed = fixed, random = random, data = data, correlation = correlation,  : 
  fewer observations than random effects in all level 6 groups

So, is there a way to specify nested random effects in mgcv::gamm?

Razneesh
  • 1,147
  • 3
  • 13
  • 29
ecology
  • 606
  • 3
  • 9
  • 29
  • 1
    Have you seen this? https://stackoverflow.com/questions/59215853/syntax-for-nested-random-effects-using-gamm-or-lme-in-r – Roman Luštrik Mar 19 '20 at 08:26
  • 1
    I have however they say in their own comment that "So this does not answer the question for mgcv but might help anyone who does not mind shifting to gamm4". I definitely want to stick with mgcv – ecology Mar 19 '20 at 14:05
  • 1
    The *gamm4* package is written by the author of *mgcv* and uses *mgcv* functions to set up GAMMs, but the fitting is done via the *lme4* package, which is much better for fitting non Gaussian GAMMs. Is there some reason not stated here why you want to stick with fitting GAMMs using PQL via `glmmPQL()` and `lme()`? – Gavin Simpson Mar 20 '20 at 13:21
  • 1
    Not a very important one, but I have been playing around with the caret::varimp function and it does not work with gamm I find. – ecology Mar 21 '20 at 23:24

1 Answers1

0

I found this https://thepsychologist.bps.org.uk/volume-21/edition-3/methods

Supposedly, this is the code to include a covariate in a gam.

s(x,m=3,k=6,bs="ps",fx=T)
ecology
  • 606
  • 3
  • 9
  • 29