### Exercise 1

# By inoculum level
jansen.apple %>% group_by(inoculum) %>% summarize(infected = mean(y))
# By genotype
jansen.apple %>% group_by(gen) %>% summarize(infected = mean(y))
# Combination
jansen.apple %>% group_by(inoculum, gen) %>% summarize(infected = mean(y))

### Exercise 2

fit_apples <- glmer(cbind(y, n - y) ~ gen + inoculum + gen:inoculum + (1|block), data = jansen.apple, family = binomial(link = 'logit'))
check_model(fit_apples)
summary(fit_apples)

The diagnostics look reasonably good but we get a singular fit. The random effect estimates for block are all zero. The effect of block is very small and probably negligible. The model summary shows us that as inoculum increases, the probability of infection increases. The interaction term between Jonathan and inoculum level 5000 indicates that the increase in infection probability as inoculum increases is much weaker for the Jonathan variety.

### Exercise 3

fit_worms <- glmer(y ~ spray + lead + spray:lead + (1|block), data = beall.webworms, family = poisson(link = 'log'))
check_model(fit_worms)
summary(fit_worms)

Some of the model diagnostics are problematic, particularly the overdispersion diagnostic. This indicates there are too many counts of 0 in the dataset for the Poisson distribution to be a good fit. We see large differences between the different treatments in the model summary, but it is unclear whether this is a good model.

### Exercise 4

fit_nb_worms <- glmer.nb(y ~ spray + lead + spray:lead + (1|block), data = beall.webworms)
check_model(fit_nb_worms)
summary(fit_nb_worms)

We see similar but somewhat weaker effects in the negative binomial model. The diagnostic plots are improved but still not ideal. This may be adequate but it may also be an option to use a nonparametric model or a zero-inflated Poisson model.