Data

The data for this will be the afterlife data from the Agresti website, which has been summarized into a number of tails

library(dplyr)

afterlife <- readRDS(url("https://collinn.github.io/data/afterlife_tables.rds"))

## Tables included
names(afterlife)
## [1] "full"        "gender"      "gender_long" "race"

For this lab, we will use yes as our outcome of success and the combined no and undecided (labeled not_yes) as our failure. This is true for both our GLMs and for any statistics you derive based on the tables

Questions

Question 1: Construct the appropriate intercept-only model. Provide a useful interpretation for the intercept. Does it matter which table we use to construct this?

Question 2: Construct the appropriate model considering only sex as the covariate. Use this to find (1) the odds that a female believes in the afterlife and (2) the odds that a male believes in the afterlife. Confirm this by finding the odds in the table

Question 3: Consider the difference of log odds for belief in the afterlife for both males and females

\[ \log \left( \frac{\pi_M}{1 - \pi_M} \right) - \log \left( \frac{\pi_F}{1 - \pi_F} \right) \] write these in terms of their linear predictors, and explain why \(e^{\beta_1}\) represents the odds ratio.

Question 4: Construct the GLM that considers belief in the afterlife using both sex and race. What is the reference category? Do the coefficients represent conditional or marginal associations? Verify by comparing exponentiated coefficients with the relevant statistic from the table. Do they match exactly? Explain why you think?

Question 5: Based on the results from Question 4, does it seem like the association between sex and belief in the afterlife are conditionally independent, given race? Explain

Question 6: Fit a GLM that includes sex and race, along with an interaction component (y ~ gender*race). Is there evidence of homogenous association? State what this means and verify by computing the relevant statistics using both the model and the table.

Example

Example syntax for fitting glm

mj <- readRDS(url("https://collinn.github.io/data/marijuana.rds"))

mj_sex <- mj$mj_sex

## Could create n if we wanted
mj_sex <- mutate(mj_sex, n = yes + no)

## Fitting a glm
fit <- glm(yes / (yes + no) ~ gender, family = binomial, 
     weights = yes + no, data = mj_sex)

## Using n instead
fit <- glm(yes / n ~ gender, family = binomial, 
     weights = n, data = mj_sex)

exp(coef(fit))