In Statistical Rethinking Edition 1, the model below is called m10.4
.
import CSV
using TuringModels: project_root
using DataFrames
path = joinpath(project_root, "data", "chimpanzees.csv")
df = CSV.read(path, DataFrame; delim=';');
using Turing
@model function m12_4(pulled_left, actor, condition, prosoc_left)
# Total num of y
N = length(pulled_left)
# Separate σ priors for each actor
σ_actor ~ truncated(Cauchy(0, 1), 0, Inf)
# Number of unique actors in the data set
N_actor = length(unique(actor)) #7
# Vector of actors (1,..,7) which we'll set priors on
α_actor ~ filldist(Normal(0, σ_actor), N_actor)
# Prior for intercept, prosoc_left, and the interaction
α ~ Normal(0, 10)
βp ~ Normal(0, 10)
βpC ~ Normal(0, 10)
logitp = α .+ α_actor[actor] .+
(βp .+ βpC * condition) .* prosoc_left
pulled_left .~ BinomialLogit.(1, logitp)
end;
chns = sample(
m12_4(df.pulled_left, df.actor, df.condition, df.prosoc_left),
NUTS(),
1000
)
Chains MCMC chain (1000×23×1 Array{Float64, 3}):
Iterations = 501:1:1500
Number of chains = 1
Samples per chain = 1000
Wall duration = 20.92 seconds
Compute duration = 20.92 seconds
parameters = σ_actor, α_actor[1], α_actor[2], α_actor[3], α_actor[4], α_actor[5], α_actor[6], α_actor[7], α, βp, βpC
internals = lp, n_steps, is_accept, acceptance_rate, log_density, hamiltonian_energy, hamiltonian_energy_error, max_hamiltonian_energy_error, tree_depth, numerical_error, step_size, nom_step_size
Summary Statistics
parameters mean std naive_se mcse ess rhat ess_per_sec
Symbol Float64 Float64 Float64 Float64 Float64 Float64 Float64
σ_actor 2.3051 0.9881 0.0312 0.0743 158.7163 1.0002 7.5861
α_actor[1] -1.2867 1.0601 0.0335 0.0965 92.5074 1.0069 4.4215
α_actor[2] 4.1051 1.6267 0.0514 0.0756 379.9454 0.9999 18.1601
α_actor[3] -1.5682 1.0706 0.0339 0.0993 88.9685 1.0064 4.2524
α_actor[4] -1.5764 1.0534 0.0333 0.0977 92.9265 1.0050 4.4416
α_actor[5] -1.2777 1.0665 0.0337 0.0971 95.7129 1.0049 4.5748
α_actor[6] -0.3330 1.0581 0.0335 0.0966 92.7081 1.0058 4.4311
α_actor[7] 1.2000 1.0951 0.0346 0.0998 93.1600 1.0074 4.4527
α 0.5634 1.0382 0.0328 0.0967 89.5140 1.0068 4.2785
βp 0.8177 0.2438 0.0077 0.0093 732.8422 1.0019 35.0274
βpC -0.1263 0.2853 0.0090 0.0101 792.4163 0.9998 37.8748
Quantiles
parameters 2.5% 25.0% 50.0% 75.0% 97.5%
Symbol Float64 Float64 Float64 Float64 Float64
σ_actor 1.1069 1.6692 2.0581 2.6542 4.8785
α_actor[1] -4.1357 -1.6900 -1.1658 -0.6658 0.5522
α_actor[2] 1.5941 3.0604 3.7703 4.8946 8.1172
α_actor[3] -4.3841 -2.0164 -1.4599 -0.9437 0.1821
α_actor[4] -4.3581 -2.0031 -1.4407 -0.9603 0.1654
α_actor[5] -4.0121 -1.6895 -1.1536 -0.7022 0.5674
α_actor[6] -3.1881 -0.7475 -0.1801 0.2731 1.4832
α_actor[7] -1.4838 0.7121 1.3188 1.8460 3.0982
α -1.2412 0.0050 0.4453 0.9926 3.2768
βp 0.3668 0.6379 0.8095 0.9873 1.2937
βpC -0.7055 -0.3144 -0.1180 0.0614 0.4223
using StatsPlots
StatsPlots.plot(chns)
"/home/runner/work/TuringModels.jl/TuringModels.jl/__site/assets/models/varying-intercepts-chimpanzees/code/output/chns.svg"
m124rethinking = "
Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
a_actor[1] -1.13 0.95 -2.62 0.27 2739 1
a_actor[2] 4.17 1.66 1.80 6.39 3958 1
a_actor[3] -1.44 0.95 -2.90 -0.02 2720 1
a_actor[4] -1.44 0.94 -2.92 -0.04 2690 1
a_actor[5] -1.13 0.94 -2.58 0.31 2727 1
a_actor[6] -0.19 0.94 -1.64 1.25 2738 1
a_actor[7] 1.34 0.97 -0.09 2.87 2889 1
a 0.42 0.93 -1.00 1.81 2622 1
bp 0.83 0.26 0.41 1.25 8594 1
bpC -0.13 0.30 -0.62 0.34 8403 1
sigma_actor 2.26 0.94 1.07 3.46 4155 1
";