# Ideas for binomial generalized linear mixed model with both phylogeny and random intercepts?

I've been struggling to find a description of an approach to a situation like mine or any R packages that seem capable of handling my needs. I have a large dataset (~80,000 rows) where my dependent variable is 1 or 0, describing the presence/absence of a feature of individual animals observed. There are two components to the non-independence of each observation: observations can come from the same location (a geographic random intercept, characterized by a label for which county the observation comes from) and the observations can come from the same species (nested within genus and family). This results in binomial data, since we often have multiple 1/0 observations for a given location and a given species. I need to model how independent (environmental) variables associated with location influence the probability of 1 vs 0 in the dependent variable, while accounting for the nonindependence of observations from the same location and observations from the same species - I expect species to differ in the proportion of 1 vs 0. Importantly, I cannot collapse observations from the same location into a single value for each location (e.g., 0.70 = proportion of 1's) because there is a critical independent variable (year) that is different for observations from the same location. In other words, I need to retain the individual observations so I can model the observation-level effect in addition to the location-level effect.

A simplified version of the data structure is below to help clarify the issue:

Y Location Species Environmental X Time
1 County A Species 1 7 1975
0 County A Species 1 7 1983
1 County B Species 1 4 1967
0 County C Species 1 5.8 1952
1 County A Species 2 7 1975
1 County D Species 2 9 1995
0 County D Species 2 9 1946
0 County D Species 2 9 1968
1 County E Species 3 2.7 1998

The independent variable Time should have effects on probability of 1 vs 0 for each observation, observations from the same location are non-independent, and observations for the same species are non-independent (and should be nested in a phylogenetic tree).

Every solution I can find doesn't seem to permit an analysis that contains all these features. I'd appreciate any advice on approaches I should look at that would best be suited for my situation.

This seems a problem to a phylogenetic linear mixed-effects model. Have you tried glmmTMB or bayesian alternatives, like brms? I'd build a binomial GLMM with repeated measures (=multiple observations within species), including country as random intercept and then include a phylogenetic VCV to account for phylogenetic autocorrelation (with your data set has > 30 species), if not I'd just include species as a crossed random effect, given that your data shows that you have the same species in different countries. For the bayesian alternative, take a look at this vignette. For glmmTMB, look here. Also, make sure you diagnose your model for zero inflation and correct for that, if needed. I'd use DHARMa in this case.