|
2 | 2 |
|
3 | 3 | This tutorial explains the steps required for constructing and plotting prior and posterior predictive distributions of a sequential sampling models (SSMs). The primary function we will be using is `predict_distribution`, which allows you to generate prior or posterior predictive distributions from a given model. |
4 | 4 |
|
| 5 | +## Full Code |
| 6 | + |
| 7 | +You can reveal copy-and-pastable version of the full code by clicking the ▶ below. |
| 8 | + |
| 9 | +```@raw html |
| 10 | +<details> |
| 11 | +<summary><b>Show Full Code</b></summary> |
| 12 | +``` |
| 13 | +```julia |
| 14 | +using Distributions |
| 15 | +using Plots |
| 16 | +using Random |
| 17 | +using SequentialSamplingModels |
| 18 | +using Turing |
| 19 | +Random.seed!(1124) |
| 20 | + |
| 21 | +n_samples = 50 |
| 22 | +rts = rand(Wald(ν=1.5, α=.8, τ=.3), n_samples) |
| 23 | + |
| 24 | +@model function wald_model(rts) |
| 25 | + ν ~ truncated(Normal(1.5, 1), 0, Inf) |
| 26 | + α ~ truncated(Normal(.8, 1), 0, Inf) |
| 27 | + τ = 0.3 |
| 28 | + rts ~ Wald(ν, α, τ) |
| 29 | + return (;ν, α, τ) |
| 30 | +end |
| 31 | + |
| 32 | +model = wald_model(rts) |
| 33 | + |
| 34 | +prior_chain = sample(model, Prior(), 1000) |
| 35 | + |
| 36 | +pred_model = predict_distribution(Wald; model, func=mean, n_samples) |
| 37 | +prior_preds = generated_quantities(pred_model, prior_chain) |
| 38 | + |
| 39 | +post_chain = sample(model, NUTS(1000, .85), 1000) |
| 40 | +post_preds = generated_quantities(pred_model, post_chain) |
| 41 | + |
| 42 | +histogram(prior_preds[:], xlims=(0,4), xlabel="Mean RT", ylabel="Density", norm=true, |
| 43 | + color=:grey, label="prior", grid=false) |
| 44 | +histogram!(post_preds[:], alpha=.7, color=:darkred, norm=true, label="posterior", grid=false) |
| 45 | +vline!([mean(rts)], linestyle=:dash, color=:black, linewidth=2, label="data") |
| 46 | +``` |
| 47 | +```@raw html |
| 48 | +</details> |
| 49 | +``` |
| 50 | + |
5 | 51 | # Example |
6 | 52 | The first step is to load the required packages and set the seed for the random number generator. |
7 | 53 |
|
|
0 commit comments