Chapter 11 - God Spiked the Integers

This chapter described some of the most common generalized linear models, those used to model counts. It is important to never convert counts to proportions before analysis, because doing so destroys information about sample size. A fundamental difficulty with these models is that parameters are on a different scale, typically log-odds (for binomial) or log-rate (for Poisson), than the outcome variable they describe. Therefore computing implied predictions is even more important than before.

Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Problems are labeled Easy (E), Medium (M), and Hard(H).

Finally, upon completion, name your final output .html file as: YourName_ANLY505-Year-Semester.html and publish the assignment to your R Pubs account and submit the link to Canvas. Each question is worth 5 points.

Questions

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

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

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

lgodds <- exp(3.2)
p <- lgodds/(1+lgodds)
returnValue(p)
## [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?

# A logistic regression value of 1.7 implies that one unit change in variable results in a 1.7 increase in the log-odds of the outcome

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

#offset will help bringing all observations on the same scale. For example, the offset parameter can  convert all measurements to the daily basis, when the number of events is measured on the daily or weekly 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?

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

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

#It implies the difference in log of outcomes will change by 5.47

exp(1.7)
## [1] 5.473947

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

# Logit link maps a parameter that is defined as a probability mass and its value falls between 0 & 1. The reason makes logit link appropriate for a binomial generalized linear model is that A binomial generalized model always generates binary outcome variables i.e. either 0 or 1. 

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

# The log link fits a poisson because log link predictor value are restricted to positive numbers and the outcome of Poisson are always positive.

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?

# As previously discussed, it implies that the likelihood will always be non-negative and the outcome are counts and always positive values.

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?

# The constraints for Binomial distribution are that the expected value is constant and the events are discrete.

# For Poisson distribution, the variance is equal to expected value and both are constant.

# Poisson has more constraints than binomial because it's a special case of binomial distribution  and it has it's own constrains.

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

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?

data(Kline)
d <- Kline
d$P <- scale( log(d$population) )
d$contact_id <- ifelse( d$contact=="high", 2, 1)

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.

data("chimpanzees")

d <- chimpanzees

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

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(m11.1,m11.2,m11.3,m11.4)
##           WAIC        SE    dWAIC      dSE     pWAIC       weight
## m11.4 548.4652 18.685918   0.0000       NA 14.805691 1.000000e+00
## m11.2 680.5945  9.143693 132.1293 18.10957  2.044180 2.034598e-29
## m11.3 682.2793  9.462305 133.8141 18.09310  2.968286 8.762601e-30
## m11.1 687.9908  7.169864 139.5257 18.96925  1.025147 5.039478e-31