# Repeated measures, ZI negative binomial GLM in R?

So...I'm not entirely sure if this is possible but thought I'd post here to ask. Additionally, I've never used R before (except for screwing around for an hour or two in Swirl) so please bear with me. I'm trying to run a repeated measures, zero-inflated negative binomial GLM. Unfortunately, it's not possible to do in JMP, which is what I typically use. The catch is that some of my factors change for each time interval. Let me try to explain everything which will hopefully clear things up.

This analysis is looking at how the reproductive output (i.e. the number of offspring produced) of a fish species varies with sex ratio. To do this, we put a number of fish out on artificial reefs that we built. On each reef, we know the sex ratio and the average female/male size of what we put out there. The fish were out there for 3 weeks. We went back once a week to examine their reproductive output (we counted the number of eggs they produced). To break it down:

• DV: Reproductive output
• IV: Sex ratio
• IV: Block
• IV: Sex ratio * Block
• Covariate: Average female size
• Covariate: Average male size

So, for this analysis, each reef is it's own replicate that we sampled once a week, for 3 weeks. The problem is that the sex ratio (obviously) varied during the time that they were out there. So, I was wondering if it'd be possible for me to run a repeated measures, ZI negative binomial GLM while also changing the sex ratio, female, and male size for each time that we sampled reproductive output (week 1, week 2, and week 3).

Help!

Edit: I also have SPSS and SigmaPlot if it'd be easier/simpler to run my analyses in either of those programs.

There aren't very many R packages that can handle the combination of zero-inflation, random effects, and negative binomial responses. glmmTMB is one. Approximately:

library(glmmTMB)
glmmTMB(output ~ 1 + sex_ratio + avg_fem_size + avg_male_size +
(1+ sex_ratio|block),
family="nbinom2",
ziformula=~1)

• (1+sex_ratio|block) is the syntax for allowing the baseline output and the effect of sex ratio to vary by block. This might be too complicated (how many blocks do you have??), in which case you could drop back to (1|block), ignoring possible variation of sex ratio effect across blocks.
• Having sex ratio vary by week and block isn't a big deal.
• The ziformula (zero-inflation formula) above assumes that the zero-inflation proportion is fixed across all blocks, values of the covariates, etc.; you can relax that (by giving the ziformula argument some fixed covariates or random effects), again subject to having enough data to fit a more complicated model.
• glmmTMB also offers a family="nbinom1" choice, which specifies a negative binomial parameterized such that the variance is a linear (rather than quadratic) function of the mean, i.e. $V=\phi\mu$ rather than $V=\mu+\mu^2/k$. Might be worth trying.
• I believe the brms package can also handle this model, but that gets you into the Bayesian world, for better or worse ...
• Great, thanks so much for the informative reply. I only have 2 blocks so I'm hoping that will keep things fairly straight forward. Definitely not trying to do any Bayesian analyses; really overkill for what I'm trying to do in my opinion. As far as I know, my zero-inflation proportion is consistent across all blocks, covariates, etc. If it is NOT...how would I handle this? – Stephen Jul 10 '18 at 1:12
• (1) for 2 blocks you probably do not want to use random effects (e.g. see the end of this section: bbolker.github.io/mixedmodels-misc/… . (2) If you want the model to include variation in the zero-inflation proportion, use a non-trivial formula, e.g. ziformula=~1+sex_ratio – Ben Bolker Jul 10 '18 at 11:12