Good afternoon. I am trying to fit a state space model in stan. My STAN code is contained below.

```
data {
// Defines data to be passed in to stan model
int<lower = 0> nday;
real Clit_obs[nday];
real Cf_obs[nday];
real Cw_obs[nday];
real Cr_obs[nday];
real Csom_obs[nday];
real mint[nday];
real maxt[nday];
real Nit;
real lat;
real yearday[nday];
real rad[nday];
real ca[nday];
real LMA;
}
parameters {
// Defines p1 - p11, parameters governing the exchanges
real<lower = pow(10,-6), upper = .01> p1;
real<lower = .2, upper = .7> p2;
real<lower = .01, upper = .5> p3;
real<lower = .01, upper = .5> p4;
real<lower = pow(10, -4), upper = .1> p5;
real<lower = pow(10, -6), upper = .01> p6;
real<lower = pow(10, -6), upper = .01> p7;
real<lower = pow(10, -5), upper = .1> p8;
real<lower = pow(10, -6), upper = .01> p9;
real<lower = .05, upper = .2> p10;
real<lower = 5, upper = 20> p11;
// Defines standard deviations for process model
real<lower = 0> sd_cf;
real<lower = 0> sd_cw;
real<lower = 0> sd_cr;
real<lower = 0> sd_csom;
real<lower = 0> sd_clit;
// Defines standard deviations for observational error
real<lower = 0> sd_cf_obs;
real<lower = 0> sd_cw_obs;
real<lower = 0> sd_cr_obs;
real<lower = 0> sd_clit_obs;
real<lower = 0> sd_csom_obs;
// Defines latent variables
real Clit[nday];
real Cr[nday];
real Cw[nday];
real Cf[nday];
real Csom[nday];
}
model {
// Defines values to be used when calculating G
real G;
real lai;
real mult;
real dayl;
real cps;
real e0;
real ci;
real gs;
gs = 4.569279;
// Initial condition priors
Cf[1] ~ normal(100, 2);
Cw[1] ~ normal(9200, 25);
Cr[1] ~ normal(100, 2);
Clit[1] ~ normal(20, 2);
Csom[1] ~ normal(11000, 25);
// Process error standard deviation priors
sd_cf ~ cauchy(0, 10);
sd_cw ~ cauchy(0, 10);
sd_csom ~ cauchy(0, 10);
sd_clit ~ cauchy(0, 10);
sd_cr ~ cauchy(0, 10);
// Observational error standard deviation priors
sd_cf_obs ~ cauchy(0, 10);
sd_cw_obs ~ cauchy(0, 10);
sd_csom_obs ~ cauchy(0, 10);
sd_clit_obs ~ cauchy(0, 10);
sd_cr_obs ~ cauchy(0, 10);
for (i in 2:nday){
// Calculates G
lai = Cf_obs[i-1]/LMA > .1 ? Cf_obs[i-1]/LMA : .1;
mult = tan(-23.4*cos(360*(yearday[i] + 10)/365 *pi()/180 )*pi()/180)*tan(lat);
dayl = fabs(mult) <= 1 ? 24*acos(-mult)/pi() : (mult >= 1 ? 24 : 0);
e0 = (7.19298*pow(lai, 2)) / (pow(lai,2) + 2.1001) ;
ci = .5*(ca[i] + (-204.6453) - lai*Nit / (4.569279) *p11*exp(.011136*maxt[i]) + pow(pow(ca[i] + (-204.6453) - lai*Nit / (4.569279)
*p11*exp(.011136*maxt[i]), 2) - 4*(ca[i]*-204.6453 - 4.22273*lai*Nit / (4.569279) *p11 *exp(.011136*maxt[i])), .5));
cps = e0*rad[i]*gs*(ca[i] - ci) / (e0*rad[i] + gs*(ca[i] - ci));
G = cps*(.0156935*dayl + .0453194);
// Distributions of latent states
Cf[i] ~ normal((1-p5)*Cf[i-1] + G*(1-p2)*p3, sd_cf);
Cw[i] ~ normal((1-p6)*Cw[i-1] + G*(1-p2)*(1-p3)*(1-p4), sd_cw);
Cr[i] ~ normal((1-p7)*Cr[i-1] + G*(1-p2)*(1-p3)*p4, sd_cr);
Clit[i] ~ normal(Cf[i-1]*p5 + Cr[i-1]*p7 + (1 - (.5*exp(p10*.5*(mint[i] + maxt[i])))*(p8 + p1))*Clit[i-1], sd_clit);
Csom[i] ~ normal(p6*Cw[i-1] + .5*exp(p10*.5*(mint[i] + maxt[i]))*p1*Clit[i-1] +
(1 - .5*exp(p10*.5*(mint[i] + maxt[i]))*p9)*Csom[i-1], sd_csom);
}
for (i in 1:nday){
// Observed distributions
Cf_obs[i] ~ normal(Cf[i], sd_cf_obs);
Cw_obs[i] ~ normal(Cw[i], sd_cw_obs);
Cr_obs[i] ~ normal(Cr[i], sd_cr_obs);
Clit_obs[i] ~ normal(Clit[i], sd_clit_obs);
Csom_obs[i] ~ normal(Csom[i], sd_csom_obs);
}
}
```

When I run this, I get the following error:

Error in (function (classes, fdef, mtable) :

unable to find an inherited method for function ‘get_stanmodel’ for signature ‘“logical”’

The data I am using was all simulated by me, so I was able to write a simplified version of the model where I removed p11 and directly passed STAN the G variable. This ran without any problems, so I am assuming that the error is stemming from calculating G (lines 86-93) in my code. I only started using STAN yesterday, so I am unsure what is causing this error and its possible I am overlooking something or unfamiliar with the syntax.

Any help would be appreciated!