11E1. If an event has probability 0.35, what are the log-odds of this event?

p <- 0.35
log (p/ (1-p))
## [1] -0.6190392

11E2. If an event has log-odds 3.2, what is the probability of this event?

exp(3.2)/(1+exp(3.2))
## [1] 0.9608343
inv_logit(3.2)
## [1] 0.9608343

11E3. Suppose that a coefficient in a logistic regression has value 1.7. What does this imply about the proportional change in odds of the outcome?

Each one unit of change in the predictor leads to an increased odds of the outcome of 5.47.

exp(1.7)
## [1] 5.473947

11M1. As explained in the chapter, binomial data can be organized in aggregated and disaggregated forms, without any impact on inference. But the likelihood of the data does change when the data are converted between the two formats. Can you explain why?

Aggregated data will contain an extra factor in its log probabilities because of the way the data is organized. This makes aggregated probabilities larger, and the PSIS/WAIC scores end up being smaller, because there are more ways to see the data.

11M7.Use quap to construct a quadratic approximate posterior distribution for the chimpanzee model that includes a unique intercept for each actor, m11.4 (page 330). Compare the quadratic approximation to the posterior distribution produced instead from MCMC. Can you explain both the differences and the similarities between the approximate and the MCMC distributions? Relax the prior on the actor intercepts to Normal(0,10). Re-estimate the posterior using both ulam and quap. Do the differences increase or decrease? Why?

## Warning in '/var/folders/37/pz72_81s6b57rfxw4l1030pw0000gp/T/RtmpSCQTng/model-18257c3d5974.stan', line 2, column 4: Declaration
##     of arrays by placing brackets after a variable name is deprecated and
##     will be removed in Stan 2.32.0. Instead use the array keyword before the
##     type. This can be changed automatically using the auto-format flag to
##     stanc
## Warning in '/var/folders/37/pz72_81s6b57rfxw4l1030pw0000gp/T/RtmpSCQTng/model-18257c3d5974.stan', line 3, column 4: Declaration
##     of arrays by placing brackets after a variable name is deprecated and
##     will be removed in Stan 2.32.0. Instead use the array keyword before the
##     type. This can be changed automatically using the auto-format flag to
##     stanc
## Warning in '/var/folders/37/pz72_81s6b57rfxw4l1030pw0000gp/T/RtmpSCQTng/model-18257c3d5974.stan', line 4, column 4: Declaration
##     of arrays by placing brackets after a variable name is deprecated and
##     will be removed in Stan 2.32.0. Instead use the array keyword before the
##     type. This can be changed automatically using the auto-format flag to
##     stanc
## Running MCMC with 4 sequential chains, with 1 thread(s) per chain...
## 
## Chain 1 Iteration:   1 / 1000 [  0%]  (Warmup) 
## Chain 1 Iteration: 100 / 1000 [ 10%]  (Warmup) 
## Chain 1 Iteration: 200 / 1000 [ 20%]  (Warmup) 
## Chain 1 Iteration: 300 / 1000 [ 30%]  (Warmup) 
## Chain 1 Iteration: 400 / 1000 [ 40%]  (Warmup) 
## Chain 1 Iteration: 500 / 1000 [ 50%]  (Warmup) 
## Chain 1 Iteration: 501 / 1000 [ 50%]  (Sampling) 
## Chain 1 Iteration: 600 / 1000 [ 60%]  (Sampling) 
## Chain 1 Iteration: 700 / 1000 [ 70%]  (Sampling) 
## Chain 1 Iteration: 800 / 1000 [ 80%]  (Sampling) 
## Chain 1 Iteration: 900 / 1000 [ 90%]  (Sampling) 
## Chain 1 Iteration: 1000 / 1000 [100%]  (Sampling) 
## Chain 1 finished in 0.7 seconds.
## Chain 2 Iteration:   1 / 1000 [  0%]  (Warmup) 
## Chain 2 Iteration: 100 / 1000 [ 10%]  (Warmup) 
## Chain 2 Iteration: 200 / 1000 [ 20%]  (Warmup) 
## Chain 2 Iteration: 300 / 1000 [ 30%]  (Warmup) 
## Chain 2 Iteration: 400 / 1000 [ 40%]  (Warmup) 
## Chain 2 Iteration: 500 / 1000 [ 50%]  (Warmup) 
## Chain 2 Iteration: 501 / 1000 [ 50%]  (Sampling) 
## Chain 2 Iteration: 600 / 1000 [ 60%]  (Sampling) 
## Chain 2 Iteration: 700 / 1000 [ 70%]  (Sampling) 
## Chain 2 Iteration: 800 / 1000 [ 80%]  (Sampling) 
## Chain 2 Iteration: 900 / 1000 [ 90%]  (Sampling) 
## Chain 2 Iteration: 1000 / 1000 [100%]  (Sampling) 
## Chain 2 finished in 0.6 seconds.
## Chain 3 Iteration:   1 / 1000 [  0%]  (Warmup) 
## Chain 3 Iteration: 100 / 1000 [ 10%]  (Warmup) 
## Chain 3 Iteration: 200 / 1000 [ 20%]  (Warmup) 
## Chain 3 Iteration: 300 / 1000 [ 30%]  (Warmup) 
## Chain 3 Iteration: 400 / 1000 [ 40%]  (Warmup) 
## Chain 3 Iteration: 500 / 1000 [ 50%]  (Warmup) 
## Chain 3 Iteration: 501 / 1000 [ 50%]  (Sampling) 
## Chain 3 Iteration: 600 / 1000 [ 60%]  (Sampling) 
## Chain 3 Iteration: 700 / 1000 [ 70%]  (Sampling) 
## Chain 3 Iteration: 800 / 1000 [ 80%]  (Sampling) 
## Chain 3 Iteration: 900 / 1000 [ 90%]  (Sampling) 
## Chain 3 Iteration: 1000 / 1000 [100%]  (Sampling) 
## Chain 3 finished in 0.5 seconds.
## Chain 4 Iteration:   1 / 1000 [  0%]  (Warmup) 
## Chain 4 Iteration: 100 / 1000 [ 10%]  (Warmup) 
## Chain 4 Iteration: 200 / 1000 [ 20%]  (Warmup) 
## Chain 4 Iteration: 300 / 1000 [ 30%]  (Warmup) 
## Chain 4 Iteration: 400 / 1000 [ 40%]  (Warmup) 
## Chain 4 Iteration: 500 / 1000 [ 50%]  (Warmup) 
## Chain 4 Iteration: 501 / 1000 [ 50%]  (Sampling) 
## Chain 4 Iteration: 600 / 1000 [ 60%]  (Sampling) 
## Chain 4 Iteration: 700 / 1000 [ 70%]  (Sampling) 
## Chain 4 Iteration: 800 / 1000 [ 80%]  (Sampling) 
## Chain 4 Iteration: 900 / 1000 [ 90%]  (Sampling) 
## Chain 4 Iteration: 1000 / 1000 [100%]  (Sampling) 
## Chain 4 finished in 0.6 seconds.
## 
## All 4 chains finished successfully.
## Mean chain execution time: 0.6 seconds.
## Total execution time: 2.7 seconds.
##            mean        sd        5.5%       94.5%     n_eff    Rhat4
## a[1] -0.3579397 0.3460545 -0.92760515  0.18744752  553.4684 1.010730
## a[2] 11.1451434 5.0413942  4.93121695 20.46383300 1002.6890 1.005685
## a[3] -0.6633590 0.3516138 -1.25593070 -0.10766511  561.6330 1.012089
## a[4] -0.6719616 0.3598649 -1.26447605 -0.09045566  521.8365 1.009488
## a[5] -0.3535669 0.3371772 -0.89951333  0.18351192  564.1114 1.006721
## a[6]  0.5860356 0.3432327  0.04383590  1.13618895  473.1831 1.017605
## a[7]  2.1698212 0.4452914  1.47806470  2.90211770  605.3447 1.006915
## b[1] -0.1423491 0.2959832 -0.63236158  0.32585755  506.3974 1.011093
## b[2]  0.3915231 0.2973554 -0.07130248  0.86647943  517.6358 1.008836
## b[3] -0.5010525 0.2994497 -0.97705097 -0.01217731  480.6272 1.014511
## b[4]  0.2761771 0.3020766 -0.18923807  0.75708629  471.6049 1.013029
m11.4quap <- quap(
  alist(
    pulled_left ~ dbinom(1, p),
    logit(p) <- a[actor] + b[treatment],
    a[actor] ~ dnorm(0, 10),
    b[treatment] ~ dnorm(0, 0.5)
  ),
  data = dat_list
)

precis(m11.4quap, depth = 2)
##            mean        sd        5.5%        94.5%
## a[1] -0.3520051 0.3477653 -0.90780114  0.203790988
## a[2]  6.9923095 3.5459736  1.32515874 12.659460219
## a[3] -0.6546688 0.3537051 -1.21995787 -0.089379669
## a[4] -0.6546637 0.3537050 -1.21995255 -0.089374770
## a[5] -0.3520022 0.3477652 -0.90779815  0.203793850
## a[6]  0.5811755 0.3522749  0.01817214  1.144178873
## a[7]  2.1186140 0.4523567  1.39566065  2.841567257
## b[1] -0.1418735 0.3011497 -0.62316894  0.339421963
## b[2]  0.3815952 0.3009911 -0.09944670  0.862637194
## b[3] -0.4901434 0.3030960 -0.97454933 -0.005737425
## b[4]  0.2695615 0.3007595 -0.21111026  0.750233291