brober
brober

Reputation: 121

Adding two random effects within a factor: GAM

From this data:

UQdata  MudUQ   Estuary   Site
7.00    10.9    NoriPau   A
6.00    13.9    NoriPau   A
5.00    10.3    NoriPau   B
4.00    7.9     Porirua   A
4.00    8.3     Porirua   A
4.00    8.7     Porirua   A
4.00    10.9    NoriPau   B
3.00    9.8     Porirua   B
3.00    9.8     Porirua   B
3.00    11.5    Porirua   B

I'm fitting the below GAM model using the mgcv package:

aa2.estuary <- gam(UQdata~s(MudUQ, bs="ps", k=5) + s(Estuary, bs="re"), 
                   family=Gamma(link=log),data=Antho)

Problem: I want to add Estuary and Site within Estuary as two random effects (i.e. s(Estuary ~ Site + Estuary, bs="re")), but when I try this it throws this error:

aa2.estuary <- gam(UQdata ~ s(MudUQ,bs="ps", k=5) + 
                   s(Estuary~Estuary+Site, bs="re"),
                   family=Gamma(link=log),data=Antho)

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels
In addition: Warning message:
In s(Estuary ~ Estuary + Site, bs = "re") :
  number of items to replace is not a multiple of replacement length

Any help here will be much appreciated.


mnel, I tried your suggest string:

> aa1.estuary<-gam(UQdata~s(MudUQ,bs="ps", k=5) + s(Estuary, bs="re") + s(Site, Estuary, bs = 're'),family=binomial, gamma=1,data=Antho)
Error in while (mean(ldxx/(ldxx + ldss)) < 0.4) { : 
  missing value where TRUE/FALSE needed

Any ideas?

Upvotes: 2

Views: 3533

Answers (1)

brober
brober

Reputation: 121

By going back to the raw data and renaming Sites in association with each estuary (see below):

UQdata  MudUQ   Estuary   Site
7.00    10.9    NoriPau   Nori1
6.00    13.9    NoriPau   Nori1
5.00    10.3    NoriPau   Nori2
4.00    7.9     Porirua   Pori1
4.00    8.3     Porirua   Pori1
4.00    8.7     Porirua   Pori1
4.00    10.9    NoriPau   Nori2
3.00    9.8     Porirua   Pori2
3.00    9.8     Porirua   Pori2
3.00    11.5    Porirua   Pori2

And including Site as another random effect:

aa2.estuary <- gam(UQdata ~ s(MudUQ,bs="ps", k=5) + s(Estuary, bs="re") + s(Site, bs="re"),family=Gamma(link=log),data=Antho)

And not worrying about which is nested within which. This takes care of both the within Site correlations, and the within Estuary correlations.

Upvotes: 4

Related Questions