Just a note: it is possible that the problem is not Sigma_phi
, but something else. The error is not about some values being immediately problematic, but rather that the gradient is infinite - this is unfortunately a bit harder to debug. The possibilities include:
- Some of the denominators in computing
H_pre04
/H_04
being very close to 0. -
f
large leading toexp(y)
very large- Using
neg_binomial_2_log(log(m) + f, r)
might be more numerically stable. Also note that using offest in neg. binom models has some complexities not seen in Poisson models as discussed e.g. at: Scaling of the overdispersion in negative binomial models
- Using
-
m < 0
(but I think that would give a different error, but I am not sure)
In all cases, you can use print
statements to get output from the model and see a bit better what’s happening. I would also suggest you try to simplify the model further - most notably, which terms of the covariance matrix can you remove and still see the issue?
EDIT: Additionally, in older versions of Stan (I think before 2.23, which includes the CRAN version of rstan
), there were some numerical instabilities in neg_binomial_2
which might also be partly responsible.