setwd()

####################### Intraclass correlation coefficient ################################
#Load packages
library(tidyr)
library(dplyr)
library(irr)
library(readxl)

Rpt <- read_excel("Repeatability.xlsx")

#Calculation of ICC per brain region
Rpt_wide_R1 <- Rpt %>%
  group_by(FishID) %>%
  mutate(rep = row_number()) %>%
  select(FishID, rep, R1_2) %>%
  pivot_wider(names_from = rep, values_from = R1_2)
icc(Rpt_wide_R1[,-1], model = "twoway", type = "agreement", unit = "single")

Rpt_wide_R2 <- Rpt %>%
  group_by(FishID) %>%
  mutate(rep = row_number()) %>%
  select(FishID, rep, R2_2) %>%
  pivot_wider(names_from = rep, values_from = R2_2)
icc(Rpt_wide_R2[,-1], model = "twoway", type = "agreement", unit = "single")

Rpt_wide_R3 <- Rpt %>%
  group_by(FishID) %>%
  mutate(rep = row_number()) %>%
  select(FishID, rep, R3_2) %>%
  pivot_wider(names_from = rep, values_from = R3_2)
icc(Rpt_wide_R3[,-1], model = "twoway", type = "agreement", unit = "single")

Rpt_wide_R4 <- Rpt %>%
  group_by(FishID) %>%
  mutate(rep = row_number()) %>%
  select(FishID, rep, R4) %>%
  pivot_wider(names_from = rep, values_from = R4)
icc(Rpt_wide_R4[,-1], model = "twoway", type = "agreement", unit = "single")

Rpt_wide_R5 <- Rpt %>%
  group_by(FishID) %>%
  mutate(rep = row_number()) %>%
  select(FishID, rep, R5) %>%
  pivot_wider(names_from = rep, values_from = R5)
icc(Rpt_wide_R5[,-1], model = "twoway", type = "agreement", unit = "single")

Rpt_wide_R6 <- Rpt %>%
  group_by(FishID) %>%
  mutate(rep = row_number()) %>%
  select(FishID, rep, R6_2) %>%
  pivot_wider(names_from = rep, values_from = R6_2)
icc(Rpt_wide_R6[,-1], model = "twoway", type = "agreement", unit = "single")

Rpt_wide_Tot <- Rpt %>%
  group_by(FishID) %>%
  mutate(rep = row_number()) %>%
  select(FishID, rep, Total) %>%
  pivot_wider(names_from = rep, values_from = Total)
icc(Rpt_wide_Tot[,-1], model = "twoway", type = "agreement", unit = "single")

################### Analysis brain weight + volume of the brain regions ###################
#Load packages
library(lmtest)
library(car)
library(readr)

#Prepare dataframe
MV <- read_csv("Mollies Volumes - R_Export_v2.csv")
MV$FishID <- as.character(MV$FishID)
MV$TreatEnrich <- as.factor(MV$TreatEnrich)
MV$TreatNeighbour <- as.factor(MV$TreatNeighbour)
names(MV)[names(MV) == "Brain_Weight _mg"] <- "Brain_Weight_mg"

##Brain weight ~ SL*TreatNeighbour*TreatEnrich (no log transformation)
MV_BW.lm1 <- lm(Brain_Weight_mg ~ SL_mm*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_BW.lm1)

MV_BW.lm2 <- lm(Brain_Weight_mg ~ SL_mm*TreatEnrich + SL_mm*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_BW.lm2)

MV_BW.lm3 <- lm(Brain_Weight_mg ~ SL_mm*TreatEnrich + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_BW.lm3)

MV_BW.lm4 <- lm(Brain_Weight_mg ~ SL_mm*TreatEnrich + TreatNeighbour, data = MV)
summary(MV_BW.lm4)

MV_BW.lm5 <- lm(Brain_Weight_mg ~ SL_mm + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_BW.lm5)

#Model selection
anova(MV_BW.lm1, MV_BW.lm2)
anova(MV_BW.lm2, MV_BW.lm3)
anova(MV_BW.lm3, MV_BW.lm4)
anova(MV_BW.lm4, MV_BW.lm5)

#Check assumptions on final model
plot(MV_BW.lm5)           #Linearity, Normality, Homogeneity, Influential values
bptest(MV_BW.lm1)         #Heteroscedascity full model

##Brain weight ~ SL*TreatNeighbour*TreatEnrich (log transformation)
MV_BW.log.lm1 <- lm(log(Brain_Weight_mg) ~ log(SL_mm)*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_BW.log.lm1)

MV_BW.log.lm2 <- lm(log(Brain_Weight_mg) ~ log(SL_mm)*TreatEnrich + log(SL_mm)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_BW.log.lm2)

MV_BW.log.lm3 <- lm(log(Brain_Weight_mg) ~ log(SL_mm)*TreatEnrich + log(SL_mm)*TreatNeighbour, data = MV)
summary(MV_BW.log.lm3)

MV_BW.log.lm4 <- lm(log(Brain_Weight_mg) ~ log(SL_mm)*TreatEnrich + TreatNeighbour, data = MV)
summary(MV_BW.log.lm4)

MV_BW.log.lm5 <- lm(log(Brain_Weight_mg) ~ log(SL_mm) + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_BW.log.lm5)

#Model selection
anova(MV_BW.log.lm1, MV_BW.log.lm2)
anova(MV_BW.log.lm2, MV_BW.log.lm3)
anova(MV_BW.log.lm3, MV_BW.log.lm4)
anova(MV_BW.log.lm4, MV_BW.log.lm5)

#Check assumptions on final model
plot(MV_BW.log.lm5)           #Linearity, Normality, Homogeneity, Influential values
bptest(MV_BW.log.lm5)         #Heteroscedascity

##Brain region ~ Rest*TreatEnrich*TreatNeigh
#Calculate volume rest brain region
MV$Rest_1x2 <- MV$Vol_Total - MV$Vol_1x2
MV$Rest_2x2 <- MV$Vol_Total - MV$Vol_2x2
MV$Rest_3x2 <- MV$Vol_Total - MV$Vol_3x2
MV$Rest_4 <- MV$Vol_Total - MV$Vol_4
MV$Rest_5 <- MV$Vol_Total - MV$Vol_5
MV$Rest_6x2 <- MV$Vol_Total - MV$Vol_6x2

##Region 1: Olfactory bulbs
#Volume region 1 ~ Rest*TreatNeighbour*TreatEnrich (no log transformation)
MV_R1.lm1 <- lm(Vol_1x2 ~ Rest_1x2*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R1.lm1)

MV_R1.lm2 <- lm(Vol_1x2 ~ Rest_1x2*TreatEnrich + TreatEnrich*TreatNeighbour + Rest_1x2*TreatNeighbour, data = MV)
summary(MV_R1.lm2)

MV_R1.lm3 <- lm(Vol_1x2 ~ Rest_1x2*TreatEnrich + Rest_1x2*TreatNeighbour, data = MV)
summary(MV_R1.lm3)

MV_R1.lm4 <- lm(Vol_1x2 ~ Rest_1x2*TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R1.lm4)

MV_R1.lm5 <- lm(Vol_1x2 ~ Rest_1x2 + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R1.lm5)

#Model selection
anova(MV_R1.lm1, MV_R1.lm2) 
anova(MV_R1.lm2, MV_R1.lm3) 
anova(MV_R1.lm3, MV_R1.lm4) 
anova(MV_R1.lm4, MV_R1.lm5) 

#Check assumptions on final model
plot(MV_R1.lm5)                 #Linearity, Normality, Homogeneity, Influential values
bptest(MV_R1.lm1)               #Heteroscedascity full model

#Volume region 1 ~ Rest*TreatNeighbour*TreatEnrich (log transformation)
MV_R1.log.lm1 <- lm(log(Vol_1x2) ~ log(Rest_1x2)*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R1.log.lm1)

MV_R1.log.lm2 <- lm(log(Vol_1x2) ~ log(Rest_1x2)*TreatEnrich + log(Rest_1x2)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R1.log.lm2)

MV_R1.log.lm3 <- lm(log(Vol_1x2) ~ log(Rest_1x2)*TreatEnrich + log(Rest_1x2)*TreatNeighbour, data = MV)
summary(MV_R1.log.lm3)

MV_R1.log.lm4 <- lm(log(Vol_1x2) ~  log(Rest_1x2)*TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R1.log.lm4)

MV_R1.log.lm5 <- lm(log(Vol_1x2) ~ log(Rest_1x2) + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R1.log.lm5)

#Model selection
anova(MV_R1.log.lm1, MV_R1.log.lm2)
anova(MV_R1.log.lm2, MV_R1.log.lm3)
anova(MV_R1.log.lm3, MV_R1.log.lm4)
anova(MV_R1.log.lm4, MV_R1.log.lm5)

#Check assumptions on final model
par(mfrow = c(2, 2))
plot(MV_R1.log.lm5)                 #Linearity, Normality, Homogeneity, Influential values
bptest(MV_R1.log.lm1)               #Heteroscedascity full model

##Region 2: Telencephalon
#Volume region 2 ~ Rest*TreatNeighbour*TreatEnrich (no log transformation)
MV_R2.lm1 <- lm(Vol_2x2 ~ Rest_2x2*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R2.lm1)

MV_R2.lm2 <- lm(Vol_2x2 ~ Rest_2x2*TreatEnrich + TreatEnrich*TreatNeighbour + Rest_2x2*TreatNeighbour, data = MV)
summary(MV_R2.lm2)

MV_R2.lm3 <- lm(Vol_2x2 ~ Rest_2x2*TreatEnrich + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R2.lm3)

MV_R2.lm4 <- lm(Vol_2x2 ~ Rest_2x2*TreatEnrich + TreatNeighbour, data = MV)
summary(MV_R2.lm4)

MV_R2.lm5 <- lm(Vol_2x2 ~ Rest_2x2 + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R2.lm5)

#Model selection
anova(MV_R2.lm1, MV_R2.lm2) 
anova(MV_R2.lm2, MV_R2.lm3) 
anova(MV_R2.lm3, MV_R2.lm4) 
anova(MV_R2.lm4, MV_R2.lm5) 

#Check assumptions on final model
plot(MV_R2.lm5)                 #Linearity, Normality, Homogeneity, Influential values
hist(resid(MV_R2.lm5))          #Visual check of normal distribution residuals
bptest(MV_R2.lm1)               #Heteroscedascity full model

#Volume region 2 ~ Rest*TreatNeighbour*TreatEnrich (log transformation)
MV_R2.log.lm1 <- lm(log(Vol_2x2) ~ log(Rest_2x2)*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R2.log.lm1)

MV_R2.log.lm2 <- lm(log(Vol_2x2) ~ log(Rest_2x2)*TreatEnrich + log(Rest_2x2)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R2.log.lm2)

MV_R2.log.lm3 <- lm(log(Vol_2x2) ~ log(Rest_2x2)*TreatEnrich + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R2.log.lm3)

MV_R2.log.lm4 <- lm(log(Vol_2x2) ~ log(Rest_2x2) + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R2.log.lm4)

MV_R2.log.lm5 <- lm(log(Vol_2x2) ~ log(Rest_2x2) + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R2.log.lm5)

#Model selection
anova(MV_R2.log.lm1, MV_R2.log.lm2)
anova(MV_R2.log.lm2, MV_R2.log.lm3)
anova(MV_R2.log.lm3, MV_R2.log.lm4)
anova(MV_R2.log.lm4, MV_R2.log.lm5)

#Check assumptions on final model
plot(MV_R2.log.lm5)                 #Linearity, Normality, Homogeneity, Influential values
hist(resid(MV_R2.log.lm1))          #Visual check of normal distribution residuals
bptest(MV_R2.log.lm1)               #Heteroscedascity full model

##Region 3: Optic tectum
#Volume region 3 ~ Rest*TreatNeighbour*TreatEnrich (no log transformation)
MV_R3.lm1 <- lm(Vol_3x2 ~ Rest_3x2*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R3.lm1)

MV_R3.lm2 <- lm(Vol_3x2 ~ Rest_3x2*TreatEnrich + TreatEnrich*TreatNeighbour + Rest_3x2*TreatNeighbour, data = MV)
summary(MV_R3.lm2)

MV_R3.lm3 <- lm(Vol_3x2 ~ Rest_3x2*TreatEnrich + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R3.lm3)

MV_R3.lm4 <- lm(Vol_3x2 ~ Rest_3x2*TreatEnrich + TreatNeighbour, data = MV)
summary(MV_R3.lm4)

MV_R3.lm5 <- lm(Vol_3x2 ~ Rest_3x2 + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R3.lm5)

#Model selection
anova(MV_R3.lm1, MV_R3.lm2) 
anova(MV_R3.lm2, MV_R3.lm3) 
anova(MV_R3.lm3, MV_R3.lm4) 
anova(MV_R3.lm4, MV_R3.lm5) 

#Check assumptions on final model
plot(MV_R3.lm5)                 #Linearity, Normality, Homogeneity, Influential values
bptest(MV_R3.lm1)               #Heteroscedascity full model

#Volume region 3 ~ Rest*TreatNeighbour*TreatEnrich (log transformation)
MV_R3.log.lm1 <- lm(log(Vol_3x2) ~ log(Rest_3x2)*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R3.log.lm1)

MV_R3.log.lm2 <- lm(log(Vol_3x2) ~ log(Rest_3x2)*TreatEnrich + log(Rest_3x2)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R3.log.lm2)

MV_R3.log.lm3 <- lm(log(Vol_3x2) ~ log(Rest_3x2)*TreatEnrich + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R3.log.lm3)

MV_R3.log.lm4 <- lm(log(Vol_3x2) ~ log(Rest_3x2)*TreatEnrich + TreatNeighbour, data = MV)
summary(MV_R3.log.lm4)

MV_R3.log.lm5 <- lm(log(Vol_3x2) ~ log(Rest_3x2) + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R3.log.lm5)

#Model selection
anova(MV_R3.log.lm1, MV_R3.log.lm2)
anova(MV_R3.log.lm2, MV_R3.log.lm3)
anova(MV_R3.log.lm3, MV_R3.log.lm4)
anova(MV_R3.log.lm4, MV_R3.log.lm5) #P<0.05, lm4 final model

#Check assumptions on final model
plot(MV_R3.log.lm4)                 #Linearity, Normality, Homogeneity, Influential values
bptest(MV_R3.log.lm1)               #Heteroscedascity full model

##Region 4: Cerebellum
#Volume region 4 ~ Rest*TreatNeighbour*TreatEnrich (no log transformation)
MV_R4.lm1 <- lm(Vol_4 ~ Rest_4*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R4.lm1)

MV_R4.lm2 <- lm(Vol_4 ~ Rest_4*TreatEnrich + TreatEnrich*TreatNeighbour + Rest_4*TreatNeighbour, data = MV)
summary(MV_R4.lm2)

MV_R4.lm3 <- lm(Vol_4 ~ Rest_4*TreatEnrich + Rest_4*TreatNeighbour, data = MV)
summary(MV_R4.lm3)

MV_R4.lm4 <- lm(Vol_4 ~ Rest_4*TreatEnrich + TreatNeighbour, data = MV)
summary(MV_R4.lm4)

MV_R4.lm5 <- lm(Vol_4 ~ Rest_4 + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R4.lm5)

#Model selection
anova(MV_R4.lm1, MV_R4.lm2) 
anova(MV_R4.lm2, MV_R4.lm3) 
anova(MV_R4.lm3, MV_R4.lm4) 
anova(MV_R4.lm4, MV_R4.lm5) 

#Check assumptions on final model
plot(MV_R4.lm5)                   #Linearity, Normality, Homogeneity, Influential values
hist(resid(MV_R4.lm5))            #Visual check of normal distribution residuals
bptest(MV_R4.lm1)                 #Heteroscedascity full model

#Volume region 4 ~ Rest*TreatNeighbour*TreatEnrich (log transformation)
MV_R4.log.lm1 <- lm(log(Vol_4) ~ log(Rest_4)*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R4.log.lm1)

MV_R4.log.lm2 <- lm(log(Vol_4) ~ log(Rest_4)*TreatEnrich + log(Rest_4)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R4.log.lm2)

MV_R4.log.lm3 <- lm(log(Vol_4) ~ log(Rest_4)*TreatEnrich + log(Rest_4)*TreatNeighbour, data = MV)
summary(MV_R4.log.lm3)

MV_R4.log.lm4 <- lm(log(Vol_4) ~ log(Rest_4)*TreatEnrich + TreatNeighbour, data = MV)
summary(MV_R4.log.lm4)

MV_R4.log.lm5 <- lm(log(Vol_4) ~ log(Rest_4) + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R4.log.lm5)

#Model selection
anova(MV_R4.log.lm1, MV_R4.log.lm2)
anova(MV_R4.log.lm2, MV_R4.log.lm3)
anova(MV_R4.log.lm3, MV_R4.log.lm4)
anova(MV_R4.log.lm4, MV_R4.log.lm5)

#Check assumptions on final model
plot(MV_R4.log.lm5)                   #Linearity, Normality, Homogeneity, Influential values
hist(resid(MV_R4.log.lm5))            #Visual check of normal distribution residuals
bptest(MV_R4.log.lm1)                 #Heteroscedascity full model

##Region 5: Dorsal medula
#Volume region 5 ~ Rest*TreatNeighbour*TreatEnrich (no log transformation)
MV_R5.lm1 <- lm(Vol_5 ~ Rest_5*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R5.lm1)

MV_R5.lm2 <- lm(Vol_5 ~ Rest_5*TreatEnrich + TreatEnrich*TreatNeighbour + Rest_5*TreatNeighbour, data = MV)
summary(MV_R5.lm2)

MV_R5.lm3 <- lm(Vol_5 ~ TreatEnrich*TreatNeighbour + Rest_5*TreatNeighbour, data = MV)
summary(MV_R5.lm3)

MV_R5.lm4 <- lm(Vol_5 ~ TreatEnrich*TreatNeighbour + Rest_5, data = MV)
summary(MV_R5.lm4)

MV_R5.lm5 <- lm(Vol_5 ~ Rest_5 + TreatNeighbour +TreatEnrich, data = MV)
summary(MV_R5.lm5)

#Model selection
anova(MV_R5.lm1, MV_R5.lm2) 
anova(MV_R5.lm2, MV_R5.lm3) 
anova(MV_R5.lm3, MV_R5.lm4) 
anova(MV_R5.lm4, MV_R5.lm5) 

#Check assumptions on final model
plot(MV_R5.lm5)                   #Linearity, Normality, Homogeneity, Influential values
bptest(MV_R5.lm1)                 #Heteroscedascity full model

#Volume region 5 ~ Rest*TreatNeighbour*TreatEnrich (log transformation)
MV_R5.log.lm1 <- lm(log(Vol_5) ~ log(Rest_5)*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R5.log.lm1)

MV_R5.log.lm2 <- lm(log(Vol_5) ~ log(Rest_5)*TreatEnrich + log(Rest_5)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R5.log.lm2)

MV_R5.log.lm3 <- lm(log(Vol_5) ~ log(Rest_5)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R5.log.lm3)

MV_R5.log.lm4 <- lm(log(Vol_5) ~ log(Rest_5) + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R5.log.lm4)

MV_R5.log.lm5 <- lm(log(Vol_5) ~ log(Rest_5) + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R5.log.lm5)

#Model selection
anova(MV_R5.log.lm1, MV_R5.log.lm2)
anova(MV_R5.log.lm2, MV_R5.log.lm3)
anova(MV_R5.log.lm3, MV_R5.log.lm4)
anova(MV_R5.log.lm4, MV_R5.log.lm5)

#Check assumptions on final model
plot(MV_R5.log.lm5)                   #Linearity, Normality, Homogeneity, Influential values
bptest(MV_R5.log.lm1)                 #Heteroscedascity full model

##Region 6: Hypothalamus
#Volume region 6 ~ Rest*TreatNeighbour*TreatEnrich (no log transformation)
MV_R6.lm1 <- lm(Vol_6x2 ~ Rest_6x2*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R6.lm1)

MV_R6.lm2 <- lm(Vol_6x2 ~ Rest_6x2*TreatEnrich + TreatEnrich*TreatNeighbour + Rest_6x2*TreatNeighbour, data = MV)
summary(MV_R6.lm2)

MV_R6.lm3 <- lm(Vol_6x2 ~ Rest_6x2*TreatEnrich  + Rest_6x2*TreatNeighbour, data = MV)
summary(MV_R6.lm3)

MV_R6.lm4 <- lm(Vol_6x2 ~ TreatEnrich  + Rest_6x2*TreatNeighbour, data = MV)
summary(MV_R6.lm4)

MV_R6.lm5 <- lm(Vol_6x2 ~ Rest_6x2 + TreatNeighbour + TreatEnrich, data = MV)
summary(MV_R6.lm5)

#Model selection
anova(MV_R6.lm1, MV_R6.lm2) 
anova(MV_R6.lm2, MV_R6.lm3) 
anova(MV_R6.lm3, MV_R6.lm4)  
anova(MV_R6.lm4, MV_R6.lm5) #P<0.05, lm4 final model

#Check assumptions on final model
plot(MV_R6.lm4)                   #Linearity, Normality, Homogeneity, Influential values
hist(resid(MV_R6.lm4))            #Visual check of normal distribution residuals
bptest(MV_R6.lm1)                 #Heteroscedascity full model

#Volume region 6 ~ Rest*TreatNeighbour*TreatEnrich (log transformation)
MV_R6.log.lm1 <- lm(log(Vol_6x2) ~ log(Rest_6x2)*TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R6.log.lm1)

MV_R6.log.lm2 <- lm(log(Vol_6x2) ~ log(Rest_6x2)*TreatEnrich + log(Rest_6x2)*TreatNeighbour + TreatEnrich*TreatNeighbour, data = MV)
summary(MV_R6.log.lm2)

MV_R6.log.lm3 <- lm(log(Vol_6x2) ~ log(Rest_6x2)*TreatEnrich + log(Rest_6x2)*TreatNeighbour, data = MV)
summary(MV_R6.log.lm3)

MV_R6.log.lm4 <- lm(log(Vol_6x2) ~ TreatEnrich + log(Rest_6x2)*TreatNeighbour, data = MV)
summary(MV_R6.log.lm4)

MV_R6.log.lm5 <- lm(log(Vol_6x2) ~ log(Rest_6x2) + TreatEnrich + TreatNeighbour, data = MV)
summary(MV_R6.log.lm5)

#Model selection
anova(MV_R6.log.lm1, MV_R6.log.lm2)
anova(MV_R6.log.lm2, MV_R6.log.lm3)
anova(MV_R6.log.lm3, MV_R6.log.lm4)
anova(MV_R6.log.lm4, MV_R6.log.lm5) #P<0.05, lm4 is final model

#Check assumptions on final model
plot(MV_R6.log.lm4)                   #Linearity, Normality, Homogeneity, Influential values
bptest(MV_R6.log.lm1)                 #Heteroscedascity full model

######################### Graphs ###########################
library(ggplot2)
library(ggpubr)
library(gridExtra)
library(grid)
library(gtable) 

shared_theme <-  theme_classic() +
  theme(
    axis.title.x = element_text(size = 11, family = "sans", face = "plain"),
    axis.text.x = element_text(size = 11, family = "sans", face = "plain"),
    axis.title.y = element_text(size = 11, family = "sans", face = "plain", margin = margin(t = 1)),
    axis.text.y  = element_text(size = 11, family = "sans", face = "plain", margin = margin(r = 1)),
    legend.position = "none",
    plot.margin = unit(c(5,5,5,5), "pt"))
 

#Standard length (body size) vs Brain weight
BW0 <- ggplot(MV, aes(x = SL_mm, y = Brain_Weight_mg, color = TreatNeighbour, linetype = TreatEnrich, group = interaction(TreatNeighbour, TreatEnrich))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) +
  scale_color_manual(values = c("#999999", "#f59f8c"), labels = c("Absent", "Present")) +
  scale_linetype_manual(values = c("solid", "dotted"), labels = c("Absent", "Present")) +
  labs(linetype = "Environmental enrichment", color = "Visual neighbour", 
       x = bquote("Body size (mm)"), #"*mm^{phantom(3)}*")"),
       y = bquote("Brain weight (mg)")) +
  scale_x_continuous(n.breaks = 4, limits = c(30, 45)) +
  scale_y_continuous(limits = c(5, 11), breaks = seq(5, 11, by = 2)) +
  guides(
    linetype = guide_legend(override.aes = list(color = "black"),
                            title = "Environmental Enrichment", direction = "vertical"),
    color = guide_legend(title = "Visual Neighbour", direction = "vertical")
  ) + 
  shared_theme +
  theme(axis.title.x = element_text(margin = margin(t = 7.5)))
BW0

#Olfactory bulb vs Rest of brain volume
OB1 <- ggplot(MV, aes(x = Rest_1x2, y = Vol_1x2, color = TreatNeighbour, linetype = TreatEnrich, group = interaction(TreatNeighbour, TreatEnrich))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) +
  scale_color_manual(values = c("#999999", "#f59f8c"), labels = c("Absent", "Present")) +
  scale_linetype_manual(values = c("solid", "dotted"), labels = c("Absent", "Present")) +
  labs(linetype = "Environmental enrichment", color = "Visual neighbour",
       x = bquote("Volume rest of the brain"~(mm^3)),
       y = bquote("Olfactory bulb"~(mm^3))) +
  scale_x_continuous(n.breaks = 5, limits = c(4.5, 8.5)) +
  scale_y_continuous(limits = c(0.03, 0.12), breaks = seq(0.03, 0.12, by = 0.03)) +
  guides(
    linetype = guide_legend(override.aes = list(color = "black"))  # override color in linetype legend
  ) +
  shared_theme
OB1

#Telencephalon vs Rest of brain volume
TC2 <- ggplot(MV, aes(x = Rest_2x2, y = Vol_2x2, color = TreatNeighbour, linetype = TreatEnrich, group = interaction(TreatNeighbour, TreatEnrich))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) +
  scale_color_manual(values = c("#999999", "#f59f8c"), labels = c("Absent", "Present")) +
  scale_linetype_manual(values = c("solid", "dotted"), labels = c("Absent", "Present")) +
  labs(linetype = "Environmental enrichment", color = "Visual neighbour",
       x = bquote("Volume rest of the brain"~(mm^3)),
       y = bquote("Telencephalon"~(mm^3))) +
  scale_x_continuous(n.breaks = 4, limits = c(3.5, 7.5)) +
  scale_y_continuous(limits = c(0.8, 1.7), breaks = seq(0.8, 1.7, by = 0.3)) +
  guides(
    linetype = guide_legend(override.aes = list(color = "black")) 
  ) +
  shared_theme
TC2

#Optic tectum vs Rest of brain volume
OT3 <- ggplot(MV, aes(x = Rest_3x2, y = Vol_3x2, color = TreatNeighbour, linetype = TreatEnrich, group = interaction(TreatNeighbour, TreatEnrich))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) +
  scale_color_manual(values = c("#999999", "#f59f8c"), labels = c("Absent", "Present")) +
  scale_linetype_manual(values = c("solid", "dotted"), labels = c("Absent", "Present")) +
  labs(linetype = "Environmental enrichment", color = "Visual neighbour",
       x = bquote("Volume rest of the brain"~(mm^3)),
       y = bquote("Optic tectum"~(mm^3))) +
  scale_x_continuous(limits = c(2.5,5.5), breaks = c(2, 3, 4, 5, 6)) +
  scale_y_continuous(limits = c(1, 5), breaks = seq(1, 5, by = 1)) +
  guides(
    linetype = guide_legend(override.aes = list(color = "black"))  # override color in linetype legend
  ) +
  shared_theme
OT3

#Cerebellum vs Rest of brain volume
CB4 <- ggplot(MV, aes(x = Rest_4, y = Vol_4, color = TreatNeighbour, linetype = TreatEnrich, group = interaction(TreatNeighbour, TreatEnrich))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) +
  scale_color_manual(values = c("#999999", "#f59f8c"), labels = c("Absent", "Present")) +
  scale_linetype_manual(values = c("solid", "dotted"), labels = c("Absent", "Present")) +
  labs(linetype = "Environmental enrichment", color = "Visual neighbour",
       x = bquote("Volume rest of the brain"~(mm^3)),
       y = bquote("Cerebellum"~(mm^3))) +
  scale_x_continuous(limits = c(3.5, 7.5), breaks = c(3, 4, 5, 6, 7)) +
  scale_y_continuous(limits = c(0.4, 2.8), breaks = seq(0.4, 2.8, by = 0.8)) +
  guides(
    linetype = guide_legend(override.aes = list(color = "black"))  # override color in linetype legend
  ) +
  shared_theme
CB4

#Dorsal medulla vs Rest of brain volume
DM5 <- ggplot(MV, aes(x = Rest_5, y = Vol_5, color = TreatNeighbour, linetype = TreatEnrich, group = interaction(TreatNeighbour, TreatEnrich))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) +
  scale_color_manual(values = c("#999999", "#f59f8c"), labels = c("Absent", "Present")) +
  scale_linetype_manual(values = c("solid", "dotted"), labels = c("Absent", "Present")) +
  labs(linetype = "Environmental enrichment", color = "Visual neighbour",
       x = bquote("Volume rest of the brain"~(mm^3)),
       y = bquote("Dorsal medulla"~(mm^3))) +
  scale_x_continuous(limits = c(4.5, 8.5), breaks = c(4, 5, 6, 7, 8)) +
  scale_y_continuous(limits = c(0.15, 0.45), breaks = seq(0.15, 0.45, by = 0.1)) +
  guides(
    linetype = guide_legend(override.aes = list(color = "black"))  # override color in linetype legend
  ) +
  shared_theme
DM5

#Hypothalamus vs Rest of brain volume
HT6 <- ggplot(MV, aes(x = Rest_6x2, y = Vol_6x2, color = TreatNeighbour, linetype = TreatEnrich, group = interaction(TreatNeighbour, TreatEnrich))) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE, fullrange = TRUE) +
  scale_color_manual(values = c("#999999", "#f59f8c"), labels = c("Absent", "Present")) +
  scale_linetype_manual(values = c("solid", "dotted"), labels = c("Absent", "Present")) +
  labs(linetype = "Environmental enrichment", color = "Visual neighbour",
       x = bquote("Volume rest of the brain"~(mm^3)),
       y = bquote("Hypothalamus"~(mm^3))) +
  scale_x_continuous(limits = c(3.5, 7.5), breaks = c(3, 4, 5, 6, 7)) +
  scale_y_continuous(limits = c(0.3, 1.5), breaks = seq(0.3, 1.5, by = 0.4)) +
  guides(
    linetype = guide_legend(override.aes = list(color = "black"))  # override color in linetype legend
  ) +
  shared_theme
HT6

#Create shared legend
legend_plot <- HT6 + theme(legend.position = "left",
                            legend.title = element_text(size = 11)) +
  guides(color = guide_legend(), fill = "none", shape = "none")

lgnd <- gtable_filter(ggplot_gtable(ggplot_build(legend_plot)), "guide-box") 

combined_legend <- ggarrange(as_ggplot(lgnd), ncol = 1)

#Arrange first row (3 plots)
toprow <- ggarrange(
  OB1, TC2, OT3, combined_legend,
  ncol = 4, nrow = 1,
  labels = c("A", "B", "C", ""),
  align = "hv",
  legend = "none")

#Arrange second row (3 plots)
bottomrow <- ggarrange(
  CB4, DM5, HT6, BW0,
  ncol = 4, nrow = 1,
  labels = c("D", "E", "F", "G"),
  align = "hv",
  legend = "none")

#Create final figure with 7 plots and a legend
final_layout <- grid.arrange(
  toprow,
  bottomrow,
  heights = unit.c(
    unit(1.5, "null"),
    unit(1.5, "null")))

######################### Means, SD, SE per region ###########################
#Calculate mean, SD, SE for each region
N <- nrow(MV)
region_data <- MV[, grepl("^Vol", names(MV))]

#Compute mean, SD, SE
results <- data.frame(
  Mean = colMeans(region_data),
  SD = apply(region_data, 2, sd),
  SE = apply(region_data, 2, function(x) sd(x) / sqrt(N)))

#Add RSE%
results$RSE_percent <- (results$SE / results$Mean) * 100

#Show results
print(results)
