Search code examples
rgammgcvrandom-effectsancova

R: programming nested random effect in gamm (gamcova)


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?


Solution

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