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.
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?
plogodds <- 3.2
plogodds/(1+plogodds)
## [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?
# the log odds go up by exp(1.7) times
11E4. Why do Poisson regressions sometimes require the use of an offset? Provide an example.
# If the time for two events is different, Poisson regressions require the use of an offset. For example, 2 events will take both 1 day to have the same amount of cases.
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 possibilities of these two formats are different. The multiplier convert to a constant on the logarithmic scale when the probability of aggregation is converted to non-aggregation.
11M2. If a coefficient in a Poisson regression has value 1.7, what does this imply about the change in the outcome?
exp(1.7)
## [1] 5.473947
11M3. Explain why the logit link is appropriate for a binomial generalized linear model.
# logit link is appropriate for binomial generalized linear models because the parameters of logit link mappings are between 0 and 1.
11M4. Explain why the log link is appropriate for a Poisson generalized linear model.
# The Poisson distribution's lambda parameters with a linear model has range of [0, infinity), and log links map all Numbers to [0, infinity).
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 a logit link for the mean of a Poisson generalized linear model means that the Poisson likelihood lambda parameter has the range of [0, infinity).
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?
# Events are discrete
# Expected values are constant.
# The Poisson distribution is a special case of binomial distribution. It's constraints are more strict than binomial distribution. For Poisson, the 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")
data_chi <- chimpanzees
data_chi$recipient <- NULL
# map
model <- map(alist(
pulled_left ~ dbinom( 1 , p ) ,
logit(p) <- a[actor] + (x1 + x2*condition)*prosoc_left ,
a[actor] ~ dnorm(0,10),
x1 ~ dnorm(0,10),
x2 ~ dnorm(0,10)
) ,
data=data_chi)
pairs(model)
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)
data_k <- Kline
data_k$P <- scale( log(data_k$population) )
data_k$contact_id <- ifelse( data_k$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")
data_chi <- chimpanzees
model_1 <- map(
alist(
pulled_left ~ dbinom(1, p),
logit(p) <- a ,
a ~ dnorm(0,10)
),
data=data_chi )
## 10.4
model_2 <- map(
alist(
pulled_left ~ dbinom(1, p) ,
logit(p) <- a + bp*prosoc_left ,
a ~ dnorm(0,10) ,
bp ~ dnorm(0,10)
),
data=data_chi )
model_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=data_chi )
model_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 = data_chi)
# compare
compare(model_1,model_2,model_3,model_4)
## WAIC SE dWAIC dSE pWAIC weight
## model_4 547.5597 18.678250 0.0000 NA 14.313895 1.000000e+00
## model_2 680.5837 9.228696 133.0240 18.09911 2.042248 1.300812e-29
## model_3 681.9881 9.326282 134.4284 18.04076 2.822208 6.445457e-30
## model_1 687.9896 7.160268 140.4299 18.94314 1.024628 3.206521e-31