Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Invalid grouping factor specification

I am trying to use glmer to model coral recruitment and get the error "Error: Invalid grouping factor specification, Site" when I run the model after re-scaling the variables. Help greatly appreciated

m1<-glmer(Tot~cs.Tile(Tile)+cs.Coral_T(Coral_T)+cs.Sponge(Sponge)+
cs.Turf(Turf)+cs.Acro(Acro)+cs.Por(Por)+cs.Poc(Poc)+
cs.Mer(Mer)+cs.Agar(Agar)+cs.Fav(Fav)+
cs.Den(Den)+cs.Sid(Sid)+cs.CCA(CCA)+cs.Soft(Soft)+
  (1|Site), 
family=poisson, data=data)

I have 16 variables and 368 obs:

str(data)
'data.frame':   368 obs. of  16 variables:
 $ Site   : Factor w/ 25 levels "Eight","Eighteen",..: 10 10 10 10 10 10 10 10 10 10 ...
 $ Tile   : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Tot    : int  28 24 17 13 29 19 6 13 14 4 ...
 $ Coral_T: num  32.6 32.6 32.6 32.6 32.6 ...
 $ Sponge : num  0.206 0.206 0.206 0.206 0.206 ...
 $ Turf   : num  32.3 32.3 32.3 32.3 32.3 ...
 $ Acro   : num  3.45 3.45 3.45 3.45 3.45 ...
 $ Por    : num  1.15 1.15 1.15 1.15 1.15 ...
 $ Poc    : num  0 0 0 0 0 0 0 0 0 0 ...
 $ Mer    : num  0.175 0.175 0.175 0.175 0.175 0.175 0.175 0.175 0.175 0.175 ...
 $ Agar   : num  24.2 24.2 24.2 24.2 24.2 ...
 $ Fav    : num  1.02 1.02 1.02 1.02 1.02 ...
 $ Den    : num  1.18 1.18 1.18 1.18 1.18 ...
 $ Sid    : int  0 0 0 0 0 0 0 0 0 0 ...
 $ CCA    : num  0.07 0.07 0.07 0.07 0.07 0.07 0.07 0.07 0.07 0.07 ...
 $ Soft   : num  0 0 0 0 0 0 0 0 0 0 ...
like image 476
DThomson Avatar asked Dec 14 '16 03:12

DThomson


1 Answers

I encountered the same error in a call to update.merMod, also after re-scaling variables. After digging down through the traceback() stack and rummaging around with what I found there using both the scaled and un-scaled datasets, I found that the issue arose in the creation of the model frame, and occurred because my scaling and centering algorithm failed to account for NA values in the original variables. I originally performed the centering/scaling as follows:

csData <- data %>% mutate(Var1 = (Var1 - mean(Var1) / sd(Var1),
                          Var2 = (Var2 - mean(Var2) / sd(Var2))

There were a few NA values in one of those variables, and scaling and centering it in this way resulted in an empty (all NA) vector in csData. This subsequently (and silently) led to the return of an empty frame from model.frame(). Though the empty frame was what tripped the error, it (the error) came about because of my (mis)handling the NA values in my variables during the re-scaling. Setting na.rm=TRUE for the calls to sd() and mean() solved the issue for me:

csData <- data %>% mutate(Var1 = (Var1 - mean(Var1, na.rm=TRUE) / sd(Var1, na.rm=TRUE),
                          Var2 = (Var2 - mean(Var2, na.rm=TRUE) / sd(Var2, na.rm=TRUE))
like image 154
Ian Hoppe Avatar answered Nov 15 '22 06:11

Ian Hoppe