RMarkdown Chunks and Stan

Hi Everyone,

Longtime lurker here. Maybe you can help me with a problem. I’ve been trying to figure out how to use the Rmarkdown Stan chunks in Rstudio.

I know the recently added support for Stan chunks but I cannot seem to get them to work correctly.

If I make a chunk with a model like so:

data {
  int K;
  int N;
  int D;
  int y[N];
  vector[D] x[N];
}
parameters {
  matrix[K,D] beta;
}
model {
for (k in 1:K)
  beta[k] ~ normal(0, 5);
for (n in 1:N)
  y[n] ~ categorical_logit(beta * x[n]);

I am returned the following error:

PROGRAM ERROR, MESSAGE(S) FROM PARSER:

Error in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  failed to parse Stan model 'stan-1b544013772e' due to the above error.

Has anyone got any experience using Stan chunks and could help me out?
All input is appreciated.

It looks like there’s a missing error message, but the obvious thing is that it’s missing an ending } (for the model block). Maybe that’s the error?

1 Like

I am not a clever man.

Thank you very much for the help. It is much appreciated.

Since i noticed the chunk header doesn’t get displayed.
It’s:

{stan output.var="model1"}