0
votes

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?

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 mgcvBlacklivesmatter
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
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.Blacklivesmatter

1 Answers

0
votes

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)