## 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. Make sure to include plots if the question requests them. 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_odds <- log(0.35/(1-0.35))
log_odds``````
``##  -0.6190392``

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

``````prob <- 1/(1+exp(-3.2))
prob``````
``##  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?

``````coeff <- exp(1.7)
coeff``````
``##  5.473947``
``#What this means is that we will see a value of 1.7 implies the log-odds will result a 5.47-time change in the odds of the outcome.``

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

``#Poisson regressions are measured as events per unit time so in order to make sure they're on scale, an offset is required sometimes. Such as weather forcast.``

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?

``````#The aggregated model contains an extra factor in its log-probabilities due to the way the data organized. When we use dbinom(), it contains a term for all the orders the successes could appear in trials. It counts all the ways you could see successes in trials.
#This makes the aggregated probabilities larger, so the PSIS/WAIC scores end up being smaller.``````

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

``#For a one unit change in the predictor variable, the difference in the log of outcome is expected to change by 1.7.``

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

``#The logit link maps a parameter that is defined as a probability mass, and therefore constrained to be between 0 and 1, which is the output of binomial generalized linear model.``

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

``#The log link maps a parameter that is defined over only positive real values onto a linear model. which works well with Poisson distribution, and the outcome are counts and always positive values.``

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?

``#It implies that the outcome of a Poisson generalized model will be positive and between 0 and 1 (inclusive).``

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 and Poisson distributions to have maximum entropy are: for binomial, binary events and fixed probability. When we talk about Poisson, the variance must be equal to expected values. The constraints are different because the distributions are different. ``

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). Plot and 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. Plot and compare the posterior distributions. Do the differences increase or decrease? Why?

``````data("chimpanzees")
df <-  chimpanzees
df\$recipient <- NULL

#Re-estimating using quap
m <- quap(
alist(
pulled_left ~ dbinom(1,p),
logit(p) <- a[actor] + (xp + xpc * condition)*prosoc_left,
a[actor] ~ dnorm(0,10),
xp ~ dnorm(0,10),
xpc ~ dnorm(0,10)
),
data = df
)
pairs(m)``````