Exercise 1

fisher.barley <- fisher.barley %>%
  mutate(year = factor(year))

We need to do this because we are treating year as a categorical variable, essentially as a component of the environment. With only two years, it is not possible to treat year as a continuous variable to estimate linear trends in the responses over time.

Exercise 2

fit_crossed <- lmer(yield ~ gen + (1|env) + (1|year), data = fisher.barley)
fit_nested <- lmer(yield ~ gen + (1|env) + (1|year:env), data = fisher.barley)

Look at the model output with summary(fit_crossed), fixef(fit_crossed), ranef(fit_crossed), and anova(fit_crossed), and do the same for the other model. The fixed effect estimates are the same but with different degrees of freedom due to the different grouping structures. The random effect estimates are different. The ANOVA has the same sums of squares but because of the different degrees of freedom, the F-statistics and p-values are different.

Exercise 3

fit <- lmer(biomass ~ treatment + (1|experiment), data = bioassay)

We get a warning that the model may not have converged. This indicates something might be wrong.

check_model(fit)

The residuals look good enough, but the posterior predictive check shows predictions of negative biomass.

Exercise 4

Here is the same model with the only difference being that we log-transform the response.

fit_log <- lmer(log(biomass) ~ treatment + (1|experiment), data = bioassay)
check_model(fit_log)

Not only do we resolve the model convergence issue, but we also now have model predictions that are all positive.