############################################################################################
################################ Mixed-model analysis ######################################
############################################################################################

##Instead of just analyzing the post-treatment bite forces, or even the differences in bite force pre- vs post-treatment,
##a better approach is to use all of the bite force data in a repeated-measures type analysis with 
##"treatment" (E, OA, antennae, etc) and "measure" (i.e. pre- or post-treatment bite force measurement)
##as fixed factors; size as a covariate; and individual as a random factor (because the same crickets are measured
##twice). This also allows us to test for an interaction between treatment and measure.

library(tidyverse)
library(nlme)
library(emmeans)

data1 <- read.csv("epicricket_mixed.csv", header=TRUE)

glimpse(data1)

hist(data1$bite)

##Data looks normal-ish. We can do Shapiro tests or whatever, but the important assumptions are really
##about normality of the model residuals, so it makes more sense to check the model fit later.

##Reorder treatments along x-axis so that we have control first in the figures
data1$treat <- factor(data1$treat, levels = c("control","epinastine","ant","ant_OA", "ant_OA_E"))
data1$measure <- factor(data1$measure, levels = c("pre","post"))

d <- ggplot(data1, aes(x = treat, y = bite, color = measure))

d + geom_boxplot() +  theme_classic() + scale_color_lancet()+geom_point()+geom_jitter()+
  theme(legend.position="none")+labs(x="Treatment",y = "Bite force (N)", color = "Measure")

##Looks like we have some effects of both measure and treatment. Let's fit the model to test where they are.

##Fit mixed-model as described above with nlme
model4 <- lme(bite ~ size + treat + measure + treat:measure + size:measure, random = ~1|cricket, method = "ML", data=data1)

##Summarize and check model fit 
summary(model4)
plot(model4)
qqnorm(model4)

##Don't see any red flags in the model fit.

##Perform log-likelihood reduction tests to find the minimum adeqwuate model

model5 <- update(model4, ~.- treat:measure)
anova(model4, model5, test = "Chi")

1-pchisq(2*(model4$logLik - model5$logLik),1)

model6 <- update(model4, ~.-size:measure)

1-pchisq(2*(model4$logLik - model6$logLik),1)

model7 <- update(model6, ~.- size)

1-pchisq(2*(model6$logLik - model7$logLik),1)

##Refit with REML

model6 <- lme(bite ~ size + treat + measure + treat:measure, random = ~1|cricket, data=data1)

summary(model6)

anova.lme(model6,type="marginal",adjustSigma=F)

##There is a significant interaction between treatment and measure. So some treatment levels are showing
##differences between pre- and post-treatment bite force, whereas others are not.
##To find out where these effects lie, we'll have to do some post-hoc tests comparing the levels of 
##the measure factor within each level of the treatment factor.

##Display the interaction levels:
levels(data1$treat:data1$measure)

##Get pairwise measure contrasts from within treatment levels using emmeans
emm_s.t <- emmeans(model4, pairwise ~ measure | treat)

##Display contrasts
emm_s.t

##Control type 1 error for contrasts from emmeans using false discovery rate

p <- c(0.7502, 0.0012, 0.0008,0.0653, 0.0001)

p.adjust(p, method = "fdr")

