I am solving an ODE function on, but it returns an error. I am fairly new to Stan and have gone through the manual (version 2.23), I build a variable function and try to use it later in the parameter section. The error message is below:
functions {
vector[2] dydt
dydt[1] = -ka * y[1];
dydt[2] = ka * y[1] - ke * y[2];
return dydt;
}
}
data {
int<lower=0> N;
vector[N] y;
real t[N];
real<lower = 0> D;
}
parameters {
real<lower = 0> ke;
real<lower = ke> ka;
real<lower = 0> V;
real<lower = 0> sigma;
vector rhs(real t, vector y,
real ka, real ke);
}
transformed parameters {
vector[N] mu;
{ vector[2] solution[N] = ode_rk45(rhs, [D, 0.0]', 0.0, t, ka, ke);
for(i in 1:N){
mu[i] = solution[i,2] / V;
}
}
}
model {
y ~ normal(mu, sigma);
sigma ~ normal(0, 1);
ke ~ student_t(7, 0, 1);
ka ~ student_t(7, 0, 5);
V ~ student_t(7, 0, 1);
}
The syntax error returns as below:
SYNTAX ERROR, MESSAGE(S) FROM PARSER:
error in 'model3f5c1ce435e1_pk' at line 2, column 9
-------------------------------------------------
1: functions {
2: vector[2] dydt
^
3: dydt[1] = -ka * y[1];
-------------------------------------------------
PARSER EXPECTED: <comma to indicate more dimensions or ] to end type declaration>
Error in stanc(file = file, model_code = model_code, model_name = model_name, :
failed to parse Stan model 'pk' due to the above error.
You’re missing the initial lines in your function declaration specifying the function name , type, and inputs and their types. It looks like you put that line in the parameters section instead, which is going to cause an error too. So I think you want:
functions {
vector rhs(real t, vector y, real ka, real ke){
vector[2] dydt ;
dydt[1] = -ka * y[1];
dydt[2] = ka * y[1] - ke * y[2];
return dydt;
}
}
data {
int<lower=0> N;
vector[N] y;
real t[N];
real<lower = 0> D;
}
parameters {
real<lower = 0> ke;
real<lower = ke> ka;
real<lower = 0> V;
real<lower = 0> sigma;
}
transformed parameters {
vector[N] mu;
{ vector[2] solution[N] = ode_rk45(rhs, [D, 0.0]', 0.0, t, ka, ke);
for(i in 1:N){
mu[i] = solution[i,2] / V;
}
}
}
model {
y ~ normal(mu, sigma);
sigma ~ normal(0, 1);
ke ~ student_t(7, 0, 1);
ka ~ student_t(7, 0, 5);
V ~ student_t(7, 0, 1);
}
The tags say RStan so I assume that’s the interface you’re working with. If I recall correctly RStan is currently at version 2.21 so features added in 2.23 are not available. You may have to use the old integrate_ode interface.
functions {
real[] rhs(real t, real[] y, real[] k, data real[] x_r, data int[] x_i) {
real ka = k[1];
real ke = k[2];
real dydt[2];
dydt[1] = -ka * y[1];
dydt[2] = ka * y[1] - ke * y[2];
return dydt;
}
}
data {
int<lower=0> N;
vector[N] y;
real t[N];
real<lower = 0> D;
}
parameters {
real<lower = 0> ke;
real<lower = ke> ka;
real<lower = 0> V;
real<lower = 0> sigma;
}
transformed parameters {
vector[N] mu;
{ real solution[N,2] = integrate_ode_rk45(rhs, {D, 0.0}, 0.0, t, {ka, ke}, {1.0}, {1});
for(i in 1:N){
mu[i] = solution[i,2] / V;
}
}
}
model {
y ~ normal(mu, sigma);
sigma ~ normal(0, 1);
ke ~ student_t(7, 0, 1);
ka ~ student_t(7, 0, 5);
V ~ student_t(7, 0, 1);
}