Skip to content
This repository has been archived by the owner on Sep 10, 2023. It is now read-only.

Convert all existing models to Literate #59

Merged
merged 8 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,30 @@ jobs:
version: 1 # Latest stable release, for example, `1.5.3`.

- name: Build site
run: julia -e '
using Pkg; Pkg.add(["NodeJS", "Franklin"]);
using NodeJS; run(`$(npm_cmd()) install highlight.js`);
using Franklin;
Pkg.develop(path="."); Pkg.activate("."); Pkg.instantiate();
Franklin.optimize();
verify_links()' > build.log
run: |
julia -e '
using Pkg; Pkg.add(["NodeJS", "Franklin"]);
using NodeJS; run(`$(npm_cmd()) install highlight.js`);
using Franklin;
Pkg.develop(path="."); Pkg.activate("."); Pkg.instantiate();
Franklin.optimize();
verify_links()' > build.log;
cat build.log

- name: Deploy to secondary branch
# Only deploy when pushed to master.
if: ${{ github.event_name == 'push' }}
uses: peaceiris/actions-gh-pages@v3
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
publish_dir: ./__site

- name: Validate output
run: |
# A Franklin Warning means an error occurred when evaluating code.
if grep -1 "Franklin Warning" build.log; then
echo "Franklin reported a warning"
exit 1
else
echo "Franklin did not report a warning"
fi

8 changes: 7 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ version = "1.1.3"
[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Franklin = "713c75ef-9fc9-4b05-94a9-213340da978e"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
NodeJS = "2bd173c7-0d6d-553b-b6af-13a54713934c"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
RData = "df47a6cb-8c03-5eed-afd8-b6050d6c41da"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
StatsPlots = "f3b207a7-027a-5e70-b257-86293d7955fd"
Turing = "fce5fe82-541a-59a6-adf8-730c64b5f9a0"

[compat]
CSV = "0.8"
DataFrames = "0.22"
Distributions = "0.24"
Franklin = "0.10"
NodeJS = "1.1"
RData = "0.7"
StatsFuns = "0.9"
StatsPlots = "0.14"
Turing = "0.15"

2 changes: 2 additions & 0 deletions _css/jtd.css
Original file line number Diff line number Diff line change
Expand Up @@ -617,13 +617,15 @@ body { counter-reset: eqnum; }
display: inline-block;
} /* overwrite katex settings */

/* Not interesting for this site.
.katex-display::after {
counter-increment: eqnum;
content: "(" counter(eqnum) ")";
position: relative;
float: right;
padding-right: 5px;
}
*/

/* ==================================================================
CODE & HIGHLIGHT.JS
Expand Down
2 changes: 2 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ We have tried our best to let this site be without mistakes.
However, if you find a mistake on this website, please let us know.
The code for this website is available on [GitHub](https://github.com/StatisticalRethinkingJulia/TuringModels.jl).

\toc

### About the pages

Each page aims to contain all the code required to reproduce the results.
Expand Down
16 changes: 8 additions & 8 deletions models.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Before you look at the models below, you might want to look at the [Basic Exampl
- [m13.1](varying-intercepts-reedfrogs): Varying intercepts Reedfrogs
- [m13.1](varying-slopes-cafe): Varying slopes cafes
- [m13.2](multinomial-poisson): Multinomial Poisson regression
- [m13.3](varying-intercepts-admission): Varying intercepts admission decisions [not-implemented]
- [m13.3](varying-intercepts-admission): Varying intercepts admission decisions

## 1st Edition (2015)

Expand All @@ -31,15 +31,15 @@ Before you look at the models below, you might want to look at the [Basic Exampl
- [m8.2](wild-chain): Wild chain
- [m8.3](weakly-informative-priors): Weakly informative priors
- [m8.4](non-identifiable): Non-identifiable model
- [m10.3](chimpanzees): Chimpanzees [not-implemented]
- [m10.4](estimate-handedness-chimpanzees): Estimate handedness for each Chimpanzee [not-implemented]
- [m10.10](oceanic-tool-complexity): Oceanic tool complexity [not-implemented]
- [m10.yyt](admit-reject): Admit or reject [not-implemented]
- [m10.3](chimpanzees): Chimpanzees
- [m10.4](estimate-handedness-chimpanzees): Estimate handedness for each Chimpanzee
- [m10.10](oceanic-tool-complexity): Oceanic tool complexity
- [m10.yyt](admit-reject): Admit or reject
- [m11.5](beta-binomial): Beta-binomial
- [m11.7](multinomial-poisson): Multinomial Poisson regression
- [m12.1](varying-intercepts-reedfrogs): Varying intercepts Reedfrogs
- [m13.4](ignoring-gender-admit): Ignoring gender for admittance
- [m13.6](multivariate-chimpanzees-priors): Multivariate Chimpanzees priors [not-implemented]
- [m13.6nc](non-centered-chimpanzees): Non-centered Chimpanzees [not-implemented]
- [m13.7](spatial-autocorrelation-oceanic): Spatial autocorrelation in Oceanic tools [not-implemented]
- [m13.6](multivariate-chimpanzees-priors): Multivariate Chimpanzees priors
- [m13.6nc](non-centered-chimpanzees): Non-centered Chimpanzees
- [m13.7](spatial-autocorrelation-oceanic): Spatial autocorrelation in Oceanic tools
- [m14.1](varying-slopes-cafe): Varying slopes cafes
35 changes: 2 additions & 33 deletions models/admit-reject.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
+++
title = "Admit or reject"
showall = true
+++

```
using TuringModels, StatsFuns

delim = ';'
d = CSV.read(joinpath(@__DIR__, "..", "..", "data", "UCBadmit.csv"), DataFrame; delim);
size(d) # Should be 12x5

@model m_pois(admit, reject) = begin
α₁ ~ Normal(0,100)
α₂ ~ Normal(0,100)

for i ∈ 1:length(admit)
λₐ = exp(α₁)
λᵣ = exp(α₂)
admit[i] ~ Poisson(λₐ)
reject[i] ~ Poisson(λᵣ)
end
end;

chns = sample(m_pois(d[:, :admit], d[:, :reject]), Turing.NUTS(0.65), 1000);

# Rethinking/CmdStan result

m_10_yyt_result = "
mean sd 5.5% 94.5% n_eff Rhat
a1 4.99 0.02 4.95 5.02 2201 1
a2 5.44 0.02 5.41 5.47 2468 1
";

# Describe the draws

chns |> display
```
\literate{/scripts/admit-reject.jl}
61 changes: 5 additions & 56 deletions models/beta-binomial.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
+++
title = "Beta-binomial"
showall = true
+++

In Rethinking 2nd Edition, this model is defined as
This beta-binomial model is `m11.5` in Statistical Rethinking 1st Edition.
In Statistical Rethinking 2nd Edition it is `m12.1` and defined as

$$
\begin{aligned}
A_i &\sim \text{BetaBinomial}(N_i, \overline{p}_i, \theta) \\
\text{logit}(\overline{p}_i) &= \alpha_\text{GID}[i] \\
\text{logit}(\overline{p}_i) &= \alpha_{\text{GID}[i]} \\
\alpha_j &\sim \text{Normal}(0, 1.5) \\
\theta &= \phi + 2 \\
\phi &\sim \text{Exponential(1)}
Expand All @@ -16,58 +18,5 @@ $$

\toc

## Data
\literate{/scripts/beta-binomial.jl}

```julia:data
using DataFrames
using TuringModels # hide

import CSV

data_path = joinpath(TuringModels.project_root, "data", "UCBadmit.csv")
df = CSV.read(data_path, DataFrame; delim=';')

@assert size(df) == (12, 5) # hide
write_csv(name, data) = CSV.write(joinpath(@OUTPUT, "$name.csv"), data) # hide
write_csv("data", df) # hide
```
\output{data}

\tableinput{}{./code/output/data.csv}

## Model
```julia:model
using StatsFuns: logistic
using Turing

@model function m11_5(admit, applications)
θ ~ truncated(Exponential(1), 0, Inf)
α ~ Normal(0, 2)

# alpha and beta for the BetaBinomial must be provided.
# The two parameterizations are related by
# alpha = prob * theta, and beta = (1-prob) * theta.
# See https://github.com/rmcelreath/rethinking/blob/master/man/dbetabinom.Rd

prob = logistic(α)
alpha = prob * θ
beta = (1 - prob) * θ
admit .~ BetaBinomial.(applications, alpha, beta)
end

model = m11_5(df.admit, df.applications)
chains = sample(model, NUTS(0.65), 1000)
```
\output{model}

## Output

\defaultoutput{}

## Original output

```
mean sd 5.5% 94.5% n_eff Rhat
theta 2.74 0.96 1.43 4.37 3583 1
a -0.37 0.31 -0.87 0.12 3210 1
```
37 changes: 4 additions & 33 deletions models/chimpanzees.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,10 @@
+++
title = "Chimpanzees"
showall = true
+++

```
using TuringModels, StatsFuns
This is the third Chimpanzees model (`m10.3`) in Statistical Rethinking Edition 1.

delim=';'
d = CSV.read(joinpath(@__DIR__, "..", "..", "data", "chimpanzees.csv"), DataFrame; delim);
size(d) # Should be 504x8
\toc

# pulled_left, condition, prosoc_left
@model m10_3(y, x₁, x₂) = begin
α ~ Normal(0, 10)
βp ~ Normal(0, 10)
βpC ~ Normal(0, 10)

logits = α .+ (βp .+ βpC * x₁) .* x₂
y .~ BinomialLogit.(1, logits)
end;

chns = sample(m10_3(d[:,:pulled_left], d[:,:condition], d[:,:prosoc_left]),
Turing.NUTS(0.65), 2000);

# Rethinking result

m_10_03t_result = "
Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
a 0.05 0.13 -0.15 0.25 3284 1
bp 0.62 0.22 0.28 0.98 3032 1
bpC -0.11 0.26 -0.53 0.29 3184 1
";

# Describe the draws

chns |> display

# End of m10.03t.jl
```
\literate{/scripts/chimpanzees.jl}
50 changes: 4 additions & 46 deletions models/estimate-handedness-chimpanzees.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,11 @@
+++
title = "Estimate handedness for each Chimpanzee"
showall = true
+++

```
using TuringModels, StatsFuns
This is model `m10.4` in Statistical Rethinking Edition 1.

delim=';'
d = CSV.read(joinpath(@__DIR__, "..", "..", "data", "chimpanzees.csv"), DataFrame; delim);
size(d) # Should be 504x8
\toc

# pulled_left, actors, condition, prosoc_left
@model m10_4(y, actors, x₁, x₂) = begin
# Number of unique actors in the data set
N_actor = length(unique(actors))
\literate{/scripts/estimate-handedness-chimpanzees.jl}

# Set an TArray for the priors/param
α ~ filldist(Normal(0, 10), N_actor)
βp ~ Normal(0, 10)
βpC ~ Normal(0, 10)

logits = α[actors] .+ (βp .+ βpC * x₁) .* x₂
y .~ BinomialLogit.(1, logits)
end;

chns = sample(m10_4(d[:,:pulled_left], d[:,:actor],d[:,:condition],
d[:,:prosoc_left]), Turing.NUTS(0.65), 1000);

# Rethinking/CmdStan results

m_10_04s_result = "
Iterations = 1:1000
Thinning interval = 1
Chains = 1,2,3,4
Samples per chain = 1000

Empirical Posterior Estimates:
Mean SD Naive SE MCSE ESS
a.1 -0.74503184 0.26613979 0.0042080396 0.0060183398 1000
a.2 10.77955494 5.32538998 0.0842018089 0.1269148045 1000
a.3 -1.04982353 0.28535997 0.0045119373 0.0049074219 1000
a.4 -1.04898135 0.28129307 0.0044476339 0.0056325117 1000
a.5 -0.74390933 0.26949936 0.0042611590 0.0052178124 1000
a.6 0.21599365 0.26307574 0.0041595927 0.0045153523 1000
a.7 1.81090866 0.39318577 0.0062168129 0.0071483527 1000
bp 0.83979926 0.26284676 0.0041559722 0.0059795826 1000
bpC -0.12913322 0.29935741 0.0047332562 0.0049519863 1000
";

# Describe the draws

chns |> display
```
1 change: 0 additions & 1 deletion models/ignoring-gender-admit.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
+++
title = "Ignoring gender for admittance"
showall = true
reeval = true
+++

\literate{/scripts/ignoring-gender-admit.jl}
Loading