# Using gamm4 on zero-inflated count data with Tweedie or zero-inflated Poisson distributions

I'm working with a dataset with a large number of zero-counts on the response variable. This dataset consists of qualitatively coded interviews in a number of important categories, but many of the interviews don't contain information about the categories (hence, the zero-inflation). Here is a general description of some of the variables:

time: number of days since the start of data collection (numerical) party: political party of the interviewee (factor) network: news network the interview aired on (factor) code: count data of the number of times the code is mentioned in a given interview (numerical) - this is the zero-inflated response variable name: the individual politician who gave the interview (factor, used in the random effect nested within party)

Here is a histogram of one of the codes I'm interested in modeling.

I've been trying to model this data using a Tweedie distribution (which I've been told is more robust than Poisson or negative binomial distributions) or a zero-inflated Poisson distribution, but both are resulting in error messages in my code.

Here is some of my code:

real.mod.p <- gamm4(code ~ party + s(time, by=party, k=20),
random =~ (1 | name/party),
data=dat,
family=tw)


I have no problems running this model with the gaussian family or the Poisson family, but when I switch the family to tw I get the following error message:

Error in as(value, fieldClass, strict = FALSE) : internal problem in as(): “extended.family” is(object, "family") is TRUE, but the metadata asserts that the 'is' relation is FALSE

Similarly, when I switch the family to be a zero-inflated Poisson model using the following code:

real.mod.p <- gamm4(code ~ party + s(time, by=party, k=20),
random =~ (1 | name/party),
data=dat,
family=ziplss)


I get this error message:

You can't use these extended families outside of gam() or bam() from the mgcv package.
With gamm4 you're stuck with the families that lmer() and glmer() from the lme4 package supports.
Options would be to use the gamlss package for ZIP models as I don't think it has a Tweedie family, or use the glmmTMB package which has lots of options, including tw() for a Tweedie family, however I don't believe it understands smooths so you'd need to convert your model into a form that can be fitted as a mixed effects model.
Another option, which is likely to be inefficient but which you can use to get started with immediately is to use the "re" basis spline with gam() or bam() and create a factor or name nested in party to use as the random effect, or even just s(name, party, bs = 're') might do it, assuming both are factors, but do the help for this basis to make sure I'm interpreting what you intend for the random effect.