# A variable from function section does not exist

**URL:** <https://discourse.mc-stan.org/t/a-variable-from-function-section-does-not-exist/21137>\
**Category:** Modeling\
**Tags:** techniques, rstan\
**Created:** [March 6, 2021, 7:18am UTC](https://discourse.mc-stan.org/t/a-variable-from-function-section-does-not-exist/21137 "2021-03-06T07:18:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jimwad](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@Jimwad](https://discourse.mc-stan.org/u/Jimwad)\
**Post date:** [March 6, 2021, 7:18am UTC](https://discourse.mc-stan.org/t/a-variable-from-function-section-does-not-exist/21137/1 "2021-03-06T07:18:52Z")

</div>

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:

```stan
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.

```

Can someone help me with this? thanks!

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [March 6, 2021, 8:30am UTC](https://discourse.mc-stan.org/t/a-variable-from-function-section-does-not-exist/21137/2 "2021-03-06T08:30:34Z")

</div>

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); 
}

```

---

<div class="post-metadata">

**Author:** ![Jimwad](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@Jimwad](https://discourse.mc-stan.org/u/Jimwad)\
**Post date:** [March 6, 2021, 9:36am UTC](https://discourse.mc-stan.org/t/a-variable-from-function-section-does-not-exist/21137/3 "2021-03-06T09:36:34Z")

</div>

Thank you for pointing that fatal problem, however after that, I still got the same error as before.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [March 6, 2021, 10:13am UTC](https://discourse.mc-stan.org/t/a-variable-from-function-section-does-not-exist/21137/4 "2021-03-06T10:13:21Z")

</div>

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.

```stan
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); 
}

```
