Good day, I have a doubt about emmeans, Im doing research in two parks, in each park I have a county and in each county, I have three habitats and on each habitat, I have collected beetles (Count response variable).... this is the sampling design for one park (sampling design for CS, the same for CSI):



I've made a Negative Binomial GLM and I'm trying to make pairwise comparisons between each factor for each variable. As you all can see this sampling design have the county variable nested within Park, I'm trying to do the comparison between parks only, it gives me that they're pretty different, I think that it has the effect of the nested variable included.

ulm<-glm.nb(Abu~Park+county %in% Park+Hab,data=data)
marginal = emmeans(ulm, ~ Park) ## if I put the nesting=NULL it gives me NA
plot(marginal, comparisons = TRUE)

my question is....how can I examine the differences between park without the county effect? with the same model of course. (I know that make separate models is kind of p-hacking thing)


1 Answer 1


First of all, park is nested in county, not the other way around. Look at a design book. It will tell you that A is nested in B if knowing which A also tells you which B. Analogous to your situation, knowing it's Yosemite tells you it's in California.

I suggest just using a model r.h.s. of ~ county + Park + Hab, or perhaps ~(county + Park)*Hab. That will produce fewer dependencies (NA regression coefficients).

It appears that emmeans correctly detected the nesting structure in spite of the way you specified the model. And is designed to show nesting factors when you specify a nested factor.

  • $\begingroup$ And BTW if you really want to compare parks across counties, either leave county out of the model, or specify that you want means for county*park. $\endgroup$
    – Russ Lenth
    Sep 1, 2021 at 1:20
  • $\begingroup$ OK, I just looked more closely at the data structure, and it does look like county is nested in park. That just doesn't make sense to me, so I guess these are not the kinds of parks or counties I'm thinking of. But then, why do you ask about getting rid of the effect of county? The EMMs will average over counties in that case. The model includes county as a predictor, so that implies it estimates county effects along with the other effects. Perhaps if you show the output you get (including annotations at the bottom), and explain what part of it bothers you? $\endgroup$
    – Russ Lenth
    Sep 1, 2021 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.