Error code:Error: unexpected '{' in "data {"

Hi guys, I’m running into an issue when trying Stan,
When trying to set the model, the Rstudio failed to run the following codes.

data {
int<lower=0> J;
array[J] real y;
array[J] real<lower=0> sigma;
parameters {
real mu; // population treatment effect
real<lower=0> tau; // standard deviation in treatment effects
vector[J] eta; // unscaled deviation from mu by school
transformed parameters {
vector[J] theta = mu + tau * eta; // school treatment effects
model {
target += normal_lpdf(eta | 0, 1); // prior log-density
target += normal_lpdf(y | theta, sigma); // log-likelihood

Error: unexpected ‘{’ in “data {”

it seems to be a unicode character issues, however, I could not find any way to solve this.

thank you for helping.

How are you trying to run the code? Are you using rstan or cmdstanr? With rstan you’ll need to store that code in a variable and use the model_code argument or store it in an external file and use the file argument. With cmdstanr you’ll need to store it in an external file and use the stan_file argument to cmdstan_model.

1 Like

I combed through the code carefully and didn’t see any issues that were obvious to me, so I saved this into a plain text document (.txt), changed the extension to .stan, and attempted to compile it with MatlabStan. It went through to completion without any issues. This may be a file type/extension issue or rstan itself may not like something, because the code itself seems fine.

I was been dumb, was ran into problem when save a stan file, and then tried to run it directly through R which gets this Error, because I have not able to actually change the text file extension to stan… But thanks, I have sorted out !