All Projects → DanOvando → learn-stan

DanOvando / learn-stan

Licence: MIT license
A practical introduction to modeling in Stan

Programming Languages

HTML
75241 projects
javascript
184084 projects - #8 most used programming language
r
7636 projects
Stan
76 projects
CSS
56736 projects

TL:DR

The slide for this workshop are available here

The code for the slides lives in slides.Rmd

There are also a few example scripts, for example hake_schaefer_model.R shows how to fit a Schaefer model given catch and CPUE data from a hake fishery.

A Practical Introduction to Stan

The goal of this repo is to get users comfortable writing, diagnosing, and using Stan models. I assume that if you’re reading this you know you want to do Bayesian modeling and you’re interested in learning how to do it in Stan. If you’re not quite there yet, some of the terminology in here may be confusing, and I strongly recommend

Recommended Books

Online Resources

This workshop also assumes you work in or are at least comfortable with R.

There are lots of more detailed and technical resources for learning Stan out there, and any user interested in using Stan in their research should be sure to read further to really understand what Stan is doing and why (the official documentation is very good, and Monnahan et al. 2016 provides a great introduction to Stan for those with an Ecological bent).

However, many of the available resources can be daunting to people unfamiliar with the Stan, or omit key steps in the “ok, but how do I actually do this” side of things. This workshop is intended to help users go from raw data to model fitting with real data and real use cases. This document is not intended to be a technically perfect explanation or example of the use of Stan, but rather a step-by-step example of how I at least have dealt with many of the kinds of problems that practical users encounter in using Stan (i.e. explanations are intended to be clear, not necessarily precise). Hopefully you can use this as a foundation then for building more rigorous analyses of your in in R and Stan. (Constructive) Comments and suggestions appreciated!

Setup

Installing Stan

Instructions for installing rstan are provided in the following wiki

https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started

It doesn’t matter if you want to install the development or official release.

Stan is a compiled language, meaning that there is some behind-the-scenes setup to your C++ toolchain that needs to be done so your computer can handle it.

Follow these instructions prior to trying to install rstan. The wiki contains links to instructions for setting up your C++ toolchain up here.

The instructions will vary depending on what operating system you use, and even the version of that operating system, and the version of R you are using. If you’re usually generally up-to-date systems this should be pretty straightforward, but the older the system you are running the trickier things might get, particularly if you are using R versions < 4.X

If you’ve never run any compiled languages before, the initial setup can take some time (e.g. downloading all the toolchain components on a Mac), so I recommend not trying to do this the morning of the workshop if your goal is to follow along.

Once you’ve configured your toolchain and installed rstan, you can test to make sure everything is working by opening R and running

library(rstan)
example(stan_model, package = "rstan", run.dontrun = TRUE)

Share and enjoy!

Installing dependencies

This repository is equipped with renv.

To install the required packages, simply install the renv package using

install.packages("renv")

And then after ensuring the working directory is appropriately set to the location of the project for this repository, running

renv::restore()

And following all prompts. To the best of its abilities renv will install the appropriate versions of the packges needed to run this repo, and will not affect other versions of your packages located in your main library.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].