I am running a regression with a negative binomial error. The outcome is occasionally censored from below, but I am getting the following error on the line with the neg_binomial_2_lcdf
call:
Exception: grad_2F1: k (internal counter) exceeded 100000 iterations, hypergeometric function gradient did not converge. (in ‘model4923325fd331_nb_std_censored’ at line 30)
The fact that not all chains fail makes me think it might be an issue with the prior or the initialization. Any ideas would be greatly appreciated. Model and fake data below.
data {
int N; //the number of observations
int<lower=0,upper=1> detected[N]; // whether the outcome is detected (i.e. not censored)
int K; //the number of columns in the design matrix
int<lower=0> y[N]; //the response
matrix[N,K] X; //the design matrix
real reciprocal_phi_scale;
}
parameters {
vector[K] beta; //the regression parameters
real<lower=0> reciprocal_phi;
}
transformed parameters {
real phi;
phi = 1. / reciprocal_phi;
}
model {
vector[N] Xbeta;
Xbeta = X * beta;
beta ~ cauchy(0,10);
reciprocal_phi ~ cauchy(0,reciprocal_phi_scale);
for (i in 1:N) {
if (detected[i]) {
y[i] ~ neg_binomial_2_log(Xbeta[i], phi);
} else {
target += neg_binomial_2_lcdf(y[i] | exp(Xbeta[i]), phi);
}
}
}
generated quantities {
vector[N] y_sim;
for (i in 1:N) {
y_sim[i] = neg_binomial_2_log_rng(X[i]*beta, phi);
}
}
Without covariates rarely gives an error. Note that I’m taking observations \leq 3 and observing them as censored \leq 3.
N <- 100
y <- rnbinom(N, mu=6, size=2)
nb_data <- list(N=N,
y=ifelse(y > 3, y, 3),
detected= ifelse(y > 3, 1, 0),
K=1,
X=data.frame(c=rep(1,N)),
reciprocal_phi_scale=5,
beta_scale=5)
And some data that occasionally gives the error. My real data gives the error reliably but I can’t share it.
N <- 100
x <- rnorm(N, sd=1)
beta <- c(1, .5)
y <- rnbinom(N, mu=exp(beta[1]+beta[2]*x), size=2)
nb_data <- list(N=N,
y=ifelse(y > 3, y, 3),
detected= ifelse(y > 3, 1, 0),
K=2,
X=data.frame(c=rep(1,N), x=x),
reciprocal_phi_scale=5,
beta_scale=5)