Code 7.1
Code 7.2
Code 7.3
Code 7.4
Code 7.5
WAIC values are point estimates and hence is a good idea to include the uncertainty asociated with their estimation when computing weights. PyMC3 uses a Bayesian bootstrapping to do this (read more here), and also to compute the standard error (SE) of WAIC/LOO estimates. If you set bootstrapping = False
weights (and SE) will be computed as in the book.
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m7.4 | 476.15 | 4.2 | 0 | 0.97 | 14.77 | 0 | 1 |
m7.3 | 539.66 | 2.7 | 63.51 | 0.03 | 12.97 | 14.55 | 0 |
Code 7.6
Since the link function isn't implemented we have to compute the mean over samples ourselves using a loop.
Code 7.7
Code 7.8
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m7.5 | 469.61 | 5.13 | 0 | 0.92 | 14.56 | 0 | 1 |
m7.4 | 476.15 | 4.2 | 6.54 | 0.08 | 14.77 | 5.87 | 1 |
m7.3 | 539.66 | 2.7 | 70.05 | 0 | 12.97 | 14.62 | 0 |
Code 7.9
Code 7.10
First calculate the necessary posterior predicted means. The link function is replaced by a loop. We'll use model 7.5b since it's a one-liner.
Code 7.11
Code 7.12
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | 9.170 | 0.136 | 0.005 | 8.961 | 9.397 | 757.0 | 1.001 |
bA | -1.827 | 0.220 | 0.007 | -2.175 | -1.475 | 914.0 | 1.001 |
bR | -0.178 | 0.075 | 0.003 | -0.309 | -0.069 | 811.0 | 1.001 |
bAR | 0.334 | 0.128 | 0.004 | 0.132 | 0.532 | 977.0 | 1.002 |
sigma | 0.954 | 0.056 | 0.001 | 0.867 | 1.044 | 1534.0 | 1.000 |
Code 7.13
Code 7.14
Code 7.15
Code 7.16
Hence the probability to have a negative slope association ruggedness with log-GDP inside Africa is so small, it might just be zero.
Code 7.17
Plot the reverse interpretation: The influence of being in Africa depends upon terrain ruggedness.
This places cont_africa
on the horizontal axis, while using different lines for different values of rugged
.
Code 7.16
water | shade | blooms | |
---|---|---|---|
count | 27.00000 | 27.00000 | 27.000000 |
mean | 2.00000 | 2.00000 | 128.993704 |
std | 0.83205 | 0.83205 | 92.683923 |
min | 1.00000 | 1.00000 | 0.000000 |
25% | 1.00000 | 1.00000 | 71.115000 |
50% | 2.00000 | 2.00000 | 111.040000 |
75% | 3.00000 | 3.00000 | 190.300000 |
max | 3.00000 | 3.00000 | 361.660000 |
Code 7.19
Code 7.20
You can use the modified Powell's method if it fails with BFGS (default MAP estimate)
Code 7.21
conftab
is not implemented in PyMC3, something similar is to use summary()
Code 7.22
WAIC | pWAIC | dWAIC | weight | SE | dSE | warning | |
---|---|---|---|---|---|---|---|
model | |||||||
m7.7 | 293.81 | 3.98 | 0 | 1 | 6.91 | 0 | 1 |
m7.6 | 303.22 | 3.42 | 9.42 | 0 | 6.81 | 3.78 | 1 |
7.23
Center and re-estimate
7.24
No interaction.
Interaction.
7.25
7.26
7.27
mean | sd | mc_error | hpd_5.5 | hpd_94.5 | n_eff | Rhat | |
---|---|---|---|---|---|---|---|
a | 127.616 | 10.447 | 0.273 | 111.626 | 143.977 | 1614.0 | 1.0 |
bW | 74.554 | 12.421 | 0.279 | 54.207 | 93.543 | 2000.0 | 1.0 |
bS | -41.036 | 12.041 | 0.253 | -58.691 | -20.283 | 2000.0 | 1.0 |
bWS | -51.541 | 14.074 | 0.271 | -74.775 | -30.657 | 2000.0 | 1.0 |
sigma | 51.674 | 7.838 | 0.209 | 39.389 | 63.193 | 1425.0 | 1.0 |
7.28
We have to replace the link
function with a loop.
Let's remake the plots with water on abscissa while varying shade levels from left to right.
When there is no interaction the slope is the same across all three plots (top row), showing a general reduction with increasing shade. For the interaction (bottom row) we can see a huge increase in blooms for the lowest amount of shade as we increase water. This effect is reduced by increasing shade to average levels and in the last plot increasing water has a minimum effect when there is lots of shade.
7.29
7.30
7.31
7.32
7.33
Not sure how this one works