TuringModels

Gaussian model of height

This model is based on data of the !Kung San people (Howell, 2010). The prior assumes that the height of a man is most likely to be 178 cm because McElreath has that lenght.

In Edition 2, McElreath defines the model as

hiNormal(μ,σ)μNormal(178,0.1)σUniform(0,50) \begin{aligned} h_i &\sim \text{Normal}(\mu, \sigma) \\ \mu &\sim \text{Normal}(178, 0.1) \\ \sigma &\sim \text{Uniform}(0, 50) \end{aligned}
  1. Data
  2. Model
  3. Output
  4. Original output
  5. df
  6. References

Data

import CSV

using DataFrames
using Random
using Turing
using TuringModels

Random.seed!(1)

data_path = joinpath(TuringModels.project_root, "data", "Howell1.csv")
df = CSV.read(data_path, DataFrame; delim=';')
df = filter(row -> row.age >= 18, df);

For df, see Section df.

Model

@model function line(height)
    μ ~ Normal(178, 20)
    σ ~ Uniform(0, 50)

    height ~ Normal(μ, σ)
end

model = line(df.height);

Output

chns = sample(model, NUTS(), 1000)
Chains MCMC chain (1000×14×1 Array{Float64, 3}):

Iterations        = 501:1:1500
Number of chains  = 1
Samples per chain = 1000
Wall duration     = 4.48 seconds
Compute duration  = 4.48 seconds
parameters        = μ, σ
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

           μ   154.5984    0.4284     0.0135    0.0142    943.1796    1.0005      210.6252
           σ     7.7572    0.3078     0.0097    0.0088   1128.9255    0.9998      252.1048

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

           μ   153.8192   154.2767   154.6241   154.8975   155.4164
           σ     7.1779     7.5499     7.7438     7.9555     8.4038

using StatsPlots

StatsPlots.plot(chns)

Original output

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

Empirical Posterior Estimates:
         Mean        SD       Naive SE       MCSE      ESS
alpha 154.597086 0.27326431 0.0043206882 0.0036304132 1000
 beta   0.906380 0.04143488 0.0006551430 0.0006994720 1000
sigma   5.106643 0.19345409 0.0030587777 0.0032035103 1000

Quantiles:
          2.5%       25.0%       50.0%       75.0%       97.5%
alpha 154.0610000 154.4150000 154.5980000 154.7812500 155.1260000
 beta   0.8255494   0.8790695   0.9057435   0.9336445   0.9882981
sigma   4.7524368   4.9683400   5.0994450   5.2353100   5.5090128
""";

df

df
352×4 DataFrame
 Row │ height   weight   age      male
     │ Float64  Float64  Float64  Int64
─────┼──────────────────────────────────
   1 │ 151.765  47.8256    63.0       1
   2 │ 139.7    36.4858    63.0       0
   3 │ 136.525  31.8648    65.0       0
   4 │ 156.845  53.0419    41.0       1
   5 │ 145.415  41.2769    51.0       0
   6 │ 163.83   62.9926    35.0       1
   7 │ 149.225  38.2435    32.0       0
   8 │ 168.91   55.48      27.0       1
   9 │ 147.955  34.8699    19.0       0
  10 │ 165.1    54.4877    54.0       1
  11 │ 154.305  49.8951    47.0       0
  12 │ 151.13   41.2202    66.0       1
  13 │ 144.78   36.0322    73.0       0
  14 │ 149.9    47.7       20.0       0
  15 │ 150.495  33.8493    65.3       0
  16 │ 163.195  48.5627    36.0       1
  17 │ 157.48   42.3258    44.0       1
  18 │ 143.942  38.3569    31.0       0
  19 │ 161.29   48.9879    39.0       1
  20 │ 156.21   42.7227    29.0       0
  21 │ 146.4    35.4936    56.0       1
  22 │ 148.59   37.9033    45.0       0
  23 │ 147.32   35.4652    19.0       0
  24 │ 147.955  40.313     29.0       1
  25 │ 161.925  55.1114    30.0       1
  26 │ 146.05   37.5064    24.0       0
  27 │ 146.05   38.4986    35.0       0
  28 │ 152.705  46.6066    33.0       0
  29 │ 142.875  38.8388    27.0       0
  30 │ 142.875  35.5786    32.0       0
  31 │ 147.955  47.4004    36.0       0
  32 │ 160.655  47.8823    24.0       1
  33 │ 151.765  49.4132    30.0       1
  34 │ 162.865  49.3848    24.0       1
  35 │ 171.45   56.5573    52.0       1
  36 │ 147.32   39.1223    42.0       0
  37 │ 147.955  49.8951    19.0       0
  38 │ 154.305  41.2485    55.0       1
  39 │ 143.51   38.5553    43.0       0
  40 │ 146.7    42.4       20.0       1
  41 │ 157.48   44.6505    18.0       1
  42 │ 165.735  58.5984    42.0       1
  43 │ 152.4    46.72      44.0       0
  44 │ 141.605  44.2252    60.0       0
  45 │ 158.8    50.9       20.0       0
  46 │ 155.575  54.3176    37.0       0
  47 │ 164.465  45.8978    50.0       1
  48 │ 151.765  48.0241    50.0       0
  49 │ 161.29   52.2198    31.0       1
  50 │ 154.305  47.6272    25.0       0
  51 │ 145.415  45.6427    23.0       0
  52 │ 145.415  42.4109    52.0       0
  53 │ 152.4    36.4858    79.3       1
  54 │ 163.83   55.9336    35.0       1
  55 │ 144.145  37.1945    27.0       0
  56 │ 153.67   48.3075    38.0       1
  57 │ 142.875  37.3363    39.0       0
  58 │ 167.005  47.1736    30.0       1
  59 │ 158.42   47.287     24.0       0
  60 │ 165.735  57.5495    51.0       1
  61 │ 149.86   37.9316    46.0       0
  62 │ 154.94   47.2019    22.0       0
  63 │ 160.96   43.2046    29.0       1
  64 │ 161.925  50.2637    38.0       1
  65 │ 147.955  39.3775    30.0       0
  66 │ 159.385  50.689     45.0       1
  67 │ 148.59   39.4342    47.0       0
  68 │ 136.525  36.2874    79.0       0
  69 │ 158.115  46.2664    45.0       1
  70 │ 144.78   42.2691    54.0       0
  71 │ 156.845  47.6272    31.0       1
  72 │ 179.07   55.7068    23.0       1
  73 │ 170.18   48.5627    41.0       1
  74 │ 146.05   42.8077    23.0       0
  75 │ 147.32   35.0683    36.0       0
  76 │ 162.56   56.7557    30.0       0
  77 │ 152.4    51.2559    34.0       0
  78 │ 160.02   47.2303    44.0       1
  79 │ 149.86   40.9367    43.0       0
  80 │ 142.875  32.7153    73.3       0
  81 │ 167.005  57.0675    38.0       1
  82 │ 159.385  42.9778    43.0       1
  83 │ 154.94   39.9444    33.0       0
  84 │ 162.56   45.9545    35.0       1
  85 │ 152.4    41.1068    29.0       0
  86 │ 170.18   47.5988    58.0       1
  87 │ 146.05   37.5064    53.0       0
  88 │ 159.385  45.019     51.0       1
  89 │ 151.13   42.2691    48.0       0
  90 │ 160.655  54.8563    29.0       1
  91 │ 169.545  53.5239    41.0       1
  92 │ 158.75   52.1914    81.75      1
  93 │ 149.86   42.4109    35.0       0
  94 │ 153.035  49.5833    46.0       0
  95 │ 161.925  41.7305    29.0       1
  96 │ 162.56   56.0186    42.0       1
  97 │ 149.225  42.1557    27.0       0
  98 │ 163.195  53.0986    22.0       1
  99 │ 161.925  50.2353    43.0       1
 100 │ 145.415  42.5243    53.0       0
 101 │ 163.195  49.1013    43.0       1
 102 │ 151.13   38.4986    41.0       0
 103 │ 150.495  49.8101    50.0       0
 104 │ 170.815  59.7607    33.0       1
 105 │ 157.48   47.939     62.0       1
 106 │ 152.4    39.2924    49.0       0
 107 │ 147.32   36.8827    22.0       0
 108 │ 145.415  42.1274    29.0       0
 109 │ 157.48   44.5654    33.0       1
 110 │ 154.305  47.854     34.0       0
 111 │ 167.005  55.1965    42.0       1
 112 │ 142.875  32.9988    40.0       0
 113 │ 152.4    40.88      27.0       0
 114 │ 160.0    51.2       25.0       1
 115 │ 159.385  49.0446    29.0       1
 116 │ 149.86   53.4388    45.0       0
 117 │ 160.655  54.0908    26.0       1
 118 │ 160.655  55.3666    45.0       1
 119 │ 149.225  42.2408    45.0       0
 120 │ 140.97   40.9367    85.6       0
 121 │ 154.94   49.6967    26.0       1
 122 │ 141.605  44.3386    24.0       0
 123 │ 160.02   45.9545    57.0       1
 124 │ 150.165  41.9573    22.0       0
 125 │ 155.575  51.4827    24.0       0
 126 │ 156.21   44.1118    21.0       0
 127 │ 153.035  32.205     79.0       0
 128 │ 167.005  56.7557    50.0       1
 129 │ 149.86   52.6734    40.0       0
 130 │ 147.955  36.4858    64.0       0
 131 │ 159.385  48.8462    32.0       1
 132 │ 161.925  56.9541    38.7       1
 133 │ 155.575  42.099     26.0       0
 134 │ 159.385  50.1786    63.0       1
 135 │ 146.685  46.5499    62.0       0
 136 │ 172.72   61.8019    22.0       1
 137 │ 166.37   48.9879    41.0       1
 138 │ 141.605  31.5246    19.0       1
 139 │ 151.765  35.2951    74.0       0
 140 │ 156.845  45.6427    41.0       1
 141 │ 148.59   43.885     33.0       0
 142 │ 157.48   45.5576    53.0       0
 143 │ 149.86   39.0089    18.0       0
 144 │ 147.955  41.1635    37.0       0
 145 │ 153.035  45.2458    61.0       0
 146 │ 160.655  53.6373    44.0       1
 147 │ 149.225  52.3048    35.0       0
 148 │ 138.43   39.094     23.0       0
 149 │ 162.56   45.6994    55.0       1
 150 │ 149.225  40.398     53.0       0
 151 │ 158.75   51.4827    59.0       1
 152 │ 149.86   38.6687    57.0       0
 153 │ 158.115  39.2357    35.0       1
 154 │ 156.21   44.3386    29.0       0
 155 │ 148.59   39.5192    62.0       1
 156 │ 143.51   31.0711    18.0       0
 157 │ 154.305  46.7767    51.0       0
 158 │ 157.48   40.6248    19.0       1
 159 │ 157.48   50.1786    42.0       1
 160 │ 154.305  41.2769    25.0       0
 161 │ 168.275  54.6       41.0       1
 162 │ 145.415  44.9907    37.0       0
 163 │ 149.225  35.8054    82.0       1
 164 │ 154.94   45.2175    28.0       1
 165 │ 162.56   48.1091    50.0       1
 166 │ 156.845  45.671     43.0       0
 167 │ 161.011  48.4209    31.0       1
 168 │ 144.78   41.1918    67.0       0
 169 │ 143.51   38.4136    39.0       0
 170 │ 149.225  42.1274    18.0       0
 171 │ 149.86   38.2435    48.0       0
 172 │ 165.735  48.3359    30.0       1
 173 │ 144.145  38.9239    64.0       0
 174 │ 157.48   40.0295    72.0       1
 175 │ 154.305  50.207     68.0       0
 176 │ 163.83   54.2893    44.0       1
 177 │ 156.21   45.6       43.0       0
 178 │ 144.145  39.4342    34.0       0
 179 │ 162.56   43.2046    62.0       1
 180 │ 146.05   31.8648    44.0       0
 181 │ 154.94   45.4442    31.0       1
 182 │ 144.78   38.045     29.0       0
 183 │ 146.685  36.0889    62.0       0
 184 │ 152.4    40.88      67.0       0
 185 │ 163.83   47.9107    57.0       1
 186 │ 165.735  47.7122    32.0       1
 187 │ 156.21   46.3798    24.0       0
 188 │ 152.4    41.1635    77.0       1
 189 │ 140.335  36.5992    62.0       0
 190 │ 163.195  48.1375    67.0       1
 191 │ 151.13   36.7126    70.0       0
 192 │ 171.12   56.5573    37.0       1
 193 │ 149.86   38.6971    58.0       0
 194 │ 163.83   47.4854    35.0       1
 195 │ 141.605  36.2023    30.0       0
 196 │ 149.225  41.2769    26.0       0
 197 │ 146.05   44.7639    21.0       0
 198 │ 161.29   50.4338    41.0       1
 199 │ 162.56   55.2815    46.0       1
 200 │ 145.415  37.9316    49.0       0
 201 │ 170.815  58.4567    28.0       1
 202 │ 159.385  44.4237    83.0       0
 203 │ 159.4    44.4       54.0       1
 204 │ 153.67   44.5654    54.0       0
 205 │ 160.02   44.6221    68.0       1
 206 │ 150.495  40.4831    68.0       0
 207 │ 149.225  44.0835    56.0       0
 208 │ 142.875  34.4163    57.0       0
 209 │ 142.113  32.772     22.0       0
 210 │ 147.32   35.9472    40.0       0
 211 │ 162.56   49.5549    19.0       1
 212 │ 164.465  53.1837    41.0       1
 213 │ 160.02   37.0811    75.9       1
 214 │ 153.67   40.5114    73.9       0
 215 │ 167.005  50.6039    49.0       1
 216 │ 151.13   43.9701    26.0       1
 217 │ 153.035  49.89      88.0       1
 218 │ 139.065  33.5942    68.0       0
 219 │ 152.4    43.8567    33.0       1
 220 │ 154.94   48.1375    26.0       0
 221 │ 147.955  42.751     56.0       0
 222 │ 144.145  33.906     34.0       0
 223 │ 155.575  39.7176    74.0       1
 224 │ 150.495  35.9472    69.0       0
 225 │ 155.575  50.9157    50.0       1
 226 │ 154.305  45.7561    44.0       0
 227 │ 157.48   49.2147    18.0       0
 228 │ 168.91   58.8252    41.0       1
 229 │ 150.495  43.4598    27.0       0
 230 │ 160.02   51.9646    38.0       1
 231 │ 167.64   50.6889    57.0       1
 232 │ 144.145  34.2462    64.5       0
 233 │ 145.415  39.3775    42.0       0
 234 │ 160.02   59.5623    24.0       1
 235 │ 164.465  52.1631    71.0       1
 236 │ 153.035  39.9728    49.5       0
 237 │ 149.225  43.9417    33.0       1
 238 │ 160.02   54.6011    28.0       0
 239 │ 149.225  45.0757    47.0       0
 240 │ 153.67   41.3336    27.0       0
 241 │ 150.495  41.9006    55.0       0
 242 │ 151.765  42.524     83.4       1
 243 │ 158.115  43.1479    63.0       1
 244 │ 149.225  40.8233    52.0       0
 245 │ 151.765  42.8644    49.0       1
 246 │ 154.94   46.2097    31.0       0
 247 │ 161.29   47.854     35.0       1
 248 │ 148.59   42.5243    35.0       0
 249 │ 160.655  48.506     24.0       1
 250 │ 157.48   45.8695    41.0       1
 251 │ 167.005  52.9002    32.0       1
 252 │ 157.48   47.5705    43.0       1
 253 │ 152.4    43.5448    63.0       0
 254 │ 152.4    43.4314    21.0       0
 255 │ 161.925  53.212     55.0       0
 256 │ 152.4    44.6788    38.0       0
 257 │ 159.385  47.2019    28.0       1
 258 │ 142.24   31.6664    36.0       0
 259 │ 168.91   56.4439    38.0       1
 260 │ 160.02   55.7918    48.0       1
 261 │ 158.115  47.4854    45.0       1
 262 │ 152.4    45.1608    38.0       0
 263 │ 155.575  45.5293    21.0       0
 264 │ 154.305  48.8745    50.0       0
 265 │ 156.845  46.5782    41.0       1
 266 │ 156.21   43.885     30.0       0
 267 │ 168.275  56.047     21.0       1
 268 │ 147.955  40.0862    38.0       0
 269 │ 157.48   50.8023    19.0       0
 270 │ 160.7    46.3       31.0       1
 271 │ 161.29   49.3565    21.0       1
 272 │ 150.495  44.1118    50.0       0
 273 │ 163.195  51.0291    39.0       1
 274 │ 148.59   40.7666    44.0       1
 275 │ 148.59   37.5631    36.0       0
 276 │ 161.925  51.5961    36.0       1
 277 │ 153.67   44.8206    18.0       0
 278 │ 151.13   43.4031    58.0       0
 279 │ 163.83   46.72      58.0       1
 280 │ 153.035  39.5476    33.0       0
 281 │ 151.765  34.7848    21.5       0
 282 │ 156.21   39.2924    26.0       1
 283 │ 140.335  37.4497    22.0       0
 284 │ 158.75   48.6761    28.0       1
 285 │ 142.875  35.607     42.0       0
 286 │ 151.943  43.7149    21.0       1
 287 │ 161.29   48.1942    19.0       1
 288 │ 160.985  50.9724    48.0       1
 289 │ 144.78   43.9984    46.0       0
 290 │ 160.02   48.1942    25.0       1
 291 │ 160.985  46.6916    51.0       1
 292 │ 165.989  56.4155    25.0       1
 293 │ 157.988  48.591     28.0       1
 294 │ 154.94   48.2225    26.0       0
 295 │ 160.655  47.4854    54.0       1
 296 │ 147.32   35.5503    66.0       0
 297 │ 146.7    36.6       20.0       0
 298 │ 147.32   48.9596    25.0       0
 299 │ 172.999  51.2559    38.0       1
 300 │ 158.115  46.5215    51.0       1
 301 │ 147.32   36.9677    48.0       0
 302 │ 165.989  48.6477    27.0       1
 303 │ 149.86   38.045     22.0       0
 304 │ 161.925  47.287     60.0       1
 305 │ 163.83   55.3949    43.0       1
 306 │ 160.02   54.2042    27.0       1
 307 │ 154.94   48.4776    30.0       1
 308 │ 152.4    43.0629    29.0       0
 309 │ 146.05   34.1895    23.0       0
 310 │ 151.994  49.9518    30.0       0
 311 │ 151.765  44.3386    41.0       0
 312 │ 144.78   33.4524    42.0       0
 313 │ 160.655  47.287     43.0       1
 314 │ 151.13   46.1246    35.0       0
 315 │ 153.67   47.4004    75.5       1
 316 │ 147.32   40.8516    64.0       0
 317 │ 139.7    50.3487    38.0       1
 318 │ 157.48   45.1324    24.2       0
 319 │ 154.94   42.2408    26.0       1
 320 │ 143.51   41.6454    19.0       0
 321 │ 158.115  45.2175    43.0       1
 322 │ 147.32   51.2559    38.0       0
 323 │ 160.02   49.2714    23.0       1
 324 │ 165.1    51.1992    49.0       1
 325 │ 154.94   43.8567    41.0       0
 326 │ 153.67   35.5219    23.0       0
 327 │ 141.605  42.8854    43.0       0
 328 │ 163.83   46.7767    21.0       1
 329 │ 161.29   41.8722    24.0       1
 330 │ 154.9    38.2       20.0       1
 331 │ 161.3    43.3       20.0       1
 332 │ 170.18   53.6373    34.0       1
 333 │ 149.86   42.9778    29.0       0
 334 │ 160.655  39.7743    65.0       1
 335 │ 154.94   43.3464    46.0       0
 336 │ 166.37   52.6734    43.0       1
 337 │ 148.285  38.4419    39.0       0
 338 │ 151.765  42.8077    43.0       0
 339 │ 148.59   35.8905    70.0       0
 340 │ 153.67   44.2252    26.0       0
 341 │ 146.685  38.0734    48.0       0
 342 │ 154.94   44.1118    44.0       1
 343 │ 156.21   44.0268    33.0       0
 344 │ 160.655  47.8823    41.0       1
 345 │ 146.05   39.4058    37.4       0
 346 │ 156.21   41.0501    53.0       1
 347 │ 152.4    40.8233    49.0       0
 348 │ 162.56   47.0318    27.0       0
 349 │ 142.875  34.2462    31.0       0
 350 │ 162.56   52.1631    31.0       1
 351 │ 156.21   54.0625    21.0       0
 352 │ 158.75   52.5316    68.0       1

References

Howell, N. (2010). Life Histories of the Dobe !Kung: Food, Fatness, and Well-being over the Life-span. Origins of Human Behavior and Culture. University of California Press