7
$\begingroup$

I am looking for an R package to fit Generalized Additive Mixed Models (GAMMs) with ZINB distribution.

ZINB is not available in the mgcv package nor in the gamm4 package. I read here that it might be possible with the brms package, but I have never worked in a Bayesian framework.

Do you know any other frequentist package that would allow fitting a GAMM with ZINB?

$\endgroup$

1 Answer 1

5
$\begingroup$

This model is possible with the brms R package which is an interface to R:

A slight modification of one of the examples from https://cran.r-project.org/web/packages/brms/vignettes/brms_distreg.html shows essentially what is involved in setting up and fitting the model

## load package
library('brms')
## load data
zinb <- read.csv("http://stats.idre.ucla.edu/stat/data/fish.csv")

## fit a model with constant zero inflation
fit_zinb1 <- brm(count ~ s(persons, k = 4) + s(child, k = 4) + camper, 
                 data = zinb, family = zero_inflated_negbinomial(),
                 chains = 4, cores = 4,
                 control = list(adapt_delta = 0.999))
## plot the marginal effects
plot(marginal_effects(fit_zinb1))
## model summary
summary(fit_zinb1, WAIC = FALSE)

## fit a model where the zero-inflation is a constant plus a linear
##   effect of number of children
fit_zinb2 <- brm(bf(count ~ s(persons, k = 4) + s(child, k= 4) + camper,
                    zi ~ s(child, k = 4)), 
                 data = zinb, family = zero_inflated_negbinomial(),
                 chains = 4, cores = 4,
                 control = list(adapt_delta = 0.999))
## plot the marginal effects
plot(marginal_effects(fit_zinb2))
## model summary
summary(fit_zinb2, WAIC = FALSE) 

There's more to this that shown above (you need to do some amount of model checking, look at posterior predictive checks etc) but it gives you an idea of what's involved in fitting a GAM --- you'll need to look at the help for brms to find the syntax for random effects.

The other alternative is to use the gamlss package, which has the ZINBI family for zero-inflated NegBin and as with brms you can model all of the parameters of the distribution with linear predictors.

$\endgroup$
2
  • $\begingroup$ Is this still up-to-date as of 2023? $\endgroup$
    – spops
    Feb 7, 2023 at 0:11
  • 2
    $\begingroup$ I would probably add the bamlss package now, as I’m pretty sure that can do these things. But I’d have to check and I haven’t done a survey of the packages in a while. $\endgroup$ Feb 7, 2023 at 12:14

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.