I am trying to develop a negative binomial model where the dependent variable is crash count, and the independent variables are traffic count and roadway length. Currently, with the below code, I get only one value of overdispersion parameter. But I want to get the overdispersion parameter that varies as a function of roadway length. Can you please tell me what should I change in the code? Thank you very much!

    glm.nb(Total_crashes~LN(traffic_count) + 
    road_length, data=mydata)
  • $\begingroup$ Something tells me that this might go beyond what the package provides and you might need to code this by yourself... But I might be wrong. $\endgroup$
    – Daeyoung
    Mar 16, 2022 at 21:04
  • $\begingroup$ Well, I was wondering, if I need to vary the overdispersion parameter relative to the segment length, then is it as simple as dividing the model overdispersion parameter by the segment length? But it cannot be that easy, can it? I am really confused! $\endgroup$
    – Meg
    Mar 16, 2022 at 21:11
  • $\begingroup$ The GAMLSS package for R should allow this. $\endgroup$ Mar 16, 2022 at 21:56

1 Answer 1


I don't think you will be able to do that with glm.nb() but it is possible to do this is a Bayesian framework. I think there are multiple options on how to do this, but the most straightforward for someone without experience in Bayesian modelling will be to use the brms package, which allows the user to fit models using the same model syntax as R packages like lme4.

Specifically, the model formula in the brms package would be the following, assuming you want to model Total_crashes by both traffic_count and road_length and just the overdispersion parameter by road_length:

Crash_count <- brm(bf(Total_crashes~LN(traffic_count) + road_length,
                      shape ~ road_length),
                   family = negbinomial())

See this post for an example of someone predicting the shape/phi parameter, which is the overdispersion, using variables in a model:


That post, in combination with the vignette I link below about fitting distributional models in brms, should give you a good start on fitting the model you'd like to fit


EDIT: As others have pointed out in the comments, there are other packages that allow for this to be modelled.

  • 1
    $\begingroup$ The VGAM::negbinomial function will also fit classical versions of the models -- the point of VGAM is to allow linear predictors on multiple parameters of a base distribution. $\endgroup$ Mar 17, 2022 at 3:22
  • 1
    $\begingroup$ Hi sjp, that was really helpful. Thanks for your response! $\endgroup$
    – Meg
    Apr 28, 2022 at 3:12
  • $\begingroup$ Hi Meg, I'm glad that you found it useful! $\endgroup$
    – sjp
    Apr 29, 2022 at 5:32

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.