Dear all, Could someone please have a look at my code which always complains of the rejection of Metropolis proposal? The priors seem reasonable. Here is the error:
Chain 1 Informational Message: The current Metropolis proposal is about to be rejected because of the following issue:
Chain 1 Exception: lognormal_lpdf: Location parameter is -nan, but must be finite! (in ‘/tmp/RtmpcpVYpz/model-26327a33e7fb.stan’, line 40, column 4 to column 54)
Chain 1 If this warning occurs sporadically, such as for highly constrained variable types like covariance matrices, then the sampler is fine,
Chain 1 but if this warning occurs often then your model may be either severely ill-conditioned or misspecified.
and here is the code:
library(rethinking)
data("Lynx_Hare")
d <- Lynx_Hare
Lynx.dat = list(N =21, L = d$Lynx, H = d$Hare, Y = d$Year)
Lynx.c = '
functions {
vector MyODE( real t, // time
vector pop_init, // initial state {lynx, hares}
vector theta){ // parameters
real L = pop_init[1];
real H = pop_init[2];
real bH = theta[1];
real mH = theta[2];
real bL = theta[3];
real mL = theta[4];
// differential equations
vector[2] dif;
dif[1] = (bH - mH * L) * H;
dif[2] = (bL * H - mL) * L;
return dif;
}
}
data{
int N;
vector[N] L;
vector[N] H;
}
parameters{
vector<lower=0>[4] theta; //{bH, mH, bL, mL}
vector<lower=0>[2] sigma;
vector<lower=0,upper=1>[2] p;
}
model{
array[N-1] real ts;
for(i in 1:(N-1)) ts[i] = i;
array[N] vector[2] pop;
pop[1,1] = H[1];
pop[1,2] = L[1];
pop[2:N, 1:2] = ode_rk45(MyODE, to_vector(pop[1]), 0, ts, theta);
for ( t in 1:N ){
target += lognormal_lpdf(H[t] | log(pop[t , 1]*p[1]), sigma[1]);
target += lognormal_lpdf(L[t] | log(pop[t , 2]*p[2]), sigma[2]);
}
p ~ beta(40, 200);
sigma ~ exponential(1);
theta[{1,4}] ~ normal(1, 0.5); // {bH, mL}
theta[{2, 3}] ~ normal(0.1, 0.05); // {mH, bL}
}
'
Lynx.f <- stan( model_code=Lynx.c,data=Lynx.dat,chains=4, iter = 3000, cores=1 , control=list( adapt_delta=0.95 ) )
Thanks :)