PARSER EXPECTED: whitespace to end of file

I have the following data and code in R

> str(stan_data_list)
List of 4
 $ N: int 3998
 $ K: int 13
 $ y: int [1:3998] 6 5 10 10 8 3 8 9 9 5 ...
 $ X: num [1:3998, 1:13] 1 1 1 1 1 1 1 1 1 1 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr [1:3998] "1" "2" "3" "4" ...
  .. ..$ : chr [1:13] "hotlineyes" "ram8MB" "screen17in" "cpu50MHz" ...

> m.stan <- stan(model_code = "alex_lm.stan", 
               data = stan_data_list, iter = 1000, chains=1)

This returns the following error:

PARSER EXPECTED: whitespace to end of file.
FOUND AT line 1:

data {
  int N;  //nrows
  int K;  //ncols
  vector[N] y; //dim output
  matrix[N,K] X; //dim input

parameters {
  real beta0;
  vector[K] beta; //dim = input col
  real<lower=0> sigma; 

model {
  y ~ normal(beta0 + X*beta, sigma);

What are some possible sources of this error?


The slashes for comments go in the opposite direction. The parser thinks that’s code rather than a comment.

1 Like

I just updated the code. Thanks for pointing out the comment issue. I am getting the same error.

Darn, I was hoping that would fix it. I don’t see anything else wrong with the Stan program.

Is that the full output when you get error message? If there’s any more output that could help debug.

Also can you attach the actual file you’re using? Usually I prefer code in the post like you did, but in this case maybe there’s something weird with the file (I’ve seen that before).

Oh the problem isn’t the file but the call to stan(). Instead of the model_code argument you should use the file argument if you have a .stan file.

If using a string then model_code needs to be the full program as a string or the name of an object (not in quotes) to which the string has been assigned. But the recommended method is to use a file and avoid the model_code argument altogether.

You are a genius.

Thanks for including the R code with your post and not only the Stan program.