TuringModels

Multilevel Reedfrogs

  1. Data
  2. Model
  3. Output
  4. Original output

Data

import CSV
import Random

using DataFrames
using TuringModels: project_root

Random.seed!(1)

path = joinpath(project_root, "data", "reedfrogs.csv")
df = CSV.read(path, DataFrame; delim=';')
df.tank = 1:nrow(df)
df
48×6 DataFrame
 Row │ density  pred       size       surv   propsurv  tank
     │ 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

# Thanks to Kai Xu!

@model function m12_2(density, tank, surv)
    σ ~ truncated(Cauchy(0, 1), 0, Inf)
    α ~ Normal(0, 1)

    N_tank = length(tank)
    α_tank ~ filldist(Normal(α, σ), N_tank)

    logitp = α_tank[tank]
    surv .~ BinomialLogit.(density, logitp)
end;

Output

chns = sample(
    m12_2(df.density, df.tank, df.surv),
    NUTS(),
    1000
)
Chains MCMC chain (1000×62×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 8.28 seconds
Compute duration  = 8.28 seconds
parameters        = α, α_tank[25], α_tank[18], α_tank[46], α_tank[19], α_tank[7], α_tank[16], α_tank[10], α_tank[31], α_tank[33], α_tank[45], α_tank[1], α_tank[23], α_tank[5], α_tank[24], α_tank[44], α_tank[38], α_tank[40], α_tank[22], σ, α_tank[17], α_tank[21], α_tank[13], α_tank[42], α_tank[34], α_tank[28], α_tank[35], α_tank[47], α_tank[9], α_tank[39], α_tank[26], α_tank[32], α_tank[29], α_tank[4], α_tank[20], α_tank[36], α_tank[14], α_tank[41], α_tank[2], α_tank[6], α_tank[37], α_tank[3], α_tank[30], α_tank[27], α_tank[11], α_tank[43], α_tank[8], α_tank[12], α_tank[15], α_tank[48]
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.6286    0.2193     0.0069    0.0067    795.7250    1.0000       96.1253
           α    1.3057    0.2541     0.0080    0.0066   1328.5004    1.0021      160.4857
   α_tank[1]    2.1176    0.8626     0.0273    0.0216   1683.2688    0.9991      203.3424
   α_tank[2]    3.0896    1.1282     0.0357    0.0328   1107.3058    1.0001      133.7649
   α_tank[3]    0.9778    0.7203     0.0228    0.0179   1585.1642    0.9990      191.4912
   α_tank[4]    3.0485    1.0961     0.0347    0.0277   1212.7436    1.0005      146.5020
   α_tank[5]    2.1513    0.9251     0.0293    0.0257   1213.2682    0.9990      146.5654
   α_tank[6]    2.1499    0.8927     0.0282    0.0236   1959.8886    0.9993      236.7587
   α_tank[7]    3.0452    1.0696     0.0338    0.0315   1334.9371    0.9995      161.2632
   α_tank[8]    2.1508    0.8942     0.0283    0.0245   1151.3186    1.0005      139.0817
   α_tank[9]   -0.1548    0.6110     0.0193    0.0114   2250.8985    0.9994      271.9133
  α_tank[10]    2.1297    0.8390     0.0265    0.0211   1448.8884    1.0000      175.0288
  α_tank[11]    0.9770    0.6587     0.0208    0.0162   1746.8160    0.9990      211.0191
  α_tank[12]    0.5764    0.6511     0.0206    0.0153   1912.8781    0.9990      231.0797
  α_tank[13]    1.0014    0.7116     0.0225    0.0162   1704.8663    0.9992      205.9515
  α_tank[14]    0.1872    0.6012     0.0190    0.0138   1625.1506    1.0011      196.3217
  α_tank[15]    2.1361    0.9210     0.0291    0.0225   1967.3689    1.0006      237.6623
  α_tank[16]    2.1358    0.8797     0.0278    0.0256   1398.3038    0.9990      168.9181
  α_tank[17]    2.9192    0.8530     0.0270    0.0258   1533.1385    0.9995      185.2064
  α_tank[18]    2.3827    0.6567     0.0208    0.0151   1399.6047    0.9991      169.0752
  α_tank[19]    2.0114    0.5879     0.0186    0.0156   1348.9007    0.9998      162.9501
  α_tank[20]    3.7103    1.0409     0.0329    0.0288   1490.5644    0.9993      180.0633
  α_tank[21]    2.3944    0.6517     0.0206    0.0197   1438.4752    1.0005      173.7709
  α_tank[22]    2.4265    0.6712     0.0212    0.0159   1771.8315    0.9993      214.0410
  α_tank[23]    2.3724    0.6425     0.0203    0.0146   1314.4065    0.9998      158.7831
  α_tank[24]    1.6865    0.5145     0.0163    0.0124   1943.9746    1.0000      234.8363
  α_tank[25]   -0.9952    0.4509     0.0143    0.0117   1194.3414    1.0007      144.2790
  α_tank[26]    0.1508    0.4134     0.0131    0.0093   1917.2153    1.0003      231.6037
  α_tank[27]   -1.4376    0.5010     0.0158    0.0101   1952.4944    0.9990      235.8655
  α_tank[28]   -0.4821    0.4014     0.0127    0.0071   2151.6091    0.9991      259.9190
  α_tank[29]    0.1526    0.4137     0.0131    0.0071   2244.5244    0.9996      271.1433
  α_tank[30]    1.4278    0.4417     0.0140    0.0097   2201.2658    1.0008      265.9176
  α_tank[31]   -0.6469    0.4070     0.0129    0.0102   1785.1007    0.9996      215.6440
  α_tank[32]   -0.3195    0.4156     0.0131    0.0096   1909.0959    0.9992      230.6228
  α_tank[33]    3.1752    0.7265     0.0230    0.0185   1405.5056    0.9998      169.7881
  α_tank[34]    2.7312    0.6442     0.0204    0.0179   1149.8877    1.0016      138.9089
  α_tank[35]    2.7145    0.6403     0.0202    0.0150   1474.6904    0.9995      178.1457
  α_tank[36]    2.0584    0.5204     0.0165    0.0134   1616.8354    0.9990      195.3172
  α_tank[37]    2.0425    0.5106     0.0161    0.0107   1661.0486    1.0011      200.6582
  α_tank[38]    3.9039    0.9490     0.0300    0.0210   1553.4017    0.9990      187.6542
  α_tank[39]    2.7185    0.6582     0.0208    0.0170   1091.6723    0.9991      131.8763
  α_tank[40]    2.3148    0.5695     0.0180    0.0155   1205.3503    1.0013      145.6089
  α_tank[41]   -1.8051    0.4494     0.0142    0.0151   1563.3980    0.9995      188.8618
  α_tank[42]   -0.5799    0.3481     0.0110    0.0077   1911.6294    0.9999      230.9289
  α_tank[43]   -0.4638    0.3574     0.0113    0.0086   2051.9151    0.9990      247.8757
  α_tank[44]   -0.3490    0.3457     0.0109    0.0063   2514.4044    1.0008      303.7454
  α_tank[45]    0.5792    0.3381     0.0107    0.0068   1820.6594    0.9997      219.9395
  α_tank[46]   -0.5746    0.3761     0.0119    0.0091   1505.6258    1.0001      181.8828
  α_tank[47]    2.0557    0.5250     0.0166    0.0114   1985.8436    0.9991      239.8941
  α_tank[48]   -0.0001    0.3602     0.0114    0.0070   2529.3754    0.9990      305.5539

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

           σ    1.2525    1.4785    1.6153    1.7679    2.1148
           α    0.8136    1.1394    1.3026    1.4632    1.8188
   α_tank[1]    0.5262    1.5297    2.0484    2.6259    4.0167
   α_tank[2]    1.1475    2.3014    3.0046    3.7098    5.7448
   α_tank[3]   -0.3183    0.4912    0.9202    1.4529    2.4690
   α_tank[4]    1.1440    2.2802    2.9426    3.7166    5.6051
   α_tank[5]    0.5173    1.4837    2.0915    2.7283    4.2276
   α_tank[6]    0.5801    1.5371    2.0848    2.6795    4.1704
   α_tank[7]    1.1765    2.3189    2.9521    3.6532    5.4448
   α_tank[8]    0.6123    1.5471    2.0524    2.6646    4.0759
   α_tank[9]   -1.3817   -0.5865   -0.1299    0.2509    0.9854
  α_tank[10]    0.6091    1.5742    2.0630    2.6224    3.9400
  α_tank[11]   -0.2176    0.5098    0.9612    1.3862    2.2899
  α_tank[12]   -0.6658    0.1373    0.5648    0.9861    1.9104
  α_tank[13]   -0.2401    0.5142    0.9828    1.4230    2.5037
  α_tank[14]   -0.9970   -0.2150    0.2012    0.5761    1.3381
  α_tank[15]    0.5032    1.4942    2.0894    2.6932    4.1646
  α_tank[16]    0.6400    1.5187    2.0732    2.7243    3.9697
  α_tank[17]    1.4363    2.2881    2.8520    3.4634    4.7816
  α_tank[18]    1.1871    1.9479    2.3284    2.8060    3.8656
  α_tank[19]    0.9542    1.6036    1.9589    2.3895    3.2824
  α_tank[20]    2.0580    2.9259    3.6184    4.4023    6.0142
  α_tank[21]    1.2346    1.9757    2.3298    2.7896    3.8769
  α_tank[22]    1.2568    1.9273    2.3776    2.8647    3.8428
  α_tank[23]    1.2646    1.9237    2.3319    2.7289    3.7912
  α_tank[24]    0.7687    1.3218    1.6601    2.0154    2.7909
  α_tank[25]   -1.8879   -1.2655   -0.9846   -0.6953   -0.1725
  α_tank[26]   -0.6447   -0.1180    0.1301    0.4079    1.0197
  α_tank[27]   -2.4972   -1.7456   -1.4178   -1.0765   -0.5723
  α_tank[28]   -1.2754   -0.7449   -0.4714   -0.2039    0.2926
  α_tank[29]   -0.6970   -0.1330    0.1488    0.4450    0.9800
  α_tank[30]    0.6302    1.1171    1.3933    1.7232    2.3341
  α_tank[31]   -1.4625   -0.9137   -0.6421   -0.3780    0.1422
  α_tank[32]   -1.1198   -0.6168   -0.3204   -0.0279    0.4807
  α_tank[33]    1.8999    2.6513    3.1134    3.6355    4.7131
  α_tank[34]    1.6560    2.2456    2.7023    3.1138    4.1553
  α_tank[35]    1.6036    2.2723    2.6636    3.0950    4.1721
  α_tank[36]    1.1584    1.6904    2.0240    2.3882    3.2683
  α_tank[37]    1.1228    1.6859    2.0030    2.3650    3.0999
  α_tank[38]    2.3035    3.2660    3.8138    4.4572    6.0319
  α_tank[39]    1.5882    2.2636    2.6696    3.1083    4.1105
  α_tank[40]    1.3341    1.9120    2.2726    2.6680    3.5313
  α_tank[41]   -2.7501   -2.0825   -1.7846   -1.4825   -1.0271
  α_tank[42]   -1.2603   -0.8239   -0.5746   -0.3260    0.0455
  α_tank[43]   -1.1941   -0.6954   -0.4642   -0.2158    0.2030
  α_tank[44]   -1.0398   -0.5675   -0.3492   -0.1180    0.2956
  α_tank[45]   -0.0802    0.3544    0.5652    0.7960    1.2854
  α_tank[46]   -1.3491   -0.8218   -0.5672   -0.3135    0.1454
  α_tank[47]    1.1524    1.6801    2.0329    2.4012    3.1858
  α_tank[48]   -0.7390   -0.2362    0.0038    0.2317    0.7140

using StatsPlots

StatsPlots.plot(chns)

Original output

m122rethinking = "
                mean   sd  5.5% 94.5% n_eff Rhat
a               1.30 0.25  0.90  1.70 11662    1
sigma       1.62 0.22  1.30  1.99  6556    1
a_tank[1]   2.12 0.88  0.84  3.60 16091    1
a_tank[2]   3.05 1.10  1.52  4.92 10962    1
a_tank[3]   1.00 0.66 -0.02  2.10 18175    1
a_tank[4]   3.05 1.11  1.47  4.96 10181    1
a_tank[5]   2.13 0.87  0.85  3.62 13720    1
a_tank[6]   2.12 0.86  0.86  3.59 11628    1
a_tank[7]   3.07 1.13  1.47  5.03 10315    1
a_tank[8]   2.13 0.87  0.86  3.60 13754    1
a_tank[9]  -0.18 0.60 -1.14  0.76 18218    1
a_tank[10]  2.11 0.86  0.83  3.58 15121    1
a_tank[11]  1.00 0.67 -0.04  2.09 17390    1
a_tank[12]  0.58 0.62 -0.41  1.60 17209    1
a_tank[13]  0.99 0.66 -0.04  2.09 15225    1
a_tank[14]  0.19 0.62 -0.79  1.20 18293    1
a_tank[15]  2.13 0.89  0.83  3.63 12445    1
a_tank[16]  2.11 0.87  0.87  3.61 12385    1
a_tank[17]  2.89 0.80  1.76  4.29 12583    1
a_tank[18]  2.38 0.66  1.43  3.49 14437    1
a_tank[19]  2.00 0.58  1.12  2.99 13959    1
a_tank[20]  3.67 1.03  2.20  5.44 10629    1
a_tank[21]  2.38 0.65  1.42  3.47 15309    1
a_tank[22]  2.39 0.66  1.42  3.49 13614    1
a_tank[23]  2.40 0.67  1.41  3.53 11868    1
a_tank[24]  1.69 0.52  0.90  2.55 18468    1
a_tank[25] -1.00 0.45 -1.74 -0.30 18153    1
a_tank[26]  0.16 0.40 -0.47  0.81 21895    1
a_tank[27] -1.44 0.50 -2.28 -0.69 16718    1
a_tank[28] -0.47 0.41 -1.15  0.17 20160    1
a_tank[29]  0.15 0.40 -0.48  0.80 19401    1
a_tank[30]  1.44 0.49  0.70  2.24 15407    1
a_tank[31] -0.64 0.42 -1.33 -0.01 15356    1
a_tank[32] -0.31 0.40 -0.95  0.32 19130    1
a_tank[33]  3.18 0.78  2.06  4.55 10894    1
a_tank[34]  2.70 0.66  1.75  3.84 13573    1
a_tank[35]  2.69 0.64  1.74  3.78 13876    1
a_tank[36]  2.06 0.53  1.26  2.92 16329    1
a_tank[37]  2.06 0.51  1.29  2.91 14672    1
a_tank[38]  3.88 0.97  2.52  5.57  9349    1
a_tank[39]  2.70 0.64  1.77  3.78 13444    1
a_tank[40]  2.34 0.56  1.49  3.31 14966    1
a_tank[41] -1.82 0.48 -2.61 -1.10 14214    1
a_tank[42] -0.58 0.36 -1.16 -0.02 17203    1
a_tank[43] -0.46 0.35 -1.02  0.08 17762    1
a_tank[44] -0.34 0.34 -0.90  0.20 16740    1
a_tank[45]  0.58 0.35  0.02  1.14 18946    1
a_tank[46] -0.57 0.34 -1.13 -0.03 19761    1
a_tank[47]  2.05 0.51  1.30  2.90 15122    1
a_tank[48]  0.00 0.33 -0.53  0.53 18236    1
";