stan::math::neg_binomial_log: Shape parameter is 0, but must be > 0!
Error in function boost::math::lgamma<long double>(long double): Evaluation of lgamma at 0.
model_code = """ data { int<lower=0> N; //rows of data int<lower=0> K; // number of covariates int<lower=0> J; // number of groups matrix[N,K] x; // design matrix (predictors) int y[N]; // response (outcomes) int<lower=1, upper=J> groups[N]; // group IDs } parameters { real intercept; // Population intercept vector[K] beta; // Population slope parameters real<lower=0> sigma; // population standard deviation real u[J]; // random effects real<lower=0> sigma_u; // standard deviation of random effects real<lower=0, upper=1> B; // negative binomial dispersion parameter } transformed parameters { real beta_0j[J]; // varying intercepts real mu[N]; // individual means real A[N]; // linking function (neg_binomial alpha parameter) //Varying intercepts definition for (j in 1:J) { beta_0j[j] <- intercept + u[j]; }
// individual means for (n in 1:N) { mu[n] <- beta_0j[groups[n]] + x[n] * beta; A[n]<-exp(mu[n]) * B; } } model { // Priors beta ~ normal(0,10); B ~ beta(2,2); // Random effects distribution u ~ normal(0,sigma_u); // Likelihood part of Bayesian inference for (n in 1:N) { y[n] ~ neg_binomial(A[n], B); } } """
model_dat = ({'N':len(df), 'K': 2, 'J': 9, 'y': list(df['Y'].astype(int)), 'x': np.matrix([list(df['X1']), list(pd.Categorical(df['X2']).codes)]).T, 'groups': list(pd.Categorical(df['groups']).codes + 1) // too make it (1:J) })
fit = pystan.stan(model_code=model_code, data=model_dat, chains=4)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "stanfit4anon_model_ce308d62b54c6136648513401f98e15f_2834170169694085174.pyx", line 580, in stanfit4anon_model_ce308d62b54c6136648513401f98e15f_2834170169694085174.StanFit4Model.__repr__ (/var/folders/34/hrtbnddj7fv1ztqqgc3l8rfw0000gn/T/tmpHVJ474/stanfit4anon_model_ce308d62b54c6136648513401f98e15f_2834170169694085174.cpp:11263) File "stanfit4anon_model_ce308d62b54c6136648513401f98e15f_2834170169694085174.pyx", line 576, in stanfit4anon_model_ce308d62b54c6136648513401f98e15f_2834170169694085174.StanFit4Model.__str__ (/var/folders/34/hrtbnddj7fv1ztqqgc3l8rfw0000gn/T/tmpHVJ474/stanfit4anon_model_ce308d62b54c6136648513401f98e15f_2834170169694085174.cpp:11139) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pystan/misc.py", line 81, in _print_stanfit s = _summary(fit, pars, probs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pystan/misc.py", line 205, in _summary ss = _summary_sim(fit.sim, pars, probs) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pystan/misc.py", line 315, in _summary_sim ess_and_rhat = np.array([pystan.chains.ess_and_splitrhat(sim, n) for n in tidx_colm]) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pystan/chains.py", line 34, in ess_and_splitrhat return (ess(sim, n), splitrhat(sim, n)) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pystan/chains.py", line 13, in ess return _chains.effective_sample_size(sim, n) File "pystan/_chains.pyx", line 128, in pystan._chains.effective_sample_size (pystan/_chains.cpp:2491)AssertionError: nan
Begin forwarded message:
fit = pystan.stan(model_code=model_code, data=model_dat, chains=4), sample_file=home+'/Desktop/model_samp.txt')
model_code = """ data { int<lower=0> N; //rows of data int<lower=0> K; // number of covariates int<lower=0> J; // number of groups matrix[N,K] x; // design matrix (predictors) int y[N]; // response (outcomes) int<lower=1, upper=J> groups[N]; // group IDs } parameters { real intercept; // Population intercept vector[K] beta; // Population slope parameters real<lower=0> sigma; // population standard deviation real u[J]; // random effects real<lower=0> sigma_u; // standard deviation of random effects
real<lower=1.0e-20, upper=1> B; // negative binomial beta parameter
} transformed parameters { real beta_0j[J]; // varying intercepts
real<lower=-100> mu[N]; // individual means real<lower=1.0e-20> A[N]; // negative binomial alpha parameter
//Varying intercepts definition for (j in 1:J) { beta_0j[j] <- intercept + u[j]; }
// individual means for (n in 1:N) { mu[n] <- beta_0j[groups[n]] + x[n] * beta;
A[n]<-exp(mu[n]) * B; // linking function } } model { // Priors sigma ~ normal(0,2); beta ~ normal(0,10); //B ~ beta(2,2);
// Random effects distribution u ~ normal(0,sigma_u);
// Data Model
for (n in 1:N) { y[n] ~ neg_binomial(A[n], B); } } """
if one assumes Uij ∼ NB(ψ, ηij )... the NB-I distribution is the only appropriate choice for the multivariate negative binomial model and mean parameterizations are often used such that ψ = σ2 and ηij = λij/ψ for j ∈ {0, 1, 2, 3}. The covariates could be incorporated as the usual log-linear model λij = exp(x′ijβj)
alpha <- sigma * sigma;
for (n in 1:N) { mu[n] <- beta_0j[groups[n]] + x[n] * beta;
lambda[n]<-exp(mu[n]); NB_beta[n]<-lambda[n] / alpha;}
model_code = """ data { int<lower=0> N; //rows of data int<lower=0> K; // number of covariates int<lower=0> J; // number of groups matrix[N,K] x; // design matrix (predictors) int y[N]; // response (outcomes) int<lower=1, upper=J> groups[N]; // group IDs } parameters {
real intercept; // Population intercept
vector[K] beta; // Population slope parameters real<lower=0> sigma; // population standard deviation real u[J]; // random effects real<lower=0> sigma_u; // standard deviation of random effects }
transformed parameters { real beta_0j[J]; // varying intercepts real<lower=-100> mu[N]; // individual means
real alpha; // negative binomial alpha parameter real NB_beta[N]; // negative binomial beta parameter real lambda[N]; // exp(mu) //real c; // a constant? alpha <- sigma * sigma;
//Varying intercepts definition for (j in 1:J) { beta_0j[j] <- intercept + u[j]; }
// individual means for (n in 1:N) { mu[n] <- beta_0j[groups[n]] + x[n] * beta;
lambda[n]<-exp(mu[n]); NB_beta[n]<-lambda[n] / alpha;
} } model { // Priors sigma ~ normal(0,2); beta ~ normal(0,10);
NB_beta ~ beta(2,2);
// Random effects distribution u ~ normal(0,sigma_u); // Data Model for (n in 1:N) {
I am still a bit confused. Taking some advice from this paper, they mention this (although its hard to tell if I'm taking it in the right context here):if one assumes Uij ∼ NB(ψ, ηij )... the NB-I distribution is the only appropriate choice for the multivariate negative binomial model and mean parameterizations are often used such that ψ = σ2 and ηij = λij/ψ for j ∈ {0, 1, 2, 3}. The covariates could be incorporated as the usual log-linear model λij = exp(x′ijβj)
So I incorporated the following into my model:
for (n in 1:N) {
mu[n] <- beta_0j[groups[n]] + x[n] * beta;
lambda[n]<-exp(mu[n]);
NB_beta[n]<-lambda[n] / alpha;
}
If I do not set a prior for NB_beta, NB_beta is still > 1. If I incorporate NB_beta ~ beta(2,2) then the NB_beta results as [0,1]. You mention that NB_beta ~ beta(2,2) is too weak of a prior. Is there some other way I should specify my priors to me more informative. Or perhaps a reference you could point me to on the subject. I really don't have any relevant data to go by other than my own dataset. The model is now giving Rhats very close to one, but I would like to make sure I set my priors properly (and also provide proper information for anyone using this thread in the future). Here is my code currently.
I apologize for the barrage of questions. I understand that modeling is something that can take years or decades to master, and I certainly have a long way to go. I truly do appreciate all the helpful suggestions and will try to implement everybody's advise as much as possible. I was not intending to reinvent the wheel, so any appearance of that probably just shows how far off base I am. I have been spoiled by R and Python, so getting myself from the standard Y ~ X1 + X2 + (1|groups) is not proving easy.
Would you happen to know how I might expose the STAN code used to fit the model in rstanarm?
model_code = """ data { int<lower=0> N; //rows of data
int y[N]; // response (outcomes) }
parameters { real beta; // the lambda parameter of a Poisson distribution also?
real alpha; // negative binomial alpha parameter }
model { beta ~ uniform(0,1); // Data Model y ~ neg_binomial(alpha, beta); } """
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhatbeta 0.82 7.5e-3 0.14 0.49 0.75 0.86 0.93 0.99 343.0 1.0alpha 0.34 5.0e-3 0.08 0.2 0.29 0.34 0.39 0.51 251.0 1.01lp__ -77.92 0.04 0.91 -80.24 -78.32 -77.69 -77.29 -76.8 452.0 1.0
model_code = """ data { int<lower=0> N; //rows of data
int y[N]; // response (outcomes) }
parameters { real beta; // the lambda parameter of a Poisson distribution also?
real alpha; // negative binomial alpha parameter }
model { beta ~ beta(0.34,0.82); // Data Model y ~ neg_binomial(alpha, beta); } """
import numpy as npfrom scipy.special import binom
def Gelman_NB(theta, alpha, beta): term1 = binom((theta + alpha - 1),(alpha - 1)) term2 = (beta / (beta + 1)) ** alpha term3 = (1 / (beta + 1)) ** theta x = term1 * term2 * term3 return x
theta = np.arange(0,11,1)pmf = Gelman_NB(theta, 2.2, 1.1)plt.bar(theta, pmf)plt.show()
print(pmf.sum()) # should intergrate to one for theta = [0, inf]