As stated many times by the author in his online lectures, StatisticalRethinking is a hands-on course. This project is intended to assist with the hands-on aspect of learning the key ideas in StatisticalRethinking.
SR2StanPluto is a Julia project that uses Pluto notebooks for this purpose. Each notebook demonstrates Julia versions of code snippets
and mcmc models
contained in the R package "rethinking" associated with the book Statistical Rethinking by Richard McElreath.
This Julia project uses Stan (the cmdstan
executable) as the underlying mcmc implementation.
From v5 onwards the basis will no longer be StatisticalRethinking.jl but RegressionAndOtherStories.jl. Both packages have very similar content, but StatisticalRethinking.jl uses Plots.jl while RegressionAndOtherStories.jl is using Makie.jl.
Tagged version 4.2.0 is the last more or less complete set of scripts covering the old
chapters 1 to 11.
To (locally) reproduce and use this project, do the following:
- Download this project from Github and move to the downloaded directory, e.g.:
$ cd .julia/dev
$ git clone https://github.com/StatisticalRethinkingJulia/SR2StanPluto.jl SR2StanPluto
$ cd SR2StanPluto
The next step assumes your basic
Julia environment includes Pkg
and Pluto
.
- Start a Pluto notebook server.
$ cd notebooks
$ julia
julia> using Pluto
julia> Pluto.run()
-
A Pluto page should open in a browser.
-
Select a notebook in the
open a file
entry box, e.g. type./
and select a notebook.
All "rethinking" data files are stored and maintained in StatisticalRethinking.jl and can be accessed via sr_datadir(...)
. See notebooks/00-Preface.jl
for an example.
In scripts, for naming models and results of simulations I tend to use:
Models and results:
- stan5_1 : Stan language program
- m5_1s : The sampled StanSample model
- q5_1s : Stan quap model (NamedTuple similar to Turing)
Draws:
- chns5_1s : MCMCChains object (4000 samples from 4 chains)
- part5_1s : Stan samples (Particles notation)
- quap5_1s : Quap samples (Particles notation)
- nt5_1s : NamedTuple with samples values
- ka5_1s : KeyedArray object (see AxisArrays.jl)
- da5_1s : DimArray object (see DimensionalData.jl)
- st5_1s : StanTable 0bject (see Tables.jl)
- i5_1s : InferenceObjects (see InferenceObjects.jl)
The default for read_samples(m1_1s)
is a StanTable chains object.
Results as a DataFrame:
- prior5_1s_df : Prior samples (DataFrame)
- post5_1s_df : Posterior samples (DataFrame)
- quap5_1s_df : Quap approximation to posterior samples (DataFrame)
- pred5_1s_df : Posterior predictions (DataFrame)
The s
at the end indicates Stan.
By default read_samples(m5_1s)
returns a StanTable with the results. In general
it is safer to specify the desired format, i.e. read_samples(m5_1s, :table)
as
the Julia eco-sytem is still evolving rapidly with new options.
Using read_samples(m5_1s, :...)
makes it easy to convert samples to other formats.
In version 5 I expect to mainly use the output_formats :dataframe, :namedtuple.
For InferenceObjects.jl there is a separate function inferencedata(m1_1s)
.
See the Notebook_Examples in Stan.jl for an example Pluto notebook.
SR2StanPluto.jl is compatible with the 2nd edition of the book.
StructuralCausalModels.jl ans ParetoSmoothedImportanceSampling.jl are included as experimental dependencies in the StatisticalRethinking.jl v3+ package. Both definitely WIP! See also below version 5 info.
Any feedback is appreciated. Please open an issue.
Of course, without the excellent textbook by Richard McElreath, this project would not have been possible. The author has also been supportive of this work and gave permission to use the datasets.
This repository and format is derived from previous versions of StatisticalRethinking.jl, work by Karajan, Max Lapan and many other contributors.
- Switch to (GL)Makie.jl, Graphs.jl, CausalInference.jl, InferenceObjects.jl (and more probably).
- SR2StanPluto v4+ requires StatisticalRethinking v4+.
- Many additions for 2nd edition of Statistical Rethinking book.
- Version 3 switched to using StanSample and StanQuap
- Initial versions (late Nov 2020).