User-defined function calls in transformed data block (for SBC, with differential equations)

I have a working stan model (tested using cmdstan 2.25.0) that uses the ode_rk45 differential equation solver to generate multinomial probabilities \mathbf z_t for observations at many different times t, in many different experimental units from several treatments. In the transformed parameters block:

	  z[tindex:(ntimesinblock[j, k] + tindex - 1)] = ode_rk45(dz_dt, zinit, t0, timeblocks[j, k, 1:ntimesinblock[j, k]], a0[depthcode[j]], a1[depthcode[j]], a1y1star[depthcode[j]], a2[depthcode[j]], deltab0[depthcode[j]], b1[depthcode[j]], b2[depthcode[j]]);

The user-defined function dz_dt returns the derivatives, given initial values zinit, initial time t0, parameters a_0, \ldots, b_2. Everything else is just indexing the times, treatments, etc, with ntimesinblock, timeblocks, depthcode supplied as data (the indexing is complicated because the treatments cause discontinuous changes at time points that differ between experimental units).

I want to use SBC via rstan to check whether sampling is working OK. In the transformed data block, I tried the same thing, having defined new variables z_, zinit_, t0_, a0_, …, b2_:

	  z_[tindex:(ntimesinblock[j, k] + tindex - 1)] = ode_rk45(dz_dt, zinit_, t0_, timeblocks[j, k, 1:ntimesinblock[j, k]], a0_[depthcode[j]], a1_[depthcode[j]], a1y1star_[depthcode[j]], a2_[depthcode[j]], deltab0_[depthcode[j]], b1_[depthcode[j]], b2_[depthcode[j]]);

and I get (with rstan 2.21.2)
Variable “dz_dt” does not exist.

Are there restrictions on calling user-defined functions from the transformed data block, or have I done something else wrong? The working (non-SBC) version and data attached.

aureliaode_overgrowth.stan (4.6 KB)
dynamicdata.txt (29.4 KB)

Turns out I’m asking the wrong question. The working version I attached gives the same error as above when I try to compile using stan_model() in rstan, but still compiles fine with cmdstan 2.27.0. Did something change in the rstan interface?

That’s correct, the ODE interfaces went through a pretty significant refactor after 2.21, so these won’t be compatible with the version of RStan on CRAN.

Given that you need RStan for SBC, you could try the preview of the next RStan version which has these functions available:

Just restart R and run:

remove.packages(c("StanHeaders", "rstan"))
install.packages("StanHeaders", repos = c("", getOption("repos")))
install.packages("rstan", repos = c("", getOption("repos")))

Thank you. That makes sense. I hadn’t realized the CRAN version was that far behind. I can compile my code on the next RStan version. For my application, I would need to distribute across multiple machines using Condor, and will probably only have access to the CRAN version of RStan on those machines, but I may be able to pre-compile the Stan code on a machine with the next RStan and distribute it along with the data.