I am new to coding models directly in STAN and this also happens to be my first attempt at fitting an ODE model using it. I am having a few issues with the model initialization and have not been able to gather from the forums a solution that I can generalize to fix my issues. I receive the following errors when running the model for all chains and the sampling does not occur:

“Chain 1: Rejecting initial value:

Chain 1: Error evaluating the log probability at the initial value.

Chain 1: Exception: normal_lpdf: Location parameter is nan, but must be finite! (in ‘string’, line 80, column 2 to column 45)”

Using print() I have found none of the inputs to the ODE integrator are NA or unexpected but the matrix returned, ‘y_sim’ in the model, returns all “nan”.

I am running the model in rstan v2.32.5. Any help on debugging or fixing this error, or comments on the model in general would be much appreciated.

Thanks.

Relevant example below.

```
functions {
real[] C_R(real t, // Time
real[] y, // State variables: y[1] = R, y[2] = C
real[] theta, // Parameters
real[] x_r, // Real data (temperature, light)
int[] x_i) { // Int data (empty here)
// observed states
real R = y[1];
real C = y[2];
real cB = y[3];
// parameters
real a = theta[1];
real h = theta[2];
real r = theta[3];
real K = theta[4];
real e = theta[5];
real m = theta[6];
// real data
real dcB_dt = a*R*C/(1+a*h*R); // Biomass consumption
real dR_dt = r*R*(1 - R/K) - cB; // Prey dynamics
real dC_dt = (e * cB) - (m * C); // Predator dynamics
return {dR_dt, dC_dt, dcB_dt};
}
}
data {
int<lower=1> T; // Number of time steps
real<lower=0> ts[T]; // Time values
real<lower=0> R_initial; // Initial resource population
real<lower=0> C_initial; // Initial consumer population
real<lower=0> cB_initial; // Initial consumption
real<lower=0> C_obs[T]; // Observed mean consumer population data
real<lower=0> R_obs[T]; // Observed mean resource population data
real<lower=0> cB_obs[T]; // Observed Biomass specific consumption
real<lower=0> sigma_C[T]; // Observed sd consumer population data
real<lower=0> sigma_R[T]; // Observed sd resources population data
real<lower=0> sigma_cB[T]; // Observed sd consumption
// real<lower=0> temp[T]; // observed temperature data
// real<lower=0> light[T]; // observed light data
}
transformed data{
real x_r[0];
int x_i[0];
}
parameters {
real<lower=0,upper=1> a; // attack/clearance rate parameter
real<lower=0> h; // handling time
real<lower=0> r; // prey growth rate
real<lower=0> K; // Carrying capacity
real<lower=0,upper=1> m; // Consumer mortality rate
real<lower=0,upper=1> e; // gross growth efficiency
// real<lower=0> sigma;
}
model {
real y_init[3] = {R_initial, C_initial, cB_initial};
real theta[6] = {a, h, r, K, e, m}; // Parameters
// matrix[T, 2] y_sim; // Simulated population dynamics
real y_sim[T,3]; // Simulated population dynamics
// Priors
r ~ lognormal(1, 1);
K ~ lognormal(10, 3);
a ~ beta(1, 1);
h ~ lognormal(0, 1);
e ~ beta(1, 4);
m ~ beta(1, 1);
// sigma ~ exponential(0.5);
print(a);
print(K);
print(r);
print(h);
print(e);
print(m);
// Solve ODE
// y_sim = integrate_ode_bdf(C_R, y_init, 0, ts, theta, x_r, x_i);
y_sim = integrate_ode_rk45(C_R, y_init, 0, ts, theta, x_r, x_i);
print(y_sim);
// Likelihood
for (t in 1:T) {
R_obs[t] ~ normal(y_sim[t, 1], sigma_R[t]);
C_obs[t] ~ normal(y_sim[t, 2], sigma_C[t]);
cB_obs[t] ~ normal(y_sim[t,3], sigma_cB[t]);
}
// for (t in 1:T) {
// R_obs[t] ~ normal(y_sim[t, 1], sigma);
// C_obs[t] ~ normal(y_sim[t, 2], sigma);
// }
}
generated quantities {
real y_init[3] = {R_initial, C_initial, cB_initial};
real theta[6];
real y_pred[T, 3];
// Parameters
theta[1] = a; // Saturation parameter
theta[2] = h; // Half-saturation constant (for resource)
theta[3] = r; // Base prey growth rate
theta[4] = K; // Carrying capacity
theta[5] = e; // Error term for consumer population
theta[6] = m; // Consumer mortality rate
// Solve ODE for generated quantities
// y_pred = integrate_ode_bdf(C_R, y_init, 0, ts, theta, x_r, x_i);
y_pred = integrate_ode_rk45(C_R, y_init, 0, ts, theta, x_r, x_i);
}
```

The data I have been passing to this model are:

```
stan_data <- list(T = 10L, ts = c(41, 87, 142, 171, 208, 214, 259, 296, 317,
346), R_initial = 7.95909938, C_initial = 317.44602496823, cB_initial = 2046.65868443038,
R_obs = c(7.95909938, 63.582298132, 166.26195652, 203.014871545303,
249.90652175, 269.91180124, 401.51878882, 146.994720504,
163.72037267184, 186.817701856), sigma_R = c(8.35977921339248,
32.7304043315385, 40.4895905956132, 77.1965146978821, 124.029486828363,
113.278703479259, 139.435933432936, 80.8086875268657, 99.999220067294,
126.500431670743), C_obs = c(317.44602496823, 925.57349071122,
5755.67593050651, 6409.7857725782, 15814.0416956033, 2492.23227708924,
2061.77271128308, 677.379879132129, 167.537343626406, 305.172916746582
), sigma_C = c(131.355718609556, 227.261418700627, 932.462821358444,
1316.3381507101, 6316.15110556263, 300.928146091724, 377.661169342442,
145.108450308571, 46.0375633029446, 71.6065909214821), cB_obs = c(2046.65868443038,
2060.03573956124, 376.377453805423, 763.560069928162, 33.6712991904767,
604.310412430487, 525.789893863879, 351.698498991322, 1655.07227107854,
2265.43247664591), sigma_cB = c(108.54143636971, 62.5486724534629,
7.96306161036773, 24.8668267776185, 0.933383933755735, 16.0339488862616,
11.4392829893471, 6.27473216475427, 60.7508978815252, 80.4613467879306
))
```