TuringModels

Varying intercepts Reedfrogs

On page 402 of Edition 2, this model is defined as

SiBinomial(Ni,pi)logit(pi)=αTANK[i]αTANK[i]Normal(0,1.5) \begin{aligned} S_i &\sim \text{Binomial}(N_i, p_i) \\ \text{logit}(p_i) &= \alpha_\text{TANK[i]} \\ \alpha_\text{TANK[i]} &\sim \text{Normal}(0, 1.5) \end{aligned}
  1. Data
  2. Model
  3. Output
  4. Original output (Edition 1)

Data

import CSV
import Random

using DataFrames
using TuringModels

Random.seed!(1)

data_path = joinpath(TuringModels.project_root, "data", "reedfrogs.csv")
df = CSV.read(data_path, DataFrame; delim=';');
df.tank_index = 1:nrow(df)
df
48×6 DataFrame
 Row │ density  pred       size       surv   propsurv  tank_index
     │ Int64    InlineSt…  InlineSt…  Int64  Float64   Int64
─────┼────────────────────────────────────────────────────────────
   1 │      10  no         big            9  0.9                1
   2 │      10  no         big           10  1.0                2
   3 │      10  no         big            7  0.7                3
   4 │      10  no         big           10  1.0                4
   5 │      10  no         small          9  0.9                5
   6 │      10  no         small          9  0.9                6
   7 │      10  no         small         10  1.0                7
   8 │      10  no         small          9  0.9                8
   9 │      10  pred       big            4  0.4                9
  10 │      10  pred       big            9  0.9               10
  11 │      10  pred       big            7  0.7               11
  12 │      10  pred       big            6  0.6               12
  13 │      10  pred       small          7  0.7               13
  14 │      10  pred       small          5  0.5               14
  15 │      10  pred       small          9  0.9               15
  16 │      10  pred       small          9  0.9               16
  17 │      25  no         big           24  0.96              17
  18 │      25  no         big           23  0.92              18
  19 │      25  no         big           22  0.88              19
  20 │      25  no         big           25  1.0               20
  21 │      25  no         small         23  0.92              21
  22 │      25  no         small         23  0.92              22
  23 │      25  no         small         23  0.92              23
  24 │      25  no         small         21  0.84              24
  25 │      25  pred       big            6  0.24              25
  26 │      25  pred       big           13  0.52              26
  27 │      25  pred       big            4  0.16              27
  28 │      25  pred       big            9  0.36              28
  29 │      25  pred       small         13  0.52              29
  30 │      25  pred       small         20  0.8               30
  31 │      25  pred       small          8  0.32              31
  32 │      25  pred       small         10  0.4               32
  33 │      35  no         big           34  0.971429          33
  34 │      35  no         big           33  0.942857          34
  35 │      35  no         big           33  0.942857          35
  36 │      35  no         big           31  0.885714          36
  37 │      35  no         small         31  0.885714          37
  38 │      35  no         small         35  1.0               38
  39 │      35  no         small         33  0.942857          39
  40 │      35  no         small         32  0.914286          40
  41 │      35  pred       big            4  0.114286          41
  42 │      35  pred       big           12  0.342857          42
  43 │      35  pred       big           13  0.371429          43
  44 │      35  pred       big           14  0.4               44
  45 │      35  pred       small         22  0.628571          45
  46 │      35  pred       small         12  0.342857          46
  47 │      35  pred       small         31  0.885714          47
  48 │      35  pred       small         17  0.485714          48

Model

using Turing
using StatsFuns: logistic

@model function reedfrogs(Nᵢ, i, Sᵢ)
    αₜₐₙₖ ~ filldist(Normal(0, 1.5), length(i))
    pᵢ = logistic.(αₜₐₙₖ[i])
    Sᵢ .~ Binomial.(Nᵢ, pᵢ)
end;

Output

n = nrow(df)
model = reedfrogs(df.density, df.tank_index, df.surv)
chns = sample(model, NUTS(), 1000)
Chains MCMC chain (1000×60×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 7.57 seconds
Compute duration  = 7.57 seconds
parameters        = αₜₐₙₖ[16], αₜₐₙₖ[13], αₜₐₙₖ[28], αₜₐₙₖ[7], αₜₐₙₖ[44], αₜₐₙₖ[12], αₜₐₙₖ[6], αₜₐₙₖ[41], αₜₐₙₖ[45], αₜₐₙₖ[8], αₜₐₙₖ[47], αₜₐₙₖ[30], αₜₐₙₖ[17], αₜₐₙₖ[21], αₜₐₙₖ[42], αₜₐₙₖ[2], αₜₐₙₖ[29], αₜₐₙₖ[23], αₜₐₙₖ[1], αₜₐₙₖ[43], αₜₐₙₖ[4], αₜₐₙₖ[48], αₜₐₙₖ[26], αₜₐₙₖ[27], αₜₐₙₖ[40], αₜₐₙₖ[14], αₜₐₙₖ[31], αₜₐₙₖ[3], αₜₐₙₖ[36], αₜₐₙₖ[19], αₜₐₙₖ[9], αₜₐₙₖ[15], αₜₐₙₖ[46], αₜₐₙₖ[38], αₜₐₙₖ[25], αₜₐₙₖ[34], αₜₐₙₖ[24], αₜₐₙₖ[20], αₜₐₙₖ[22], αₜₐₙₖ[33], αₜₐₙₖ[37], αₜₐₙₖ[10], αₜₐₙₖ[32], αₜₐₙₖ[11], αₜₐₙₖ[35], αₜₐₙₖ[18], αₜₐₙₖ[39], αₜₐₙₖ[5]
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

    αₜₐₙₖ[1]    1.6678    0.7762     0.0245    0.0212   1320.6237    0.9990      174.5241
    αₜₐₙₖ[2]    2.4205    0.9295     0.0294    0.0243   1746.3398    0.9994      230.7836
    αₜₐₙₖ[3]    0.7386    0.6304     0.0199    0.0155   2152.3322    0.9991      284.4367
    αₜₐₙₖ[4]    2.4057    0.8943     0.0283    0.0226   1723.3183    0.9990      227.7413
    αₜₐₙₖ[5]    1.7260    0.7378     0.0233    0.0204   1411.7927    1.0005      186.5723
    αₜₐₙₖ[6]    1.7439    0.7743     0.0245    0.0202   1909.7708    0.9990      252.3815
    αₜₐₙₖ[7]    2.3672    0.8714     0.0276    0.0244   1457.9927    1.0003      192.6778
    αₜₐₙₖ[8]    1.7050    0.7634     0.0241    0.0165   2141.2771    0.9993      282.9757
    αₜₐₙₖ[9]   -0.3968    0.5806     0.0184    0.0149   1677.6531    0.9993      221.7065
   αₜₐₙₖ[10]    1.7416    0.7427     0.0235    0.0165   1789.7015    0.9990      236.5140
   αₜₐₙₖ[11]    0.7518    0.6648     0.0210    0.0119   2165.1738    0.9996      286.1337
   αₜₐₙₖ[12]    0.3678    0.6271     0.0198    0.0134   1696.1572    0.9990      224.1519
   αₜₐₙₖ[13]    0.7602    0.6321     0.0200    0.0184   1503.9658    0.9991      198.7532
   αₜₐₙₖ[14]   -0.0089    0.5591     0.0177    0.0143   2251.7398    0.9990      297.5737
   αₜₐₙₖ[15]    1.7186    0.7513     0.0238    0.0181   1701.2954    0.9991      224.8309
   αₜₐₙₖ[16]    1.7052    0.8352     0.0264    0.0197   2561.0844    0.9994      338.4544
   αₜₐₙₖ[17]    2.5293    0.6807     0.0215    0.0153   2335.2608    0.9999      308.6112
   αₜₐₙₖ[18]    2.1538    0.6106     0.0193    0.0127   2017.5408    0.9994      266.6236
   αₜₐₙₖ[19]    1.8211    0.5541     0.0175    0.0129   2040.7464    0.9991      269.6903
   αₜₐₙₖ[20]    3.1182    0.8090     0.0256    0.0180   1860.2350    0.9990      245.8352
   αₜₐₙₖ[21]    2.1118    0.5939     0.0188    0.0124   2154.4957    1.0004      284.7226
   αₜₐₙₖ[22]    2.1330    0.5403     0.0171    0.0124   1977.3792    0.9991      261.3161
   αₜₐₙₖ[23]    2.1404    0.6296     0.0199    0.0157   1597.7011    0.9996      211.1406
   αₜₐₙₖ[24]    1.5428    0.5133     0.0162    0.0102   2564.4389    0.9999      338.8977
   αₜₐₙₖ[25]   -1.0897    0.4634     0.0147    0.0094   2311.6748    0.9992      305.4942
   αₜₐₙₖ[26]    0.0814    0.4085     0.0129    0.0115   1540.2659    0.9990      203.5504
   αₜₐₙₖ[27]   -1.5639    0.5140     0.0163    0.0143   1536.3644    1.0001      203.0348
   αₜₐₙₖ[28]   -0.5354    0.4277     0.0135    0.0096   1693.7501    1.0015      223.8338
   αₜₐₙₖ[29]    0.0727    0.4244     0.0134    0.0076   2843.2748    0.9990      375.7466
   αₜₐₙₖ[30]    1.3104    0.4524     0.0143    0.0089   2570.4760    0.9993      339.6955
   αₜₐₙₖ[31]   -0.7293    0.4217     0.0133    0.0088   1539.9226    0.9996      203.5050
   αₜₐₙₖ[32]   -0.3756    0.3968     0.0125    0.0078   2113.3073    0.9991      279.2794
   αₜₐₙₖ[33]    2.8705    0.6783     0.0214    0.0155   1703.4320    0.9990      225.1133
   αₜₐₙₖ[34]    2.4523    0.5466     0.0173    0.0126   2068.7305    0.9990      273.3885
   αₜₐₙₖ[35]    2.4406    0.6026     0.0191    0.0118   2475.6821    0.9993      327.1682
   αₜₐₙₖ[36]    1.9179    0.4763     0.0151    0.0119   1917.7756    0.9990      253.4393
   αₜₐₙₖ[37]    1.9135    0.5010     0.0158    0.0142   1296.2046    0.9996      171.2970
   αₜₐₙₖ[38]    3.3758    0.8253     0.0261    0.0238   1291.2082    0.9995      170.6367
   αₜₐₙₖ[39]    2.4725    0.6036     0.0191    0.0142   1720.3647    0.9991      227.3510
   αₜₐₙₖ[40]    2.1440    0.4969     0.0157    0.0127   1753.4820    0.9991      231.7275
   αₜₐₙₖ[41]   -1.8925    0.4557     0.0144    0.0108   1974.6291    0.9993      260.9527
   αₜₐₙₖ[42]   -0.6283    0.3449     0.0109    0.0077   1989.7949    0.9999      262.9569
   αₜₐₙₖ[43]   -0.5123    0.3367     0.0106    0.0094   1763.3956    0.9991      233.0376
   αₜₐₙₖ[44]   -0.3909    0.3327     0.0105    0.0076   2145.7543    0.9990      283.5674
   αₜₐₙₖ[45]    0.5078    0.3592     0.0114    0.0072   1976.7262    0.9990      261.2298
   αₜₐₙₖ[46]   -0.6413    0.3376     0.0107    0.0090   1816.5516    0.9993      240.0623
   αₜₐₙₖ[47]    1.9353    0.5267     0.0167    0.0103   2106.5737    0.9996      278.3896
   αₜₐₙₖ[48]   -0.0410    0.3468     0.0110    0.0071   2359.9446    0.9990      311.8732

Quantiles
  parameters      2.5%     25.0%     50.0%     75.0%     97.5%
      Symbol   Float64   Float64   Float64   Float64   Float64

    αₜₐₙₖ[1]    0.2694    1.1550    1.6078    2.1583    3.3936
    αₜₐₙₖ[2]    0.7243    1.7697    2.3538    2.9963    4.3801
    αₜₐₙₖ[3]   -0.4896    0.3273    0.7345    1.1290    2.0754
    αₜₐₙₖ[4]    0.8548    1.7689    2.3424    3.0058    4.2713
    αₜₐₙₖ[5]    0.4060    1.2084    1.6893    2.2203    3.2626
    αₜₐₙₖ[6]    0.3278    1.2318    1.6867    2.2370    3.3928
    αₜₐₙₖ[7]    0.8883    1.7269    2.2781    2.9212    4.2139
    αₜₐₙₖ[8]    0.3293    1.1929    1.6484    2.1340    3.3526
    αₜₐₙₖ[9]   -1.5661   -0.7822   -0.3866   -0.0117    0.7805
   αₜₐₙₖ[10]    0.3875    1.2408    1.7127    2.1879    3.3472
   αₜₐₙₖ[11]   -0.5468    0.2865    0.7516    1.1726    2.1685
   αₜₐₙₖ[12]   -0.8160   -0.0812    0.3619    0.7980    1.6312
   αₜₐₙₖ[13]   -0.3960    0.3489    0.7247    1.1509    2.0850
   αₜₐₙₖ[14]   -1.1464   -0.3696   -0.0063    0.3440    1.0939
   αₜₐₙₖ[15]    0.4494    1.1834    1.6613    2.2258    3.3030
   αₜₐₙₖ[16]    0.2264    1.1131    1.6637    2.2285    3.4964
   αₜₐₙₖ[17]    1.3218    2.0481    2.4716    2.9686    4.0217
   αₜₐₙₖ[18]    1.0372    1.7577    2.1257    2.5041    3.4587
   αₜₐₙₖ[19]    0.8206    1.4054    1.7939    2.1906    2.9923
   αₜₐₙₖ[20]    1.6780    2.5540    3.0383    3.6051    4.9211
   αₜₐₙₖ[21]    1.0108    1.7116    2.0717    2.4760    3.3297
   αₜₐₙₖ[22]    1.2312    1.7482    2.0728    2.4794    3.2575
   αₜₐₙₖ[23]    1.0092    1.7010    2.1121    2.5103    3.5527
   αₜₐₙₖ[24]    0.6141    1.1762    1.5206    1.8869    2.6036
   αₜₐₙₖ[25]   -2.0290   -1.3989   -1.0869   -0.7731   -0.1862
   αₜₐₙₖ[26]   -0.6788   -0.2079    0.0769    0.3606    0.8990
   αₜₐₙₖ[27]   -2.6793   -1.9015   -1.5391   -1.2086   -0.5469
   αₜₐₙₖ[28]   -1.3956   -0.8012   -0.5360   -0.2751    0.2666
   αₜₐₙₖ[29]   -0.7322   -0.2058    0.0778    0.3284    0.8691
   αₜₐₙₖ[30]    0.4300    1.0080    1.2861    1.5866    2.2842
   αₜₐₙₖ[31]   -1.5350   -1.0038   -0.7235   -0.4447    0.1090
   αₜₐₙₖ[32]   -1.2231   -0.6265   -0.3640   -0.1265    0.4005
   αₜₐₙₖ[33]    1.6639    2.4036    2.8001    3.3225    4.3641
   αₜₐₙₖ[34]    1.4346    2.0697    2.4250    2.8127    3.5536
   αₜₐₙₖ[35]    1.4072    2.0030    2.4201    2.7952    3.7704
   αₜₐₙₖ[36]    1.0631    1.5780    1.8816    2.2376    2.9018
   αₜₐₙₖ[37]    1.0328    1.5693    1.8792    2.2170    3.0447
   αₜₐₙₖ[38]    1.9374    2.7992    3.2986    3.8883    5.1105
   αₜₐₙₖ[39]    1.3844    2.0599    2.4422    2.8266    3.7683
   αₜₐₙₖ[40]    1.2355    1.8256    2.1181    2.4530    3.1640
   αₜₐₙₖ[41]   -2.8522   -2.1601   -1.8748   -1.5912   -1.0543
   αₜₐₙₖ[42]   -1.2909   -0.8678   -0.6247   -0.4012    0.0465
   αₜₐₙₖ[43]   -1.1707   -0.7438   -0.5124   -0.2802    0.1138
   αₜₐₙₖ[44]   -1.0568   -0.6073   -0.3802   -0.1650    0.2633
   αₜₐₙₖ[45]   -0.1564    0.2520    0.4984    0.7473    1.2143
   αₜₐₙₖ[46]   -1.3230   -0.8526   -0.6355   -0.4140    0.0151
   αₜₐₙₖ[47]    0.9772    1.5614    1.9208    2.2859    3.0336
   αₜₐₙₖ[48]   -0.7252   -0.2527   -0.0320    0.1712    0.6416

using StatsPlots

StatsPlots.plot(chns)

Original output (Edition 1)

"""
             mean   sd  5.5% 94.5% n_eff Rhat
a_tank[1]   2.49 1.16  0.85  4.53  1079    1
a_tank[2]   5.69 2.75  2.22 10.89  1055    1
a_tank[3]   0.89 0.75 -0.23  2.16  1891    1
a_tank[4]   5.71 2.70  2.21 10.85   684    1
a_tank[5]   2.52 1.14  0.92  4.42  1640    1
a_tank[6]   2.49 1.13  0.94  4.52  1164    1
a_tank[7]   5.74 2.71  2.25 10.86   777    1
a_tank[8]   2.52 1.19  0.95  4.42  1000    1
a_tank[9]  -0.46 0.69 -1.62  0.55  2673    1
a_tank[10]  2.53 1.19  0.93  4.59  1430    1
a_tank[11]  0.93 0.72 -0.17  2.11  1387    1
a_tank[12]  0.47 0.74 -0.63  1.70  1346    1
a_tank[13]  0.91 0.76 -0.25  2.30  1559    1
a_tank[14]  0.00 0.66 -1.04  1.06  2085    1
a_tank[15]  2.50 1.19  0.95  4.40  1317    1
a_tank[16]  2.50 1.14  0.98  4.31  1412    1
a_tank[17]  3.49 1.12  1.94  5.49   945    1
a_tank[18]  2.59 0.75  1.50  3.81  1561    1
a_tank[19]  2.11 0.64  1.15  3.15  1712    1
a_tank[20]  6.40 2.57  3.11 11.04   996    1
a_tank[21]  2.59 0.74  1.54  3.93  1233    1
a_tank[22]  2.63 0.79  1.49  4.01  1184    1
a_tank[23]  2.64 0.83  1.45  4.13  1379    1
a_tank[24]  1.74 0.59  0.85  2.72  1736    1
a_tank[25] -1.19 0.45 -1.90 -0.50  2145    1
a_tank[26]  0.09 0.41 -0.53  0.78  2167    1
a_tank[27] -1.75 0.56 -2.65 -0.88  1666    1
a_tank[28] -0.58 0.43 -1.25  0.08  1567    1
a_tank[29]  0.08 0.39 -0.54  0.71  3053    1
a_tank[30]  1.43 0.49  0.66  2.24  2754    1
a_tank[31] -0.79 0.44 -1.50 -0.12  1299    1
a_tank[32] -0.42 0.41 -1.12  0.23  1661    1
a_tank[33]  3.84 1.08  2.31  5.70   808    1
a_tank[34]  3.00 0.85  1.83  4.36  1038    1
a_tank[35]  2.96 0.82  1.82  4.25  1578    1
a_tank[36]  2.14 0.55  1.31  3.08  1734    1
a_tank[37]  2.12 0.56  1.31  3.04  1131    1
a_tank[38]  6.72 2.62  3.45 11.44   706    1
a_tank[39]  2.95 0.73  1.85  4.08  1509    1
a_tank[40]  2.48 0.65  1.53  3.61  1731    1
a_tank[41] -2.15 0.57 -3.11 -1.29  1231    1
a_tank[42] -0.67 0.35 -1.22 -0.14  1444    1
a_tank[43] -0.54 0.35 -1.12  0.03  1776    1
a_tank[44] -0.43 0.34 -1.00  0.10  1735    1
a_tank[45]  0.54 0.36 -0.04  1.14  1376    1
a_tank[46] -0.67 0.34 -1.25 -0.15  1619    1
a_tank[47]  2.14 0.55  1.31  3.04  1916    1
a_tank[48] -0.06 0.35 -0.61  0.50  1932    1
""";