#### Title: Supplementary Analysis File for “Sex-specific effects of cooperative breeding and colonial nesting on prosociality in corvids” ####


#######################################################################################


#### 1) Pielou’s J’ calculations

Pielou's evenness index J’ is defined as J’ = H’ / H’max (after E. C. Pielou, Mathematical Ecology (Wiley, New York, 1977))

H’ is the number derived from the Shannon diversity index
H’max is the maximum possible value of H’ (if all proportions are equal)

H’ = - (ln p1^p1 + ln p2^p2 + … ln pN^pN)

p is the proportion of food pieces obtained by a specific bird
N is the number of birds per group

### Example Siberian jay group 2 (N=3)

Total number of available food pieces = N*5 = 15

H’max = - (ln 0.33^0.33 + ln 0.33^0.33 + ln 0.33^0.33) = 1.099

# Session 1

pMGMB = 3 / 15 = 0.2
pOO = 9 / 15 = 0.6
pMBV = 3/ 15 = 0.2

H’Session1 = - (ln 0.2^0.2 + ln 0.6^0.6 + ln 0.2^0.2) = 0.950

J’Session1 = H’Session1 / H’max = 0.950 / 1.099 = 0.86

# Session 2

pMGMB = 6 / 15 = 0.4
pOO = 6 / 15 = 0.4
pMBV = 3/ 15 = 0.2

H’Session2 = - (ln 0.4^0.4 + ln 0.4^0.4 + ln 0.2^0.2) = 1.055

J’Session2 = H’Session2 / H’max = 1.055 / 1.099 = 0.96

# Average of sessions 1 and 2

J’ = (J’Session1 + J’Session2) / 2 = (0.86 + 0.96) / 2 = 0.91

#######################################################################################


#### 2) R script for the linear mixed-effects model on the full dataset and the model selection and averaging procedure

### Load full dataset

Corvid_GSP_Dataset <- read.csv("Enter file path for Corvid_GSP_Data.csv")

### Select only the birds that passed the criterion for Phase IV and for whom the variable "Sex" is known

library(dplyr)

data <-
  Corvid_GSP_Dataset %>%
  filter(Habituated_IV == "yes" & Sex != "NA")

### Calculate the linear mixed-effects model

library(lme4)

LMM.full <- lmer(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size+(1|Species/Group_ID), data=data, REML=FALSE)

# boundary (singular) fit: see ?isSingular

### Derive all possible sub-models with all possible combinations from the set of factors

library(MuMIn)

options(na.action = "na.fail")
dredge(LMM.full)

### Select and average the top 2AICc models

ms.lmm.full <- dredge(LMM.full)
model.avg(ms.lmm.full, subset = delta <= 2)
avg.lmm.full.d2 <- model.avg(ms.lmm.full, subset = delta <= 2)

### Model results

summary(avg.lmm.full.d2)
sw(avg.lmm.full.d2)

########################################################################################


#### 3) R script for the linear model on the full dataset and the model selection and averaging procedure

### Requires loading the same packages as above

### 3.a) Calculate the linear model

LM.full <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=data)

### Derive all possible sub-models with all possible combinations from the set of factors

dredge(LM.full)

### Select and average the top 2AICc models

ms.lm.full <- dredge(LM.full)
model.avg(ms.lm.full, subset = delta <= 2)
avg.lm.full.d2 <- model.avg(ms.lm.full, subset = delta <= 2)

### Model results with threshold set to delta AICc≤2

summary(avg.lm.full.d2)
sw(avg.lm.full.d2)

### Select and average the top 7AICc models

model.avg(ms.lm.full, subset = delta <= 7)
avg.lm.full.d7 <- model.avg(ms.lm.full, subset = delta <= 7)

### Model results with threshold set to delta AICc≤7

summary(avg.lm.full.d7)
sw(avg.lm.full.d7)

#### 3.b) Checking the robustness of the full model

### No azure-winged magpies

totalA <-
  data %>%
  filter(Species != "Azure-winged magpie")

lm.totalA <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalA)
ms.lm.totalA <- dredge(lm.totalA)
model.avg(ms.lm.totalA, subset = delta <= 2)
avg.model.delta2.lm.totalA <- model.avg(ms.lm.totalA, subset = delta <= 2)
summary(avg.model.delta2.lm.totalA)
sw(avg.model.delta2.lm.totalA)

### No carrion crows

totalB <-
  data %>%
  filter(Species != "Carrion crow")

lm.totalB <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalB)
ms.lm.totalB <- dredge(lm.totalB)
model.avg(ms.lm.totalB, subset = delta <= 2)
avg.model.delta2.lm.totalB <- model.avg(ms.lm.totalB, subset = delta <= 2)
summary(avg.model.delta2.lm.totalB)
sw(avg.model.delta2.lm.totalB)

### No large-billed crows

totalC <-
  data %>%
  filter(Species != "Large-billed crow")

lm.totalC <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalC)
ms.lm.totalC <- dredge(lm.totalC)
model.avg(ms.lm.totalC, subset = delta <= 2)
avg.model.delta2.lm.totalC <- model.avg(ms.lm.totalC, subset = delta <= 2)
summary(avg.model.delta2.lm.totalC)
sw(avg.model.delta2.lm.totalC)

### No New-Caledonian crows

totalD <-
  data %>%
  filter(Species != "New-Caledonian crow")

lm.totalD <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalD)
ms.lm.totalD <- dredge(lm.totalD)
model.avg(ms.lm.totalD, subset = delta <= 2)
avg.model.delta2.lm.totalD <- model.avg(ms.lm.totalD, subset = delta <= 2)
summary(avg.model.delta2.lm.totalD)
sw(avg.model.delta2.lm.totalD)

### No common ravens

totalE <-
  data %>%
  filter(Species != "Common raven")

lm.totalE <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalE)
ms.lm.totalE <- dredge(lm.totalE)
model.avg(ms.lm.totalE, subset = delta <= 2)
avg.model.delta2.lm.totalE <- model.avg(ms.lm.totalE, subset = delta <= 2)
summary(avg.model.delta2.lm.totalE)
sw(avg.model.delta2.lm.totalE)

# Only one model in selection of top 2AICc models - identify model and run as simple linear model

dredge(lm.totalE)
lm.totalE.reduced <- lm(PhaseIV_Landing_Test~Cooperation+Nesting+Sex+Cooperation:Sex+Nesting:Sex, data=totalE)
summary(lm.totalE.reduced)

### No rooks

totalF <-
  data %>%
  filter(Species != "Rook")

lm.totalF <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalF)
ms.lm.totalF <- dredge(lm.totalF)
model.avg(ms.lm.totalF, subset = delta <= 2)
avg.model.delta2.lm.totalF <- model.avg(ms.lm.totalF, subset = delta <= 2)
summary(avg.model.delta2.lm.totalF)
sw(avg.model.delta2.lm.totalF)

### No Siberian jays

totalG <-
  data %>%
  filter(Species != "Siberian jay")

lm.totalG <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalG)
ms.lm.totalG <- dredge(lm.totalG)
model.avg(ms.lm.totalG, subset = delta <= 2)
avg.model.delta2.lm.totalG <- model.avg(ms.lm.totalG, subset = delta <= 2)
summary(avg.model.delta2.lm.totalG)
sw(avg.model.delta2.lm.totalG)

### No Eurasian jackdwas

totalH <-
  data %>%
  filter(Species != "Eurasian jackdaw")

lm.totalH <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=totalH)
ms.lm.totalH <- dredge(lm.totalH)

model.avg(ms.lm.totalH, subset = delta <= 2)
avg.model.delta2.lm.totalH <- model.avg(ms.lm.totalH, subset = delta <= 2)
summary(avg.model.delta2.lm.totalH)
sw(avg.model.delta2.lm.totalH)


########################################################################################


#### 4) R script for doing the analysis separately for female and male birds

### Requires loading the same packages as above


#### 4.a) Select only females

females <-
  data %>%
  filter(Sex == "female")

### Calculate the linear mixed-effects model for the female birds

LMM.females <- lmer(PhaseIV_Landing_Test~Cooperation*Nesting+(1|Species/Group_ID), data=females, REML=FALSE)

# boundary (singular) fit: see ?isSingular

### Model selection and averaging procedure (not included in the paper due to zero variance of the random factors)

dredge(LMM.females)
ms.lmm.females <- dredge(LMM.females)
model.avg(ms.lmm.females, subset = delta <= 2)
avg.lmm.females.d2 <- model.avg(ms.lmm.females, subset = delta <= 2)
summary(avg.lmm.females.d2)
sw(avg.lmm.females.d2)

### Calculate the linear model for the female birds

LM.females <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=females)

### Derive all possible sub-models with all possible combinations from the set of factors

dredge(LM.females)

### Select and average the top 2AICc models

ms.lm.females <- dredge(LM.females)
model.avg(ms.lm.females, subset = delta <= 2)
avg.lm.females.d2 <- model.avg(ms.lm.females, subset = delta <= 2)

### Model results

summary(avg.lm.females.d2)
sw(avg.lm.females.d2)


#### 4.b) Select only males

males <-
  data %>%
  filter(Sex == "male")

### Calculate the linear mixed-effects model for the male birds

LMM.males <- lmer(PhaseIV_Landing_Test~Cooperation*Nesting+(1|Species/Group_ID), data=males, REML=FALSE)

# boundary (singular) fit: see ?isSingular

### Model selection and averaging procedure (not included in the paper due to zero variance of the random factors)

dredge(LMM.males)

# Only 1 model (inlcuding only the factor "Nesting") in the selection of top 2AICc models

### Calculate a linear mixed-effects model including only the factor "Nesting"

LMM.males.reduced <- lmer(PhaseIV_Landing_Test~Nesting+(1|Species/Group_ID), data=males, REML=FALSE)
# boundary (singular) fit: see ?isSingular

summary(LMM.males.reduced)
confint(LMM.males.reduced)

### Calculate the linear model for the male birds

LM.males <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=males)

### Derive all possible sub-models with all possible combinations from the set of factors

dredge(LM.males)

### Select and average the top 2AICc models

ms.lm.males <- dredge(LM.males)
model.avg(ms.lm.males, subset = delta <= 2)
avg.lm.males.d2 <- model.avg(ms.lm.males, subset = delta <= 2)

### Model results

summary(avg.lm.males.d2)
sw(avg.lm.males.d2)


#### 4.c) Checking the robustness of the single-sex models

### Models with the female birds

### No azure-winged magpies

femalesA <-
  females %>%
  filter(Species != "Azure-winged magpie")

lm.femalesA <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesA)
ms.lm.femalesA <- dredge(lm.femalesA)
model.avg(ms.lm.femalesA, subset = delta <= 2)
avg.model.delta2.lm.femalesA <- model.avg(ms.lm.femalesA, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesA)
sw(avg.model.delta2.lm.femalesA)

### No carrion crows

femalesB <-
  females %>%
  filter(Species != "Carrion crow")

lm.femalesB <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesB)
ms.lm.femalesB <- dredge(lm.femalesB)
model.avg(ms.lm.femalesB, subset = delta <= 2)
avg.model.delta2.lm.femalesB <- model.avg(ms.lm.femalesB, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesB)
sw(avg.model.delta2.lm.femalesB)

### No large-billed crows

femalesC <-
  females %>%
  filter(Species != "Large-billed crow")

lm.femalesC <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesC)
ms.lm.femalesC <- dredge(lm.femalesC)
model.avg(ms.lm.femalesC, subset = delta <= 2)
avg.model.delta2.lm.femalesC <- model.avg(ms.lm.femalesC, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesC)
sw(avg.model.delta2.lm.femalesC)

### No New-Caledonion crows

femalesD <-
  females %>%
  filter(Species != "New-Caledonian crow")

lm.femalesD <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesD)
ms.lm.femalesD <- dredge(lm.femalesD)
model.avg(ms.lm.femalesD, subset = delta <= 2)
avg.model.delta2.lm.femalesD <- model.avg(ms.lm.femalesD, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesD)
sw(avg.model.delta2.lm.femalesD)

### No common ravens

femalesE <-
  females %>%
  filter(Species != "Common raven")

lm.femalesE <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesE)
ms.lm.femalesE <- dredge(lm.femalesE)
model.avg(ms.lm.femalesE, subset = delta <= 2)
avg.model.delta2.lm.femalesE <- model.avg(ms.lm.femalesE, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesE)
sw(avg.model.delta2.lm.femalesE)

# Only one model in selection of top 2AICc models - identify model and run as simple linear model

dredge(lm.femalesE)
lm.femalesE.coop <- lm(PhaseIV_Landing_Test~Cooperation, data=femalesE)
summary(lm.femalesE.coop)

### No rooks

femalesF <-
  females %>%
  filter(Species != "Rook")

lm.femalesF <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesF)
ms.lm.femalesF <- dredge(lm.femalesF)
model.avg(ms.lm.femalesF, subset = delta <= 2)
avg.model.delta2.lm.femalesF <- model.avg(ms.lm.femalesF, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesF)
sw(avg.model.delta2.lm.femalesF)

### No Siberian jays

femalesG <-
  females %>%
  filter(Species != "Siberian jay")

lm.femalesG <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesG)
ms.lm.femalesG <- dredge(lm.femalesG)
model.avg(ms.lm.femalesG, subset = delta <= 2)
avg.model.delta2.lm.femalesG <- model.avg(ms.lm.femalesG, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesG)
sw(avg.model.delta2.lm.femalesG)

### No Eurasian jackdaws

femalesH <-
  females %>%
  filter(Species != "Eurasian jackdaw")

lm.femalesH <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=femalesH)
ms.lm.femalesH <- dredge(lm.femalesH)

model.avg(ms.lm.femalesH, subset = delta <= 2)
avg.model.delta2.lm.femalesH <- model.avg(ms.lm.femalesH, subset = delta <= 2)
summary(avg.model.delta2.lm.femalesH)
sw(avg.model.delta2.lm.femalesH)

### Simple group comparison

t.test(PhaseIV_Landing_Test~Cooperation, alternative="less", data=females)


### Models with the male birds

### No azure-winged magpies

malesA <-
  males %>%
  filter(Species != "Azure-winged magpie")

lm.malesA <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesA)
ms.lm.malesA <- dredge(lm.malesA)
model.avg(ms.lm.malesA, subset = delta <= 2)
avg.model.delta2.lm.malesA <- model.avg(ms.lm.malesA, subset = delta <= 2)
summary(avg.model.delta2.lm.malesA)
sw(avg.model.delta2.lm.malesA)

### No carrion crows

malesB <-
  males %>%
  filter(Species != "Carrion crow")

lm.malesB <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesB)
ms.lm.malesB <- dredge(lm.malesB)
model.avg(ms.lm.malesB, subset = delta <= 2)
avg.model.delta2.lm.malesB <- model.avg(ms.lm.malesB, subset = delta <= 2)
summary(avg.model.delta2.lm.malesB)
sw(avg.model.delta2.lm.malesB)

# Only one model in selection of top 2AICc models - identify model and run as simple linear model

dredge(lm.malesB)
lm.malesB.nest <- lm(PhaseIV_Landing_Test~Nesting, data=malesB)
summary(lm.malesB.nest)

### No large-billed crows

malesC <-
  males %>%
  filter(Species != "Large-billed crow")

lm.malesC <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesC)
ms.lm.malesC <- dredge(lm.malesC)
model.avg(ms.lm.malesC, subset = delta <= 2)
avg.model.delta2.lm.malesC <- model.avg(ms.lm.malesC, subset = delta <= 2)
summary(avg.model.delta2.lm.malesC)
sw(avg.model.delta2.lm.malesC)

### No New-Caledonian crows

malesD <-
  males %>%
  filter(Species != "New-Caledonian crow")

lm.malesD <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesD)
ms.lm.malesD <- dredge(lm.malesD)
model.avg(ms.lm.malesD, subset = delta <= 2)
avg.model.delta2.lm.malesD <- model.avg(ms.lm.malesD, subset = delta <= 2)
summary(avg.model.delta2.lm.malesD)
sw(avg.model.delta2.lm.malesD)

### No common ravens

malesE <-
  males %>%
  filter(Species != "Common raven")

lm.malesE <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesE)
ms.lm.malesE <- dredge(lm.malesE)
model.avg(ms.lm.malesE, subset = delta <= 2)
avg.model.delta2.lm.malesE <- model.avg(ms.lm.malesE, subset = delta <= 2)
summary(avg.model.delta2.lm.malesE)
sw(avg.model.delta2.lm.malesE)


### No rooks

malesF <-
  males %>%
  filter(Species != "Rook")

lm.malesF <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesF)
ms.lm.malesF <- dredge(lm.malesF)
model.avg(ms.lm.malesF, subset = delta <= 2)
avg.model.delta2.lm.malesF <- model.avg(ms.lm.malesF, subset = delta <= 2)
summary(avg.model.delta2.lm.malesF)
sw(avg.model.delta2.lm.malesF)

### No Siberian jays

malesG <-
  males %>%
  filter(Species != "Siberian jay")

lm.malesG <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesG)
ms.lm.malesG <- dredge(lm.malesG)
model.avg(ms.lm.malesG, subset = delta <= 2)
avg.model.delta2.lm.malesG <- model.avg(ms.lm.malesG, subset = delta <= 2)
summary(avg.model.delta2.lm.malesG)
sw(avg.model.delta2.lm.malesG)

### No Eurasian jackdaws

malesH <-
  males %>%
  filter(Species != "Eurasian jackdaw")

lm.malesH <- lm(PhaseIV_Landing_Test~Cooperation*Nesting, data=malesH)
ms.lm.malesH <- dredge(lm.malesH)

model.avg(ms.lm.malesH, subset = delta <= 2)
avg.model.delta2.lm.malesH <- model.avg(ms.lm.malesH, subset = delta <= 2)
summary(avg.model.delta2.lm.malesH)
sw(avg.model.delta2.lm.malesH)

### Simple group comparison

t.test(PhaseIV_Landing_Test~Nesting, alternative="greater", data=males)


############################################################################################


#### 5) R script for doing the analysis without the Siberian jays

### Requires loading the same packages as above

### Exclude the data from the Siberian jays

data.woSibjays <-
  data %>%
  filter(Species != "Siberian jay")

### Calculate the linear mixed-effects model without the Siberian jays

LMM.woSibjays <- lmer(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size+(1|Species/Group_ID), data=data.woSibjays, REML=FALSE)

# boundary (singular) fit: see ?isSingular

### Model selection and averaging procedure (not included in the paper due to zero variance of the random factors)

dredge(LMM.woSibjays)
ms.lmm.woSibjays <- dredge(LMM.woSibjays)
model.avg(ms.lmm.woSibjays, subset = delta <= 2)
avg.lmm.woSibjays.d2 <- model.avg(ms.lmm.woSibjays, subset = delta <= 2)
summary(avg.lmm.woSibjays.d2)
sw(avg.lmm.woSibjays.d2)


### Calculate the linear model without the Siberian jays

LM.woSibjays <- lm(PhaseIV_Landing_Test~Cooperation*Nesting*Sex+Group_Size, data=data.woSibjays)

### Derive all possible sub-models with all possible combinations from the set of factors

dredge(LM.woSibjays)

### Select and average the top 2AICc models

ms.lm.woSibjays <- dredge(LM.woSibjays)
model.avg(ms.lm.woSibjays, subset = delta <= 2)
avg.lm.woSibjays.d2 <- model.avg(ms.lm.woSibjays, subset = delta <= 2)

### Model results

summary(avg.lm.woSibjays.d2)
sw(avg.lm.woSibjays.d2)

##########################################################################################


#### 6) R script for doing the phylogenetically controlled analysis

### Requires loading the same packages as above

### Get the phylogenetic tree and trim it down to our species

# The file “Jetztree1” is a consensus tree obtained from Gavin Thomas, from the publication W. Jetz, G. H. Thomas, J. B. Joy, K. Hartmann, A. O. Mooers, The global diversity of birds in space and time. Nature 491, 444–448 (2012).

library(geiger)

phylo <- read.nexus("Enter file path for Jetztree1.nex")

matches<-match(phylo$tip.label, data$tip_label, nomatch=0)
not<-subset(phylo$tip.label, matches==0)
phylo.small<-drop.tip(phylo, not)

name.check(phylo.small, data, data.names = data$tip_label)
names(data)


#### Do the phylogenetically controlled analysis with MCMCglmm

library(MCMCglmm)

### Make the inverse of the sum matrix of phylogenetic correlation

inv.phylo<-inverseA(phylo.small,nodes="TIPS",scale=TRUE)

### Default priors ###

prior<-list(G=list(G1=list(V=1,nu=0.02),G2=list(V=1,nu=0.02)), R=list(V=1,nu=0.02))

### Calculate the model

set.seed(11111)
model.phylo <- MCMCglmm(PhaseIV_Landing_Test~Nesting+Cooperation+Sex+Nesting:Sex+Cooperation:Sex,
                         random=~tip_label+Species, family="gaussian",
                         ginverse=list(tip_label=inv.phylo$Ainv),prior=prior, 
                         data=data,nitt=5000000,burnin=1000,thin=500)

### Model results

summary(model.phylo)

### Calculate posterior mean, posterior mode, and 95% credible interval of the phylogenetic signal λ  

lambda <- model.phylo$VCV[,'tip_label']/(model.phylo$VCV[,'tip_label']+model.phylo$VCV[,'units'])

mean(lambda)
posterior.mode(lambda)
HPDinterval(lambda)






