# Load lme4 library.
library(lme4)

# Fit Model 5.1 (see chapter5_R_final.R for code to load the data).
model5.1.fit.lmer <- lmer(activate ~ region.f*treat + (1|animal),
                            data = rat.brain, REML = T)

# View results and 95% confidence intervals.
summary(model5.1.fit.lmer)
confint(model5.1.fit.lmer)
anova(model5.1.fit.lmer)

# Display the random effects (EBLUPs) from the model.
ranef(model5.1.fit.lmer)

# Fit Model 5.2.
model5.2.fit.lmer <- lmer(activate ~ region.f*treat + (treat|animal),
                              data = rat.brain, REML = T)

summary(model5.2.fit.lmer)
anova(model5.2.fit.lmer)

# Plot the predicted random effects along with measures of uncertainty.
library(merTools)
REsim(model5.2.fit.lmer)
plotREsim(REsim(model5.2.fit.lmer))

# For Hypothesis 5.1.
0.5*(1 - pchisq(26.1,1)) + 0.5*(1 - pchisq(26.1,2))

