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
odds <- p/(1-p)
log_odds <- log(odds)
log_odds
## [1] -0.6190392
11E2. If an event has log-odds 3.2, what is the probability of this event?
log_odds <- 3.2
odds <- exp(log_odds)
p <- odds/(1+odds)
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?
exp(1.7)
## [1] 5.473947
#The odd increases 5.5 times in the outcome.
11E4. Why do Poisson regressions sometimes require the use of an offset? Provide an example.
#Poisson Regression assumes that the rate of an event is constant. An offset is needed to account for the different times of an event. For example, six cases over 1 year should not amount to the same as six cases over 10 years.
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 data change when it gets converted between the two formats because the aggregated form involves a extra log-odd factor. 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?
#Every time the predictor variable changes by one unit, 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 constrained to 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 Poisson linear model has the rate which gives the number of events per time interval and has to be 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?
#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?
#The constraints for binomial distribution are that the events are discrete and the expect value is constant.
#Poisson distribution constraints are that the variance is equal to expected value and both are constant.
#Poisson has more constraints than binomial, because it is a special case of binomial distribution and it has its own constraints.
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
q11.7 <- 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(q11.7)
#The posterior mean and standard deviation are very close. MCMC model's posterior std is slightly higher.
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 )
d
## culture population contact total_tools mean_TU P contact_id
## 1 Malekula 1100 low 13 3.2 -1.291473310 1
## 2 Tikopia 1500 low 22 4.7 -1.088550750 1
## 3 Santa Cruz 3600 low 24 4.0 -0.515764892 1
## 4 Yap 4791 high 43 5.0 -0.328773359 2
## 5 Lau Fiji 7400 high 33 5.0 -0.044338980 2
## 6 Trobriand 8000 high 19 4.0 0.006668287 2
## 7 Chuuk 9200 high 40 3.8 0.098109204 2
## 8 Manus 13000 low 28 6.6 0.324317564 1
## 9 Tonga 17500 high 55 5.4 0.518797917 2
## 10 Hawaii 275000 low 71 6.6 2.321008320 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.
d <- chimpanzees
m11.1 <- map(
alist(
pulled_left ~ dbinom(1, p),
logit(p) <- a ,
a ~ dnorm(0,10)
),
data=d )
m11.2.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.2,m11.3,m11.4)
## WAIC SE dWAIC dSE pWAIC weight
## m11.4 552.0683 18.518042 0.0000 NA 16.2971688 1.000000e+00
## m11.2.2 680.4642 9.374649 128.3959 18.02903 1.9840350 1.315780e-28
## m11.3 682.3675 9.321657 130.2992 17.94727 3.0113041 5.080360e-29
## m11.1 687.9165 7.130248 135.8481 18.86838 0.9881897 3.169226e-30
#m11.4 which has the unique intercepts for each chimp is the best model.