I am trying to build GAMs to understand the detectability of two cetacean species using acoustic and visual data. For now, I am running three separate models (All detections (encounters), Acoustic detections (acoustic), and Visual detections (visual)). My response variable is count data (detections along a line segment). And predictor variables are sea swell, bft scale, swell in meters, depth of water, distance from shore for the detection, time of day, month, and area. I have an effortkm offset as each survey line is of slightly different lengths. My aim is to understand which of these factors (if any) influence the detectability of the species. Many of the lines, don't have any encounters, so I have a lot of zeros, especially for the visual detection model (example dataframe in image link)

(Example of how the data frame looks)

I ran zero inflation GAM in mgcv and it works okay for encounters and acoustic but for the visual model I get an error: Warning: Fitting terminated with step failure - check results carefully.

In my case, would a hurdle model work better? I tried the hurdle model in mgcv but I am having trouble adding my categorical variables. I am able to add them to only one part of the model:

fp_hurdle_enc <- gam(list(encounters ~
      s(swell, k = 5) + 
      s(dist, k = 5) +
      s(depth, k = 5) +
      s(time, k = 5),
    ~ s(swell, k = 5) +
      s(dist, k = 5) +
      s(depth, k = 5) +
      s(time, k=5) +
      bft2 + area + month + offset(effortkm)),
  data = porpoise, method = "REML",
  family= ziplss(link=list("identity","identity")))

Does this mean that it is using the categorical variables in one part of the model and not the other? Also when I try to fit the same to the visual detections, it gives the following error:

*Error in gam.fit5(x, y, sp, Sl = Sl, weights = weights, offset = offset, :
indefinite penalized likelihood in gam.fit5*

Its a smallish data set, I have about 331 data points, with 136 encounters, out of which 132 are acoustic and 37 are visual.

I'm very new to GAM, and would really appreciate help in navigating these errors. Any alternate method suggestions are welcome!

  • 1
    $\begingroup$ I am not an expert on GAMs but fitting a model with so many covariates to a dataset (visual encounters) with so few events strikes me as very ambitious. $\endgroup$
    – mdewey
    Aug 20, 2023 at 13:03

1 Answer 1


As @mdewey says, this would be very ambitious indeed with 331 data points, let alone a smaller data set (if that is what is implied by 37 visual encounters?).

Anyway, it looks like you have the linear predictors back to front. The first formula is for the Poisson part of the model so I would expect the offset to be in that count model and not in the binomial part, which comes second in the list of linear predictors.

Currently the categorical variables are only in the binomial part of the model.

It wouldn't surprise me if you are able to perfectly (or nearly perfectly) predict the 0s and 1s with a data set this small and all those terms in the binomial part of the model, which can lead to errors like this. Even if it is not this specific problem, this error often arises when the model is not identifiable or you are trying to fit too many terms to the data at hand.

Finally, lots of 0s doesn't imply zero-inflation; it could well be that the 0s arises from a low expected count. Zero-inflation can only be assessed with respect to the mean model; does your data have more zeroes than can be generated by your model?

I would suggest starting off simply and model the counts with a Poisson of Negative Binomial model and compare the expected number of 0s with the observed number (using say a rootogram).


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.