I am new to this, while replicating the error of the code, i typed the function part and run it, it gives this outcome:
> functions {
Error: unexpected '{' in "functions {"
Please what can I do?
functions {
real[] sir(real t, real[] y, real[] theta,
real[] x_r, int[] x_i) {
real S = y[1];
real I = y[2];
real R = y[3];
real N = x_i[1];
real beta = theta[1];
real sigma = theta[2];
#TODO: fill it in.
real dS_dt =
real dI_dt =
real dR_dt =
return {dS_dt, dI_dt, dR_dt};
}
}
If you want to return multiple values, you first need to package them into a vector such as: v = [dS_dt, dI_dt, dR_dt] and then return v. The use of curly brackets after return is going to result in an error. However, even if you assign the vector for dS_dt, dI_dt, and dR_dt, it will result in a new error until you complete their assignment statements (i.e. real dS_dt =, real dI_dt =, and real dR_dt =)
What version of Stan are you using? If I put dummy values in to the TODO section, it compiles fine with 2.30 – newer versions you probably need to rewrite the arrays from real[] to array[] real
Thank you for your response:
Every other lines runs:
write.csv(influenza_england_1978_school,file="mydata.csv")
library(outbreaks)
library(tidyverse)
head(influenza_england_1978_school)
theme_set(theme_bw())
ggplot(data = influenza_england_1978_school) +
geom_point(mapping = aes(x = date, y = in_bed)) +
labs(y = "Number of students in bed")
library(rstan)
library(gridExtra)
rstan_options (auto_write = TRUE)
options (mc.cores = parallel::detectCores ())
Except for this:
functions {
real[] sir(real t, real[] y, real[] theta,
real[] x_r, int[] x_i) {
real S = y[1];
real I = y[2];
real R = y[3];
real N = x_i[1];
real beta = theta[1];
real gamma = theta[2];
real dS_dt = -beta * I * S / N;
real dI_dt = beta * I * S / N - gamma * I;
real dR_dt = gamma * I;
return {dS_dt, dI_dt, dR_dt};
}
}
Once I run it, it gives
> functions {
Error: unexpected '{' in "functions {"
functions {
vector sir(real t , vector y, array[] real theta, array[] real x_i) {
vector[3] dydt;
real S = y[1];
real I = y[2];
real R = y[3];
real N = x_i[1];
real beta = theta[1];
real gamma = theta[2];
dydt[1] = -beta * I * S / N;
dydt[2] = beta * I * S / N - gamma * I;
dydt[3] = gamma * I;
return dydt;
}
}
I think you’re following old code that’s not compatible with Stan’s current approach of declaring variables. For instance, real[] is not accepted anymore. It should be either vector or array[] real.