Declaring parameters in an "include" file

Thank you so much to the rstantools developers for the excellent package, with illustrative vignettes. This is really helping me start my R package that relies on Stan for Stage 1 of a 2-stage estimator.

I am trying to utilize the #include option for chunks of Stan syntax shared across files. I am having difficulty ascertaining the requirements:

  • Are quotation marks necessary around the file name? Or only when directories/files include spaces?
  • Is it necessary to specify the full or relative path under /inst/stan/ before the file name?

For example, if I have a file “sigma.stan” in the package directory /inst/stan/include/, which of these are best practice to point to it in my general model (located in /inst/stan/Main.stan)?

// assume it finds the file in /inst/stan/include/
#include sigma.stan

// with quotation marks?
#include "sigma.stan"

// specify full path to the file?
#include /inst/stan/include/sigma.stan

// specify relative path to the file? (relative to /inst/stan/, where larger model is found)
#include /include/sigma.stan

// path with quotation marks?
#include "/include/sigma.stan"
// or
#include "/inst/stan/include/sigma.stan"

I believe I have tried all of these options, using the rstan_create_package(path = 'rstanlm') example from the Step-by-Step vignette. The package compiles fine when I do not use #include statement to point to a file specifying the sigma parameter (as in the “lm.stan” file from the vignette). But when I put that parameter in its own file and #include it (in a copy called “lm2.stan”), then I get error messages saying the parameter does not exist

SYNTAX ERROR, MESSAGE(S) FROM PARSER:
   Variable "sigma" does not exist.
    error in 'model_lm2' at line 15, column 7
     -------------------------------------------------
       13:   intercept ~ normal(0, 1);
       14:   beta ~ normal(0, 1);
       15:   sigma ~ student_t(4, 1, .5) T[0,];
                 ^
       16:   y ~ normal(intercept + beta * x, sigma);
     -------------------------------------------------

My modified rstanlm package is available on GitHub: GitHub - TDJorgensen/rstanlm: Reproduce rstantools vignette to make reprex for questions

Thanks for any help/guidance.
Terrence

It’s a bit tricky, but you want the path with no quotations AND no space before the #include:

data {
  int<lower=1> N;
  vector[N] x;
  vector[N] y;
}
parameters {
  real intercept;
  real beta;
#include /include/sigma.stan
}
model {
  // ... priors, etc.
  intercept ~ normal(0, 1);
  beta ~ normal(0, 1);
  sigma ~ student_t(4, 1, .5) T[0,];
  y ~ normal(intercept + beta * x, sigma);
}

That did the trick, thanks @andrjohns !