Stan program compiles in PyStan but doesn't compile in RStan

Hi Stan community! I’m an intermediate Stan user and first time forum poster. 99% of my Stan experience has been with PyStan, but for a current project I need to use RStan. I have Stan code I’ve developed locally and run in PyStan that works like a charm, but when I try to use the same Stan code in RStan it gives errors I’ve never seen before.

y ~ normal(beta0 + beta1 * exp(-D.^2 ./ r) * rep_vector(1, m), sigma);

The first error I get is:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
Variable “D.” does not exist.
 error in ‘model103d64bcfce11_thresholded_kernel’ at line 28, column 36
  -------------------------------------------------
    26:
    27:   // use vectorized operations to compute the kernel values,
    28:   y ~ normal(beta0 + beta1 * exp(-D.^2 ./ r^2) * rep_vector(1, m), sigma);
                                           ^
    29: }
  -------------------------------------------------
Error in stanc(file = file, model_code = model_code, model_name = model_name,  :
  failed to parse Stan model ‘thresholded-kernel’ due to the above error.

I was initially confused because the dot should be being parsed as part of the .^ operator. Wondering if there was an easy fix, I changed the code to the following:

y ~ normal(beta0 + beta1 * exp(-D .^ 2 ./ r) * rep_vector(1, m), sigma);

But that line gave a new error:

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
 error in ‘model103d65c8aff37_thresholded_kernel’ at line 28, column 32
  -------------------------------------------------
    26:
    27:   // use vectorized operations to compute the kernel values,
    28:   y ~ normal(beta0 + beta1 * exp(-D .^ 2 ./ r^2) * rep_vector(1, m), sigma);
                                       ^
    29: }
  -------------------------------------------------
PARSER EXPECTED: “(”
Error in stanc(file = file, model_code = model_code, model_name = model_name,  :
  failed to parse Stan model ‘thresholded-kernel’ due to the above error.

Does anyone know why this code in particular isn’t working in RStan? Again, the model compiles and runs with no issues in the latest version of PyStan.

I’m on R version 4.3.1 and RStan version 2.21.8, in case those are relevant.

1 Like

Try updating Rstan to 2.26 following the instructions here RStan Getting Started · stan-dev/rstan Wiki · GitHub

Before doing that, I tried compiling the code in CmdStanR and fitting the model there. It’s running slower than it did in PyStan, but at least it works. Will give updating Rstan a shot and report back!