Rstan - error

1,043 views
Skip to first unread message

Chunlei Zhang

unread,
Apr 6, 2017, 1:46:06 PM4/6/17
to Stan users mailing list
Hi there,

I received an error after run a stan model. It would be great, if anyone could help me to figure out the reason. Thanks for your time and consideration.

The below is my OS and R version.

1. Windows 7 Enterprise, 64 bit Operating System;

2. R version: R version 3.3.3 (2017-03-06), 64 bit.

The error message is the below.
> fit <- stan(model_code = "stan_code", data = schools_dat, iter = 1000, chains = 4)
SYNTAX ERROR, MESSAGE(S) FROM PARSER:


ERROR at line 1

  1:    stan_code
       ^

PARSER EXPECTED: <model declaration (or perhaps an earlier block)>
Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model '9b0dc7c5007252e0174a4f88f6c3c089' due to the above error.
>

The R code is the below (this is an example from the web site, https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started ).

library("rstan") 

rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())


stan_code <- "
data {
  int<lower=0> J; // number of schools 
  real y[J]; // estimated treatment effects
  real<lower=0> sigma[J]; // s.e. of effect estimates 
}
parameters {
  real mu; 
  real<lower=0> tau;
  real eta[J];
}
transformed parameters {
  real theta[J];
  for (j in 1:J)
    theta[j] = mu + tau * eta[j];
}
model {
  target += normal_lpdf(eta | 0, 1);
  target += normal_lpdf(y | theta, sigma);
}
"
 

schools_dat <- list(J = 8, 
                    y = c(28,  8, -3,  7, -1,  1, 18, 12),
                    sigma = c(15, 10, 16, 11,  9, 11, 10, 18)
)

fit <- stan(model_code = "stan_code", data = schools_dat, iter = 1000, chains = 4)



Thanks again,
Peter

Bob Carpenter

unread,
Apr 6, 2017, 2:09:32 PM4/6/17
to stan-...@googlegroups.com
"stan_code" is not a well-formed model. Did you meant
to say file="stan_code"? Usually we use the ".stan" suffix
on Stan files.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+...@googlegroups.com.
> To post to this group, send email to stan-...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Chunlei Zhang

unread,
Apr 6, 2017, 4:49:33 PM4/6/17
to Stan users mailing list
I changed the stan_code to scode, and use scode in the function like the below. Same errror message showed.

stan(model_code = "scode", data = schools_dat, iter = 1000, chains = 4) 


 Any new though would be appreciated. Should I install C++ compiler in order to execute Rstan function?


Thanks,
Chunlei

Chunlei Zhang

unread,
Apr 6, 2017, 4:55:45 PM4/6/17
to stan-...@googlegroups.com

Chunlei Zhang

unread,
Apr 6, 2017, 4:57:34 PM4/6/17
to Stan users mailing list
The code is an example from the web located at https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started, I do not understand why it did not work on my computer.

Bob Carpenter

unread,
Apr 6, 2017, 5:33:33 PM4/6/17
to stan-...@googlegroups.com
That is not an example from that page. There's no example
there using "model_code". The first example on that page
is this:

fit <- stan(file = '8schools.stan', data = schools_dat,
iter = 1000, chains = 4)

You'll see the argument is file and it's pointing to a file
containing a Stan program.

- Bob

Jonah Gabry

unread,
Apr 7, 2017, 1:25:08 PM4/7/17
to Stan users mailing list
If you want to use the model_code argument to pass the Stan program as a string (instead of using a separate file) then you can't put the name of the Stan program string in quotes. So 

fit <- stan(model_code = "scode", data = schools_dat, iter = 1000, chains = 4) 

won't work. It needs to be 

fit <- stan(model_code = scode, data = schools_dat, iter = 1000, chains = 4) 

We definitely recommend using a file instead of a string, but that should at least work. 

Jonah
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages