glmmTMB() returns invalid model formula in ExtractVars in R

47 Views Asked by At

I'm fairly new to R and have been trying to find the solution to this on here, but none of the advice has helped my specific case. I hope I'm not missing something obvious.

I want to fit a generalized linear mixed model to get an idea which radius has the most effect on species richness in my study (250 m, 500 m etc). This is the code I used. all the variables in the formula have been correctly assigned beforehand (but the columns have the same name in the original data, just to be extra sure) and I had no issues before I got to this specific line of code:

mod_cover.radii <- glmmTMB(
 species.richness ~ forest.cover.250 + 
 forest.cover.500 + 
forest.cover.750 + forest.cover.1000 + 
forest.cover.1500, 
   data=bees, family=gaussian, na.action)

R tells me this line of code has invalid model formula in ExtractVars. I've tried different ways of writing the formula (using $, leaving out spaces), but I just can't figure out what's wrong here.

For some reason, I don't run into the same issue with this:

mod_complex <- glmmTMB(
 species.richness ~ forest.cover.250 + 
  forest.edge.250 + size + 
  flower.species.richness, 
  data = bees, family = gaussian)
mod_nested <- glmmTMB(
  species.richness ~ forest.cover.250 + 
  forest.edge.250, 
  data = bees, family = gaussian)
1

There are 1 best solutions below

0
Ben Bolker On

To be more specific: here is the result of args(glmmTMB):

function (formula, data = NULL, family = gaussian(), ziformula = ~0, 
    dispformula = ~1, weights = NULL, offset = NULL, contrasts = NULL, 
    na.action, se = TRUE, verbose = FALSE, doFit = TRUE, control = glmmTMBControl(), 
    REML = FALSE, start = NULL, map = NULL, sparseX = NULL) 

You can see that the next argument after family is ziformula, so the object na.action (which in this case is an S3 generic method) gets passed as ziformula.

I guess it would be possible to try to detect this kind of typo, but the general case would be complicated.