Getting Started
To analyze your data with Stan, you can either
Use Stan directly from within your preferred programming environment. This option lets you write custom models using the Stan language and then fit them to data.
Use a Stan based-modeling package - skip to High-level Stan Interfaces. This option lets you specify your model using formula-based syntax, as in R packages
lm
andlme4
, eliminating the need to learn how to write Stan programs.
Download and Install Stan
To compile and run Stan models directly from within R, Python, or Julia, select your OS, programming language interface, and preferred installation method in the grid below. For other programming environments, skip to Other Programming Environments
Prerequisites
How to Install
Please select interface and preferred package manager.
Conda simplifies the installation process by ensuring that all required libraries and tools are compatible with each other and is available for Linux, Mac, and Windows platforms.
You can either install miniconda, a free, minimal installer for conda or you can get the full Anaconda system which provides graphical installer wizards for MacOS and Windows users.
Julia users can install Conda.jl.
Other Programming Environments
Language | Tool | Description |
---|---|---|
R, Python | Google Colab | Prebuilt CmdStan binaries for Google Colab are available from the GitHub CmdStan releases page. Installing these binaries at the start of a Colab session is much faster than installing CmdStan during a Colab session. |
Mathematica | MathematicaStan | Stan interface for Mathematica. Available from its GitHub repository. |
MATLAB | MatlabStan | Installation instructions available on the MatlabStan wiki. |
Python | PyStan | Available via pip. Run command: python -m pip install pystan . |
As of Release 3.10.0, PyStan is no longer being actively supported.
Prerequisite: C++17 toolchain
Stan models are specified using the Stan language which are then compiled to executable programs that can be run on your data to perform inference and make predictions. To use Stan from within your preferred programming environment, you need a C++ toolchain comprised of a C++17 compiler and the GNU Make utility.
On Linux, these are bundled into the meta-package
build-essential
. To install, run command:
sudo apt-get install build-essential
On Mac, the Clang compiler and GNU Make are included with Xcode, the Apple toolset for software developers. Install Xcode from the App Store and then run command:
xcode-select --install
- On Windows 10, there are two ways to get a Stan-compatible C++ toolchain:
- Use the conda installer for CmdStan, CmdStanPy or CmdStanR or RStan, since these packages all include the required toolchain.
- Get Rtools which includes a C++17 compiler, GNU Make for windows, and a few Unix utilities.
CmdStanR users can call the internal functioncmdstanr:::install_toolchain
.
CmdStanPy provides both the functioncmdstanpy::get_cxx_toolchain
and command line scriptget_cxx_toolchain
.
Local CmdStan installations for CmdStanPy, CmdStanR, and Stan.jl
CmdStanPy, CmdStanR, and Stan.jl and require a local CmdStan installation. Both CmdStanPy and CmdStanR provide method install_cmdstan
to do this from within Python or R; and CmdStanPy also provides this as a command-line function. See the online documentation:
- CmdStanPy: install_cmdstan function
- CmdStanR: install_cmdstan function
- Stan.jl build instructions
The default installation location is in the user’s home directory and is named .cmdstan
(a hidden directory). This directory contains one or more versions of CmdStan.
Both CmdStanPy and CmdStanR provide the following functions:
rebuild_cmdstan
- rebuild the specified release. On Mac, often required after an Xcode update.show_versions
- shows all installed CmdStan versionscmdstan_path
- shows which version of CmdStan is being usedset_cmdstan_path
- specify which version of CmdStan to use.
Troubleshooting the Install
To help troubleshoot problems that arise when trying to use Stan, we provide the following summary of the chain of events in conditioning a model on data and doing inference:
- Compile model
- Stan compiler translates Stan file to C++ file
- C++ file is compiled to executable program, via GNU Make
- Run inference algorithm
- Interfaces run compiled executable program
- Compiled executable generates per-chain outputs
If the program contains syntax errors, these will be caught and reported by the Stan compiler (program stanc
). If the Stan program is successfully translated to C++, then it should compile; error messages from the C++ compiler indicate a problem with the C++ toolchain.
If a model fails to run or appears to run slowly, this is a strong indication that the model is poorly specified given the data. Consult the Stan User’s Guide or search the Stan Forums on Discourse
Common Points of failure; how to address them.
- Software download failed.
- workaround: check internet connectivity, disk space, and file permissions
- C++ components fail to compile
- Workaround: we highly recommend installing using conda to create a clean environment for Stan and its toolchain.
- Stan model fails to compile with error message about a ““.(PCH file)()
- Fix: for CmdStan based systems, rebuild CmdStan.
See CmdStan Guide section Troubleshooting the Installation for further details.
High-level Stan Interfaces
Language | Tool | Description |
---|---|---|
R | brms | Use extended lme4 -like formula syntax to specify and fit multivariate and multilevel models in Stan. (Requires CmdStanR and C++ compiler.) |
R | RStanArm | Provides stable, efficient Stan versions of R model-fitting packages. (Stan models are pre-compiled, no C++ compiler needed.) |
R | Rethinking | Accompanies the book and course materials for Statistical Rethinking, 2nd Ed by Richard McElreath. (Requires CmdStanR and C++ compiler.) |
Introductory Notebooks, Vignettes, and Tutorials
For more learning resources, see the Tutorials, Publications and Stan Case Studies pages.