Extensions

R

Some R extensions for Stan include the following:

  • shiny_stan: for interactive diagnostics

Screen shot of shiny_stan

  • loo: Provides approximate leave-one-out cross-validation statistics for model comparison.

  • bayesplot: diagnostic plots and other useful tools, though notable overlap with shiny_stan

Example plots from bayesplot

Stan functions

One can write their own Stan functions just like with R. Just start your model code with a functions {} block. Perhaps you will need something to make later code a little more efficient, or a specific type of calculation. You can create a custom function to suit your needs. An example function below standardizes a variable to have a mean of 0 and standard deviation of 1, or just center it if scale=0.

functions {
  vector stdized(int N, vector x, int scale) {
     vector[N] x_sc;

     x_sc = scale ? x-mean(x) : (x-mean(x))/sd(x);
     
     return x_sc;
  }
}

Presumably this capability will result in custom modules that are essentially the equivalent of R packages for Stan. However, at this time there doesn’t look to be much in this regard.

Other frameworks

Stan goes beyond R, so if you find yourself using other tools but still need the power of Stan, fret not.

  • CmdStan: shell, command-line terminal
  • PyStan: Python
  • StataStan: Stata
  • MatlabStan: MATLAB
  • Stan.jl: Julia
  • MathematicaStan: Mathematica