# Understanding the mixed_model () function in R [closed]

Even though I already have some experience working with R I would still consider myself a beginner. For my current research project, I need to run a zero-inflated negative binomial regression with fixed effects. The mixed_model() function seems to be the only way to do this in R. However, I find the manual to the function very challenging and thus I am looking for some help and explanations here in the community.

I want to run a regression with violent_events as the dependent and project_sum as the dependent variable. Additionally, I want to control for gdp, population_size and education. My units of analysis are different districts, which can be identified via the district_id variable. For each district, I have data for the years 2004 to 2010.

My initial attempt looked like this:

gm1 <- mixed_model(violent_events ~ sproject_sum + gdp + population_size + education,
random = year | district_id, data = DF,
family = zi.negative.binomial(), zi_fixed = ~ district_id)


Of course, the code is not working. I would be grateful for suggestions and particularly explanations that let me understand the *mixed_model()+ function better.

• You can find some explanation about the GLMMadaptive package here. However, as such, this question is off-topic. Is there a specific statistical problem you are having? If so please update your post. Jan 25, 2023 at 10:19
• Thanks for the suggestion. I already had a look at this, but find it rather hard to understand. That's why I asked here for a more beginner friendly explanation.
– KC15
Jan 25, 2023 at 11:30

The zero-inflated negative binomial model accounts for "extra" zeros in over-dispersed count outcomes. In particular, it has two components, a negative binomial model and a logistic regression for the extra zeros. The syntax you used above translates to the following specific model,

$$\left \{ \begin{array}{rcl} \log \{E(\texttt{violent_events}_{ij}) \} & = & \beta_0 + \beta_1 \texttt{sproject_sum}_{ij} + \beta_2 \texttt{gdp}_{ij} + \beta_3 \texttt{population_size}_{ij}\\&& + \beta_4 \texttt{education}_{ij} + b_{i0} + b_{i1} \texttt{time}_{ij}\\&&\\ \Pr\{\mbox{extra } \texttt{violent_events}_{ij} = 0\} & = & \gamma_0 + \gamma_1 \texttt{district_id}_{ij} \end{array} \right.$$

Some notes:

• The term $$E(\texttt{violent_events}_{ij})$$ denotes the average of the violent_events variable. When violent_events is zero, then this may be a zero from the negative binomial model or an extra zero with a probability specified by the logistic regression model.
• In the above specification, I do not know if some of the covariates you have are factors with multiple levels (e.g., district_id). In this case, their specification expands to multiple coefficients using treatment contrasts.
• In the output, you get the estimated fixed-effects coefficients $$\beta_0, \ldots, \beta_4$$, and $$\gamma_0, \gamma_1$$ reported separately.
• In the syntax, you need to include a ~ before the time variable in the specification of the random argument, i.e., random = ~ time | district_id
• Given that district_id is a grouping variable, you would need to put a random effect for it in the logistic regression part, i.e., zi_random = ~ 1 | district_id, and most probably you do not want to include it as a fixed effect, i.e., zi_fixed = ~ 1. If you do that, then in the equation above, you will have an extra random effect $$u_i$$ in the logistic regression part, and the random effects $$b_{0i}$$, $$b_{1i}$$, and $$u_{i}$$ will be assumed to be correlated.
• Thank you a lot! I received: Error in mixed_model... : you have defined a family with an extra zero-part;at least argument 'zi_fixed' needs to be defined, and potentially also argument 'zi_random'. My model looked like this: gm1 <- mixed_model(violent_events ~ sproject_sum + gdp + population_size + education, random = ~ year | distict_id, data = TC, family = zi.negative.binomial(), zi_random = ~ 1 | district_id, zi_fixed = NULL)
– KC15
Jan 25, 2023 at 17:05
• If I add zi_random = ~ 1 i get Error in chol.default(X[[i]], ...) : the leading minor of order 1 is not positive definite
– KC15
Jan 25, 2023 at 17:46
• You may need to simplify the random effects structure for the negative binomial part, e.g., random = ~ 1 | district_id. And, you also need zi_fixed = ~ 1. Jan 25, 2023 at 19:08
• Then I receive Error in mixed_fit(y, X, Z, X_zi, Z_zi, id, offset, offset_zi, family, : A large coefficient value has been detected during the optimization. Please re-scale you covariates and/or try setting the control argument 'iter_EM = 0'. Alternatively, this may due to a divergence of the optimization algorithm, indicating that an overly complex model is fitted to the data. For example, this could be caused when including random-effects terms (e.g., in the zero-inflated part) that you do not need. Otherwise, adjust the 'max_coef_value' control argument.
– KC15
Jan 26, 2023 at 8:32