I’m trying to fit a hierarichal model with a continuous latent variable (distest) coming from a WALD dispersal kernel and a higher order variable (dist2) coming from a student bivariate distribution. Both of these distributions are custom so I define the whole model as follows.

```
functions{
real wald_lpdf(real x, real mu, real lambda){ //defining wald pdf
real prob;
real lprob;
prob = ((lambda/(2.00*pi()*(x^3.00)))^0.5)*exp(-(lambda*((x-mu)^2.00))/(2.00*(mu^2.00)*x));
lprob = log(prob);
return lprob;
}
real student2dt_lpdf(real r, real p, real u){ //defining 2dt pdf
real prob;
real lprob;
real area;
real kernel;
area = 2.00*pi()*r;
kernel = (p/(pi()*u*(1.00+(r^2.00)/u)^(p+1.00)));
prob = area * kernel;
lprob = log(prob);
return lprob;
}
}
data {
int <lower = 0> N; //number of observations
real <lower = 0.0000000001> dist[N]; //trap distance
real <lower = 0> httrap[N]; //height on trap
real <lower = 0> mu_wald[N];
real <lower = 0> lambda[N];
real <lower = 0> sigma2[N];
}
parameters {
real<lower=0> p; //additional predictor effects
real<lower=0> u;
real<lower=0.000000000001> distest[N]; //latent dispersal distance from WALD kernel
}
transformed parameters{
real <lower=0> dist2[N];
//calculate estimated distances
for(s in 1:N){
if(httrap[s] <= 0.2){
dist2[s] = dist[s]; //if height <= 0.2m, est distance is measured distance
}
else {
dist2[s] = dist[s] + distest[s]; //latent ground distance from wald kernel
}
}
}
model{
p ~ gamma(0.001,0.001);
u ~ gamma(0.001,0.001);
//likelihood holding out some data
for(n in 1:N){
target += wald_lpdf(distest[n]|mu_wald[n],lambda[n]);
target += student2dt_lpdf(dist2[n]|p,u);
}
}
```

I think I’m not correctly specifying the pdfs for both of these custom probability functions. The first is a wald dispersal kernel pdf (from which a distance, distest, is being drawn) and the second is a student 2dt pdf, from which a distance, dist2 is being drawn. Distest is latent, dist2 is the sum of distest and a measured value dist.

I’m getting the following errors.

“Chain 1: Rejecting initial value:

Chain 1: Log probability evaluates to log(0), i.e. negative infinity.

Chain 1: Stan can’t start sampling from this initial value.”

I’m including a subsection of my data (randomized).

MockData.RData (2.1 KB)

I’ve tried; specifying initial values (p = 1, u = 1), setting gamma priors on p and u to constrain them above 0 (since they cannot be 0 in the pdf), exp mu_wald and lamba and p and u (I’m a bit unclear on this since the lpdfs are specified via log probability). None of these has worked. I’ve also tried just separately running the wald and student bivariate distributions separately. The student bivariate will run, the wald still gives me the same error.

I edited this post to reflect recent updates I made.