# Problem with Lotka-Volterra example

**URL:** <https://discourse.mc-stan.org/t/problem-with-lotka-volterra-example/10085>\
**Category:** Modeling\
**Created:** [August 7, 2019, 4:56am UTC](https://discourse.mc-stan.org/t/problem-with-lotka-volterra-example/10085 "2019-08-07T04:56:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wtzr](https://avatars.discourse-cdn.com/v4/letter/w/9e8a1a/32.png) [@wtzr](https://discourse.mc-stan.org/u/wtzr)\
**Post date:** [August 7, 2019, 4:56am UTC](https://discourse.mc-stan.org/t/problem-with-lotka-volterra-example/10085/1 "2019-08-07T04:56:22Z")

</div>

I am going through [the Lotka-Volterra example](https://mc-stan.org/users/documentation/case-studies/lotka-volterra-predator-prey.html#coding-the-model-stan-program), trying to reproduce it.

```

library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores(logical = FALSE))

lynx_hare_df <-
  read.csv("hudson-bay-lynx-hare.csv", comment.char="#")
head(lynx_hare_df, n = 3)

lynx_hare_melted_df <- melt(as.matrix(lynx_hare_df[, 2:3]))
colnames(lynx_hare_melted_df) <- c("year", "species", "pelts")
lynx_hare_melted_df$year <-
  lynx_hare_melted_df$year +
  rep(1899, length(lynx_hare_melted_df$year))
head(lynx_hare_melted_df, n=3)

tail(lynx_hare_melted_df, n=3)

N <- length(lynx_hare_df$Year) - 1 # num observations

ts <- 1:N # observation time

y0 <- c(lynx_hare_df$Hare[1], lynx_hare_df$Lynx[1]) # first observation

y<- as.matrix(lynx_hare_df[2:(N + 1), 2:3]) # remaining observ

y <- cbind(y[, 2], y[, 1]); # reverse order
lynx_hare_data <- list(N, ts, y0, y)

model <- stan_model("lotka-volterra.stan")

fit <- sampling(model, data = lynx_hare_data)

```

I get the following error:

```
> fit <- sampling(model, data = lynx_hare_data)
Error in new_CppObject_xp(fields$.module, fields$.pointer, ...) : 
  Exception: variable does not exist; processing stage=data initialization; variable name=N; base type=int (in 'model6d34530f74d5_lotka_volterra' at line 21)

failed to create the sampler; sampling not done

```

But the variable N does exist, the data for the stan model is a class list with N in the first position as a num.

lynx\_hare\_data \<- list(N, ts, y0, y)

Any help would be much appreciated, thank you.

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [August 7, 2019, 1:35pm UTC](https://discourse.mc-stan.org/t/problem-with-lotka-volterra-example/10085/2 "2019-08-07T13:35:40Z")

</div>

You’ll want to add names to the list, like:

```
lynx_hare_data <- list(N = N,
                       ts = ts,
                       y_init = y0,
                       y = y)

```

These aren’t added automatically by R. The list names (the things on the left side of the equals signs) are matched to the names in the Stan model. That make sense?

---

<div class="post-metadata">

**Author:** ![wtzr](https://avatars.discourse-cdn.com/v4/letter/w/9e8a1a/32.png) [@wtzr](https://discourse.mc-stan.org/u/wtzr)\
**Post date:** [August 7, 2019, 1:54pm UTC](https://discourse.mc-stan.org/t/problem-with-lotka-volterra-example/10085/3 "2019-08-07T13:54:24Z")

</div>

Yes, adding names did the trick!  
I obtained similar estimates for the parameters than those provided in the example.  
However, I noticed that during the sampling process, there were some errors:

```
SAMPLING FOR MODEL 'lotka-volterra' NOW (CHAIN 4).
Chain 4: Rejecting initial value:
Chain 4: Error evaluating the log probability at the initial value.
Chain 4: Exception: lognormal_lpdf: Location parameter[2] is nan, but must be finite! (in 'model6d34530f74d5_lotka_volterra' at line 44)

Chain 4: Rejecting initial value:
Chain 4: Error evaluating the log probability at the initial value.
Chain 4: Exception: lognormal_lpdf: Location parameter[6] is nan, but must be finite! (in 'model6d34530f74d5_lotka_volterra' at line 44)

```

---

<div class="post-metadata">

**Author:** ![bbbales2](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bbbales2/32/77_2.png) [@bbbales2](https://discourse.mc-stan.org/u/bbbales2)\
**Post date:** [August 8, 2019, 7:35pm UTC](https://discourse.mc-stan.org/t/problem-with-lotka-volterra-example/10085/4 "2019-08-08T19:35:54Z")

</div>

Sometimes it takes a few tries before Stan can find initial conditions that work. If the chain stops complaining after a couple tries, you’re probably good to go.
