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

p <- 0.35
p/(1-p)
## [1] 0.5384615

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

lo <- 3.2
lo/(1+lo)
## [1] 0.7619048

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?

# It implies that log ods will change in exp(1.7) times 
exp(1.7)
## [1] 5.473947

11E4. Why do Poisson regressions sometimes require the use of an offset? Provide an example.

# Because offset can help bringing all observations on the same scale. 
# An example could be if the number of events is measured on the daily or weekly basis, the offset parameter can be used to convert all measurements to the daily basis. 

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?

#Because likelihood in these two formats are different. When converting likelihood in the aggregated form to the non-aggregated format, the c(n,m) multiplier is converted to an constant at the log-scale.    

11M2. If a coefficient in a Poisson regression has value 1.7, what does this imply about the change in the outcome?

# It implies that the change of the predictor by 1 unit increases the lambda parameter of the Poisson distribution in exp(1.7)=5.4739 times.

11M3. Explain why the logit link is appropriate for a binomial generalized linear model.

# Binomial likelihood is parametrised by parameter p - probability of an event
# We are interested in modelling it with linear combination of the predictors.
# But p shoudl fall in the [0, 1] range
# Because log link maps all negative numbers to the [0,1]
# So we model p = f^-1(a+b*x) is in [0,1] scale
# f=logit=log(p/1-p), f^-1=logistic=exp(a+b*x)/(1+exp(a+b*x))
# logit function ensures required contraint.

11M4. Explain why the log link is appropriate for a Poisson generalized linear model.

# As in the previous case(10M3) we are interested in modelling lambda - parameter of the Poisson distribution with linear model. 
# Lambda should be in [0, +inf) range. 
# Because log link maps all  numbers to [0, infinity]. That's why log link is appropriate. 

11M5. What would it imply to use a logit link for the mean of a Poisson generalized linear model? Can you think of a real research problem for which this would make sense?

# Using logit link implies that a lambda parameter of the Poisson likelihood always falls in [0, +inf) range.

11M6. State the constraints for which the binomial and Poisson distributions have maximum entropy. Are the constraints different at all for binomial and Poisson? Why or why not?

# Constrains for binomial distribution is that the events are discrete and the expected value is constant. 
# Poisson distributions adds more constraints than binomial because it's a special case of binomial distribution, and it has it's own constrains. For Poisson, its variance is equal to the expected value and both are constant.

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?

data("chimpanzees")
d <- chimpanzees
d$recipient <- NULL

# map
q2 <- map(alist(
  pulled_left ~ dbinom( 1 , p ) ,
  logit(p) <- a[actor] + (bp + bpC*condition)*prosoc_left ,
  a[actor] ~ dnorm(0,10),
  bp ~ dnorm(0,10),
  bpC ~ dnorm(0,10)
) ,
data=d)
pairs(q2)

11M8. Revisit the data(Kline) islands example. This time drop Hawaii from the sample and refit the models. What changes do you observe?

11H1. Use WAIC or PSIS to compare the chimpanzee model that includes a unique intercept for each actor, m11.4 (page 330), to the simpler models fit in the same section. Interpret the results.

m11.1 <- map(
  alist(
    pulled_left ~ dbinom(1, p),
    logit(p) <- a ,
    a ~ dnorm(0,10)
  ),
  data=d )

## 10.4
m11.2 <- map(
  alist(
    pulled_left ~ dbinom(1, p) ,
    logit(p) <- a + bp*prosoc_left ,
    a ~ dnorm(0,10) ,
    bp ~ dnorm(0,10)
  ),
  data=d )

m11.3 <- map(
  alist(
    pulled_left ~ dbinom(1, p) ,
    logit(p) <- a + (bp + bpC*condition)*prosoc_left ,
    a ~ dnorm(0,10) ,
    bp ~ dnorm(0,10) ,
    bpC ~ dnorm(0,10)
  ), data=d )

m11.4 <- map(
  alist(
    pulled_left ~ dbinom(1, p),
    logit(p) <- a[actor] + (bp + bpC*condition)*prosoc_left,
    a[actor] ~ dnorm(0, 10),
    bp ~ dnorm(0, 10),
    bpC ~ dnorm(0, 10)
  ),
  data = d)


# compare
compare(m11.1,m11.2,m11.3,m11.4)
##           WAIC        SE    dWAIC      dSE      pWAIC       weight
## m11.4 550.5687 18.590296   0.0000       NA 15.9346256 1.000000e+00
## m11.2 680.6659  9.301107 130.0971 18.08342  2.0842705 5.620451e-29
## m11.3 682.7074  9.364443 132.1386 18.03149  3.1819112 2.025173e-29
## m11.1 687.8868  7.154588 137.3180 18.91436  0.9732327 1.519738e-30
# H11.4 with the uniqure intercepts for each chimp wins.