Hi,
I am new to Stan and am trying to construct a hierarchical model as per the following requirements:
response Y ~ neg_binomial_2( mu.y , phi.y ) ## the dots are only for explanation purposes here; the parameters are not named with dots in the Stan code.
where, mu.y = exp(a) * X^b ## This is a biologically meaningful and established relationship for the variables being measured in this system.
and, X is itself drawn from a negative binomial distribution X ~ neg_binomial_2( lamda , phi.x) with lamda ~ lognormal( lamda.bar , sigma)
lamda.bar = mean(X.obs.plot), where X.obs is a vector of the observed values of the predictor at that location/plot, measured over multiple years. The expected number of X for a plot is lamda and these were drawn from a lognormal distribution with mean lamda.bar and variance sigma sq.
FYI, to give you an idea of why I need to undertake this joint modeling of the probability distribution of Y:
In this data, the response Y was measured in plots adjacent to where the predictor X.obs was measured. This spatial mismatch implies that the values of Y do not correspond to the true value of X at that location, but have to be estimated from X.obs. Additionally, in many cases, the value of X.obs is 0 but there is a non-zero value for Y. This again occurs due to the spatial mismatch mentioned above. Therefore, the actual values of X used to obtain mu.y for the negative binomial distribution for estimating the response Y, have to be drawn from the values of X.obs. The response Y has to modeled from a joint probability distribution of the expected values for X and Y.
I've pasted my code and error messages below. There might be something very basic in the coding that I am unable to understand and hence going wrong with. Any help will be much appreciated!
Thank you for your time.
Best,
Meghna
perspp.nb.hier<-"
data{
int<lower=0> N; //no.of data points
vector[N] seed; //predictor: positive real number, range= 0 - 50000 (~60% values are zeros)
int<lower=0> sdl[N]; //response: integer, range = 0 - 212 (~50% values are zeros)
}
parameters{
real sigma;
real<lower=0> lamda;
real<lower=0> phisd;
real alpha;
real beta;
real<lower=0> phisdl;
}
model{
int seed2[N];
sigma ~ uniform(0,10);
phisd ~ uniform(0,10);
alpha ~ uniform(0,100);
beta ~ uniform(0,10);
phisdl ~ uniform(0,10);
for(i in 1:N){
lamda[i] ~ lognormal(seed[i], sigma);
seed2[i] ~ neg_binomial_2(lamda[i], phisd);
}
sdl ~ neg_binomial_2_log(exp(alpha)*seed2^beta, phisdl);
}
}"
DIAGNOSTIC(S) FROM PARSER:
Warning (non-fatal):
Left-hand side of sampling statement (~) may contain a non-linear
transform of a parameter or local variable.
If so, you need to call increment_log_prob() with the log absolute
determinant of the Jacobian of the transform.
Left-hand-side of sampling statement:
seed2[i] ~ neg_binomial_2(...)
SAMPLING FOR MODEL 'eb5d4d92b044bf68050a15a91450a8dc' NOW (CHAIN 1).
[1] "Rejecting initial value:"
[2] " Error evaluating the log probability at the initial value."
[3] "Informational Message: The current Metropolis proposal is about
to be rejected because of the following issue:"
[4] "Exception thrown at line 30: stan::math::neg_binomial_2_log:
Location parameter is 0, but must be > 0!"
[5] "If this warning occurs sporadically, such as for highly
constrained variable types like covariance matrices, then the sampler
is fine,"
[6] "but if this warning occurs often then your model may be either
severely ill-conditioned or misspecified."
[7] "Rejecting initial value:"
[8] " Error evaluating the log probability at the initial value."
[9] "Initialization between (-2, 2) failed after 100 attempts. "
[10] " Try specifying initial values, reducing ranges of constrained
values, or reparameterizing the model."
[1] "error occurred during calling the sampler; sampling not done"