Hi, I’m sorry if the answer to this question is pretty simple - I’m a Stan novice and a high school student, so I’m still figuring this all out.

I’m trying to generalize Bob Carpenter’s Lotka Volterra case study for three populations and I keep getting the same error, regardless of what I change.

Here’s some information about my computer, if it’s helpful:

- Operating system: macOS Mojave, 10.4.2
- RStan Version: 2.18.2
- Computer: 2017 MacBook Air, 8GB RAM

The error I keep getting:

```
SAMPLING FOR MODEL 'wolfelkcoyote' NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: lognormal_lpdf: Location parameter[1] is nan, but must be finite! (in 'model14865b3e324_wolfelkcoyote' at line 53)
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: lognormal_lpdf: Location parameter[1] is nan, but must be finite! (in 'model14865b3e324_wolfelkcoyote' at line 53)
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: Max number of iterations exceeded (1000). (in 'model14865b3e324_wolfelkcoyote' at line 39)
[1] "Error in sampler$call_sampler(args_list[[i]]) : "
[2] " Exception: Max number of iterations exceeded (1000). (in 'model14865b3e324_wolfelkcoyote' at line 39)"
error occurred during calling the sampler; sampling not done
```

Here’s my Stan code:

```
functions {
real[] dz_dt(real t, // time
real[] z, // system state {prey, predator}
real[] theta, // parameters
real[] x_r, // unused data
int[] x_i) {
real l = z[1]; // elk (as e isn't a valid variable name)
real c = z[2]; // coyotes
real w = z[3]; // wolves
real kpo = theta[1]; // this and below are coefficents used in equations
real pec = theta[2];
real pew = theta[3];
real kdo = theta[4];
real kptw = theta[5];
real kdtw = theta[6];
real kdth = theta[7];
real kpth = theta[8];
real dl_dt = (kpo - (pec * c) - (pew * w)) * l; // diff eq for elk pop
real dc_dt = ((kptw * l) - (kdtw * w) - kdo) * c; // diff eq for coyote pop
real dw_dt = ((kpth * l) - kdth) * w; // diff eq for wolf pop
return { dl_dt, dw_dt, dc_dt }; // dz_dt gives results of all 3 diff eqs (e, w, c)
}
}
data {
int<lower = 0> N; // number of measurement times
real ts[N]; // measurement times > 0
real y_init[3]; // initial measured populations
real<lower = 0> y[N, 3]; // measured populations
}
parameters {
real<lower = 0> theta[8]; // { kpo, pec... }
real<lower = 0> z_init[3]; // initial population
real<lower = 0> sigma[3]; // measurement errors
}
transformed parameters {
real z[N, 3]
= integrate_ode_rk45(dz_dt, z_init, 0, ts, theta,
rep_array(0.0, 0), rep_array(0, 0),
1e-5, 1e-3, 1e3);
}
model {
theta[{1,3,5}] ~ normal(1, 0.5);
theta[{2,4,6,7,8}] ~ normal(0, 0.5);
sigma ~ lognormal(-1, 1);
z_init ~ lognormal(10, 1);
for (k in 1:3) {
y_init[k] ~ lognormal(log(z_init[k]), sigma[k]);
y[ , k] ~ lognormal(log(z[, k]), sigma[k]);
}
}
generated quantities {
real y_init_rep[3];
real y_rep[N, 3];
for (k in 1:3) {
y_init_rep[k] = lognormal_rng(log(z_init[k]), sigma[k]);
for (n in 1:N)
y_rep[n, k] = lognormal_rng(log(z[n, k]), sigma[k]);
}
}
```

Here’s my R code:

rstan_options(auto_write=TRUE)

```
setwd("/Users/username/desktop/rfolder")
elk_wolf_coyote <- read.csv("incorrectFinalProjData.csv", comment.char="#")
N <- length(elk_wolf_coyote$Year) - 1 # number of observations after the first
ts <- 1:N # observation times (years)
y_init <- c(elk_wolf_coyote$Elk[1], elk_wolf_coyote$Coyotes[1], elk_wolf_coyote$Wolves[1]) # first observation
y <- as.matrix(elk_wolf_coyote[2:(N+1), 2:4]) # observations after the first
y <- cbind(y[ ,3],y[ ,2], y[ , 1]); # wolf, coyote, elk order (REVERSES the order of above line)
elk_wolf_coyote_data <- list(N, ts, y_init, y)
model <- stan_model("wolfelkcoyote.stan")
fit <- sampling(model, data = elk_wolf_coyote_data, seed=123)
print(fit, pars=c("theta", "sigma", "z_init"),
probs=c(0.1, 0.5, 0.9), digits = 3)
```

Here’s the session info for my computer as well:

```
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS 10.14.2
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
L.APACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rstan_2.18.2 StanHeaders_2.18.0-1 ggplot2_3.1.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.0 pillar_1.3.0 compiler_3.5.1 plyr_1.8.4 bindr_0.1.1 prettyunits_1.0.2
[7] tools_3.5.1 pkgbuild_1.0.2 tibble_1.4.2 gtable_0.2.0 pkgconfig_2.0.2 rlang_0.3.0.1
[13] cli_1.0.1 rstudioapi_0.8 parallel_3.5.1 yaml_2.2.0 loo_2.0.0 bindrcpp_0.2.2
[19] gridExtra_2.3 withr_2.1.2 dplyr_0.7.8 stats4_3.5.1 grid_3.5.1 tidyselect_0.2.5
[25] glue_1.3.0 inline_0.3.15 R6_2.3.0 processx_3.2.1 purrr_0.2.5 callr_3.1.0
[31] magrittr_1.5 codetools_0.2-15 scales_1.0.0 ps_1.2.1 matrixStats_0.54.0 assertthat_0.2.0
[37] colorspace_1.3-2 lazyeval_0.2.1 munsell_0.5.0 crayon_1.3.4
```

I don’t know how to fix this error. Do any of you have suggestions for possible fixes?

Thank you very much! Again, I apologize if any of my code is silly, I’m still really inexperienced with Stan.

Edit: I originally had a typo (wrote our instead of out somewhere) so fixed it for clarity.