#analaysis file to Hykollarih2026FishLifeHistory_MS

library("lme4")
library("DHARMa")
library("MASS")
library("ape")
library("readxl")

#loading data----
bys<-read_xlsx("Hykollari2026FishLifeHistory_Data.xlsx", 1)
byi<-read_xlsx("Hykollari2026FishLifeHistory_Data.xlsx", 2)
byir<-subset(byi, byi$ICI>0 & byi$ICI<100) #reduced dataset without long ICI due to moving fish and without first clutch where we don't know ICI
growth<-read_xlsx("Hykollari2026FishLifeHistory_Data.xlsx", 4)
growthnnt<-subset(growth, !is.na(growth$ResSize)) #reduced dataset without NeoTet data
surv<-read_xlsx("Hykollari2026FishLifeHistory_Data.xlsx", 3)
surv$NumPropSurv<-as.numeric(surv$PropSurv)

#calculating sample sizes----
#sample size clutch size and inter-clutch interval----
n.eggs<-matrix(
  c(length(subset(byir$ID, byi$Species=="AltCom")),
    10,
    length(subset(byir$ID, byi$Species=="JulOrn")),
    length(subset(byir$ID, byi$Species=="LepElo")),
    length(subset(byir$ID, byi$Species=="NeoPul")),
    length(subset(byir$ID, byi$Species=="TelTem")),
    length(subset(byir$ID, byi$Species=="VarMoo")),
    length(subset(byir$ID, byi$Species=="AltCom")),
    10,
    length(subset(byir$ID, byir$Species=="JulOrn")),
    length(subset(byir$ID, byir$Species=="LepElo")),
    length(subset(byir$ID, byir$Species=="NeoPul")),
    length(subset(byir$ID, byir$Species=="TelTem")),
    length(subset(byir$ID, byir$Species=="VarMoo")),
    length(unique(subset(byir$ID, byir$Species=="AltCom"))),
    2,
    length(unique(subset(byir$ID, byir$Species=="JulOrn"))),
    length(unique(subset(byir$ID, byir$Species=="LepElo"))),
    length(unique(subset(byir$ID, byir$Species=="NeoPul"))),
    length(unique(subset(byir$ID, byir$Species=="TelTem"))),
    length(unique(subset(byir$ID, byir$Species=="VarMoo")))
  
  ),
  ncol=3, nrow=7)
colnames(n.eggs)<-c("AllClutches", "Clutches", "Females")
rownames(n.eggs)<-c("AC", "EC", "JO", "LE", "NP", "TT", "VM")

#sample size growth----
n.growth<-matrix(
  c(length(subset(growth$Length, growth$Species=="AltCom" )),
    length(subset(growth$Length, growth$Species=="EreCya" )),
    length(subset(growth$Length, growth$Species=="JulOrn" )),
    length(subset(growth$Length, growth$Species=="LepElo" )),
    length(subset(growth$Length, growth$Species=="NeoPul" )),
    length(subset(growth$Length, growth$Species=="TelTem" )),
    length(subset(growth$Length, growth$Species=="VarMoo" )),
    length(subset(growth$Length, growth$Species=="AltCom"  & growth$Sex=="f")),
    length(subset(growth$Length, growth$Species=="EreCya"  & growth$Sex=="f")),
    length(subset(growth$Length, growth$Species=="JulOrn"  & growth$Sex=="f")),
    length(subset(growth$Length, growth$Species=="LepElo"  & growth$Sex=="f")),
    length(subset(growth$Length, growth$Species=="NeoPul"  & growth$Sex=="f")),
    length(subset(growth$Length, growth$Species=="TelTem"  & growth$Sex=="f")),
    length(subset(growth$Length, growth$Species=="VarMoo"  & growth$Sex=="f")),
    length(subset(growth$Length, growth$Species=="AltCom"  & growth$Sex=="m")),
    length(subset(growth$Length, growth$Species=="EreCya"  & growth$Sex=="m")),
    length(subset(growth$Length, growth$Species=="JulOrn"  & growth$Sex=="m")),
    length(subset(growth$Length, growth$Species=="LepElo"  & growth$Sex=="m")),
    length(subset(growth$Length, growth$Species=="NeoPul"  & growth$Sex=="m")),
    length(subset(growth$Length, growth$Species=="TelTem"  & growth$Sex=="m")),
    length(subset(growth$Length, growth$Species=="VarMoo"  & growth$Sex=="m")),
    length(subset(growth$Length, growth$Species=="AltCom" & growth$AgeClass==0.5)),
    length(subset(growth$Length, growth$Species=="EreCya" & growth$AgeClass==0.5)),
    length(subset(growth$Length, growth$Species=="JulOrn" & growth$AgeClass==0.5)),
    length(subset(growth$Length, growth$Species=="LepElo" & growth$AgeClass==0.5)),
    length(subset(growth$Length, growth$Species=="NeoPul" & growth$AgeClass==0.5)),
    length(subset(growth$Length, growth$Species=="TelTem" & growth$AgeClass==0.5)),
    length(subset(growth$Length, growth$Species=="VarMoo" & growth$AgeClass==0.5))
    ),
  ncol=4, nrow=7)
colnames(n.growth)<-c("All", "Females", "Males", "0.5")
rownames(n.growth)<-c("AC", "EC", "JO", "LE", "NP", "TT", "VM")

#boxcox transformation of Clutch size----
par(mfrow=c(1, 1), mar=c(5, 5, 1, 1))
egg.m1.boxcox<-boxcox(ClutchSize~Species, data=byi)
egg.m1.boxcox.lambda<-egg.m1.boxcox$x[which.max(egg.m1.boxcox$y)]

byi$boxcoxcs<-(byi$ClutchSize^egg.m1.boxcox.lambda-1)/egg.m1.boxcox.lambda
byir<-subset(byi, byi$ICI>0 & byi$ICI<100) 

#making the tree for us based on Ronco et al. 2021 Nature----
par(mar=c(1, 1, 1, 1), mfrow=c(1, 1))
fulltree<-read.nexus("Hykollari2026FishLifeHistory_Tree.tre")
plot(fulltree)
ourfish<-c("Altcom", "Erecya", "Julorn", "Lepelo", "Neopul", "TelteS", "Varmoo") #vector with the names of our 7 focal species as used in Ronco et al.
lampfish<-c("Altcom", "Julorn", "Lepelo", "Neopul", "TelteS", "Varmoo") #vector with the names of our 6 focal lamprologine species as used in Ronco et al.
ourtree<-drop.tip(fulltree, which(!fulltree$tip.label %in% ourfish)) #dropping all species but our focal ones from the tree
lamptree<-drop.tip(fulltree, which(!fulltree$tip.label %in% lampfish)) #dropping all species but our focal ones from the tree
par(mar=c(1, 1, 1, 1), mfrow=c(1, 1))
plot(ourtree)
plot(ourtree, show.tip.label=FALSE)
plot(lamptree)
plot(lamptree, show.tip.label=FALSE)

###phylogenetic signal###----
#add taxonomic structure to bys----
bys$A<-c("Lamprologini", "Eretmodini", "Lamprologini", "Lamprologini", "Lamprologini", "Lamprologini", "Lamprologini")
bys$B<-c("1", "2", "3", "1", "3", "3", "3")
bys$C<-c("1", "2", "3", "1", "3", "3", "4")

#phylogenetic signal in average clutch size?----
cs.evo.formula<-AvgClutchSize~A/B/C
cs.evo.corr<-correlogram.formula(cs.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(cs.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> there is no evidence that phylogenetic relationships influence average clutch size

#phylogenetic signal in egg size?----
em.evo.formula<-AvgEggMass~A/B/C
em.evo.corr<-correlogram.formula(em.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(em.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(0.1, 0.8))
#-> egg size is influenced by phylogeny: small lamprologines differ from eretmodini and predatory lamprologines

#phylogenetic signal in water content?----
wc.evo.formula<-Water~A/B/C
wc.evo.corr<-correlogram.formula(wc.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(wc.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> there is no evidence that phylogenetic relationships influence water content of eggs

#phylogenetic signal in fat content?----
fc.evo.formula<-DryFat~A/B/C
fc.evo.corr<-correlogram.formula(fc.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(fc.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> there is no evidence that phylogenetic relationships influence fat content of eggs

#phylogenetic signal in protein content?----
pc.evo.formula<-DryProtein~A/B/C
pc.evo.corr<-correlogram.formula(pc.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(pc.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> there is no evidence that phylogenetic relationships influence protein content of eggs

#phylogenetic signal in protein content?----
ici.evo.formula<-AvgInterClutchInterval~B/C
ici.evo.corr<-correlogram.formula(ici.evo.formula, data=subset(bys, bys$A=="Lamprologini"))
par(mar=c(5, 5, 1, 1))
plot(ici.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> inter-clutch interval is influenced by phylogeny: small lamprologines differ predatory lamprologines and V. moorii

#phylogenetic signal in age at first reproduction----
afr.evo.formula<-AgeFirstReproduction~A/B/C
afr.evo.corr<-correlogram.formula(afr.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(afr.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.5, 0.4))
#-> there is no evidence that phylogenetic relationships influence age at first reproduction

#phylogenetic signal in time from laying to hatching----
lth.evo.formula<-LayingToHatching~A/B/C
lth.evo.corr<-correlogram.formula(lth.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(lth.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> Eretmodini differ from Lamprologini in the time from egg laying to hatching of fry

#phylogenetic signal in time from hatching to swimming----
hts.evo.formula<-HatchingToSwimming~A/B/C
hts.evo.corr<-correlogram.formula(hts.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(hts.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.4, 1))
#-> small Lamprologini differ from Eretmodini and predatory Lamprologini

#phylogenetic signal in growth rate----
growth.evo.formula<-GrowthRate~A/B/C
growth.evo.corr<-correlogram.formula(growth.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(growth.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.5, 0.25))
#-> there is no evidence that phylogenetic relationships influence growth rates

#phylogenetic signal in female size----
fs.evo.formula<-AdultFemaleMaxSL~A/B/C
fs.evo.corr<-correlogram.formula(fs.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(fs.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.5, 0.25))
#-> there is no evidence that phylogenetic relationships influence female maximum size distributions

#phylogenetic signal in male size----
ms.evo.formula<-AdultMaleMaxSL~A/B/C
ms.evo.corr<-correlogram.formula(ms.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(ms.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.6, 0.8))
#-> predatory Lamprologini differ from Eretmodini and small Lamprologini

#phylogenetic signal in diet: predation----
pred.evo.formula<-Pred~A/B/C
pred.evo.corr<-correlogram.formula(pred.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(pred.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> predation is different between all groupings

#phylogenetic signal in diet: aufwuchs----
auf.evo.formula<-Aufwuchs~A/B/C
auf.evo.corr<-correlogram.formula(auf.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(auf.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> there is no evidence that phylogenetic relationships influence distributions of aufwuchs feeding

#phylogenetic signal in sociality: pair forming----
pair.evo.formula<-Pair~A/B/C
pair.evo.corr<-correlogram.formula(pair.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(pair.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> pair forming is different between all groupings (unsurprising, same distribution)

#phylogenetic signal in sociality: cooperation----
coop.evo.formula<-Coop~A/B/C
coop.evo.corr<-correlogram.formula(coop.evo.formula, data=bys)
par(mar=c(5, 5, 1, 1))
plot(coop.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5)
#-> there is no evidence that phylogenetic relationships influence distributions of cooperative breeding

#summary plot of phylogenetic signal analyses in evo-devo traits - Figure S1----
par(mfrow=c(4, 2), mar=c(5, 5, 1, 1))
plot(fs.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.5, 0.75))
legend("topleft", c("a: female SL"))
plot(ms.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.75, 2.75))
legend("topleft", c("b: male SL"))
plot(lth.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(0.4, 1.2))
legend("topleft", c("c: LTH"))
plot(hts.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.6, 2.75))
legend("topleft", c("d: HTS"))
plot(growth.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.75, 1.75))
legend("topleft", c("e: growth"))
plot(afr.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.75, 2))
legend("topleft", c("f: AFR"))
plot(pred.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5,  ylim=c(-1.5, 5))
legend("topleft", c("g: predatory"))
plot(coop.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5,  ylim=c(-0.45, 0.3))
legend("bottomleft", c("h: cooperative"))
par(mfrow=c(1, 1), mar=c(5, 5, 1, 1))

#summary plot of phylogenetic signal analyses in egg traits - Figure S2----
par(mfrow=c(3, 2), mar=c(5, 5, 1, 1))
plot(em.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(0.1, 1))
legend("topleft", c("a: egg mass"))
plot(ici.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.25, 0.7))
legend("bottomleft", c("b: ICI"))
plot(cs.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.75, -0.05))
legend("bottomleft", c("c: clutch size"))
plot(wc.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.65, 0.15))
legend("topleft", c("d: water content"))
plot(fc.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.6, 0.65))
legend("bottomleft", c("e: fat content"))
plot(pc.evo.corr, xlab="Phylogenetic contrast", cex.lab=1.5, ylim=c(-0.45, 0.6))
legend("bottomleft", c("f: protein content"))
par(mfrow=c(1, 1), mar=c(5, 5, 1, 1))

#egg characteristics----
#water content
par(mfrow=c(2, 2), mar=c(5, 5, 1, 1))
plot(Water~AvgEggMass, data=bys, col=0)
points(Water~AvgEggMass, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(Water~AvgEggMass, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(Water~AvgEggMass, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(Water~AvgEggMass, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(Water~AvgEggMass, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(Water~AvgEggMass, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(Water~AvgEggMass, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#fat content
plot(DryFat~AvgEggMass, data=bys, col=0)
points(DryFat~AvgEggMass, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(DryFat~AvgEggMass, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(DryFat~AvgEggMass, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(DryFat~AvgEggMass, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(DryFat~AvgEggMass, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(DryFat~AvgEggMass, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(DryFat~AvgEggMass, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#protein content
plot(DryProtein~AvgEggMass, data=bys, col=0)
points(DryProtein~AvgEggMass, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(DryProtein~AvgEggMass, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(DryProtein~AvgEggMass, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(DryProtein~AvgEggMass, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(DryProtein~AvgEggMass, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(DryProtein~AvgEggMass, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(DryProtein~AvgEggMass, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#protein content
plot(DryOther~AvgEggMass, data=bys, col=0)
points(DryOther~AvgEggMass, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(DryOther~AvgEggMass, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(DryOther~AvgEggMass, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(DryOther~AvgEggMass, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(DryOther~AvgEggMass, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(DryOther~AvgEggMass, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(DryOther~AvgEggMass, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#developmental time
plot(HatchingToSwimming~AvgEggMass, data=bys, col=0)
points(HatchingToSwimming~AvgEggMass, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(HatchingToSwimming~AvgEggMass, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(HatchingToSwimming~AvgEggMass, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(HatchingToSwimming~AvgEggMass, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(HatchingToSwimming~AvgEggMass, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(HatchingToSwimming~AvgEggMass, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(HatchingToSwimming~AvgEggMass, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

###species-level reproductive trait relationships----
###relative reproductive investment----
#length
par(mfrow=c(3, 2), mar=c(5, 5, 1, 1))
plot(AdultFemaleSize~AdultFemaleWeight, data=bys, col=0)
points(AdultFemaleSize~AdultFemaleWeight, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(AdultFemaleSize~AdultFemaleWeight, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(AdultFemaleSize~AdultFemaleWeight, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(AdultFemaleSize~AdultFemaleWeight, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(AdultFemaleSize~AdultFemaleWeight, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(AdultFemaleSize~AdultFemaleWeight, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(AdultFemaleSize~AdultFemaleWeight, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#egg mass
plot(AvgEggMass~AdultFemaleWeight, data=bys, col=0)
points(AvgEggMass~AdultFemaleWeight, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(AvgEggMass~AdultFemaleWeight, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(AvgEggMass~AdultFemaleWeight, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(AvgEggMass~AdultFemaleWeight, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(AvgEggMass~AdultFemaleWeight, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(AvgEggMass~AdultFemaleWeight, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(AvgEggMass~AdultFemaleWeight, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#clutch mass
plot(AvgClutchWeight~AdultFemaleWeight, data=bys, col=0)
points(AvgClutchWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(AvgClutchWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(AvgClutchWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(AvgClutchWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(AvgClutchWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(AvgClutchWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(AvgClutchWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#relative clutch mass
plot(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=bys, col=0)
points(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(PropAvgClutchOfFemaleWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#clutches to own body weight
plot(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=bys, col=0)
points(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(NumClutchesToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

#time to own body weight
plot(TimeToOwnBodyWeight~AdultFemaleWeight, data=bys, col=0)
points(TimeToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="AltCom"), col="burlywood", pch=16)
points(TimeToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="EreCya"), col="cyan3", pch=16)
points(TimeToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(TimeToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="LepElo"), col="azure4", pch=16)
points(TimeToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="NeoPul"), col="chocolate1", pch=16)
points(TimeToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="TelTem"), col="darkorchid4", pch=16)
points(TimeToOwnBodyWeight~AdultFemaleWeight, data=subset(bys, bys$Species=="VarMoo"), col=1, pch=16)

###clutch size and inter-clutch interval###----
#do the species differ in clutch size? model cs.m1----

#explorations of clutch size distributions
par(mar=c(5, 5, 1, 1), mfrow=c(3, 2))
hist(byir$ClutchSize)
hist(byir$ZCS)
hist(log(byir$ClutchSize))
hist(log(byir$ZCS))
hist(byir$boxcoxcs)
hist(log(byir$boxcoxcs))
par(mar=c(5, 5, 5, 1), mfrow=c(3, 2))

#explorations of clutch size as a function of species identity model fit
#naive lm
cs.m1.lm<-lmer(ClutchSize~Species+(1|ID), data=byir)
testResiduals(cs.m1.lm)
#-> bad model fit

#z transformed lm
cs.m1.z<-lmer(ZCS~Species+(1|ID), data=byir)
testResiduals(cs.m1.z)
#-> bad model (of course)

#log transformed lm
cs.m1.log<-lmer(log(ClutchSize)~Species+(1|ID), data=byir)
testResiduals(cs.m1.log)
#-> slightly better model fit, but still bad

#boxcox transformed lm
cs.m1.bc<-lmer(boxcoxcs~Species+(1|ID), data=byir)
testResiduals(cs.m1.bc)
#-> similar to log transformed model, but slightly better

#poisson glm
cs.m1.po<-glmer(ClutchSize~Species+(1|ID), data=byir, family="poisson")
testResiduals(cs.m1.po)
#-> similar to boxcox transformed model, but slightly better

#negative binomial glm
cs.m1.nb<-glmer.nb(ClutchSize~Species+(1|ID), data=byir)
testResiduals(cs.m1.nb)
#-> similar to poisson, but chosen because no outliers and almost okay dispersion-wise

#clutch size model cs.m1, M1
cs.m1<-glmer.nb(ClutchSize~Species+(1|ID), data=byir)
summary(cs.m1)
drop1(cs.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
qqnorm(resid(cs.m1))
qqline(resid(cs.m1), col=2)
plot(resid(cs.m1)~fitted(cs.m1))
res_ind.m1=simulateResiduals(cs.m1)
testResiduals(cs.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))
###
#-> clutch size differs between the species! 
###

#do the species differ in inter-clutch interval? model ici.m1----
#explorations of inter-clutch interval as a function of species identity model fit

#explorations of clutch size distributions
par(mar=c(5, 5, 1, 1), mfrow=c(2, 1))
hist(byir$ICI)
hist(log(byir$ICI))
par(mar=c(5, 5, 5, 1), mfrow=c(1, 1))

#naive lm
ici.m1.lm<-lmer(ICI~Species+(1|ID), data=byir)
testResiduals(ici.m1.lm)
#-> bad model fit

#log transformed lm
ici.m1.log<-lmer(log(ICI)~Species+(1|ID), data=subset(byir, byir$ICI>0))
testResiduals(ici.m1.log)
#-> slightly better model fit, but still not great. At least okay dispersion-wise

#poisson glm
ici.m1.po<-glmer(ICI~Species+(1|ID), data=byir, family="poisson")
testResiduals(ici.m1.po)
#-> worse than log model

#negative binomial glm
ici.m1.nb<-glmer.nb(ICI~Species+(1|ID), data=byir)
testResiduals(ici.m1.po)
#-> also very bad fit

#inter-clutch interval model ici.m1, M2
ici.m1<-lmer(log(ICI)~Species+(1|ID), data=subset(byir, byir$ICI>0))
summary(ici.m1)
drop1(ici.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
qqnorm(resid(ici.m1))
qqline(resid(ici.m1), col=2)
plot(resid(ici.m1)~fitted(ici.m1))
res_ind.m1=simulateResiduals(ici.m1)
testResiduals(ici.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))
###
#-> inter-clutch interval differs between the species! 
###

#does ICI affect clutch size? model egg.m1----
#explorations for clutch size versus ICI model

#naive lm
egg.m1.lm<-lmer(ClutchSize~ICI*Species+(1|ID), data=byir)
testResiduals(egg.m1.lm)
#-> bad model fit

#log transformed lm
egg.m1.log<-lmer(log(ClutchSize)~ICI*Species+(1|ID), data=byir)
testResiduals(egg.m1.log)
#-> slightly better model fit, but still bad

#boxcox transformed lm
egg.m1.bc<-lmer(boxcoxcs~ICI*Species+(1|ID), data=byir)
testResiduals(egg.m1.bc)
#-> slightly better than log transformed model and okay dispersion-wise

#poisson glm
egg.m1.po<-glmer(ClutchSize~ICI*Species+(1|ID), data=byir, family="poisson")
testResiduals(egg.m1.po)
#-> bad model fit and also no good convergence

#clutch size as function of ICI model: egg.m1, M3
egg.m1<-lmer(boxcoxcs~ICI*Species+(1|ID), data=byir)
summary(egg.m1)
drop1(egg.m1, test="Chisq")
egg.m1<-lmer(boxcoxcs~ICI+Species+(1|ID), data=byir)
summary(egg.m1)
drop1(egg.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
qqnorm(resid(egg.m1))
qqline(resid(egg.m1), col=2)
plot(resid(egg.m1)~fitted(egg.m1))
res_ind.m1=simulateResiduals(egg.m1)
testResiduals(egg.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))
###
#-> inter-clutch interval generally increases clutch size, when correcting for species identity! 
###

#plot clutch size and inter-clutch interval related plots: Figure 2----
#layout(matrix(c(2, 1, 4, 3), nrow=2, ncol=2), heights=c(1, 2), widths=c(2, 1))

layout(matrix(c(1, 2, 2, 
                3, 2, 2,
                4, 5, 6), nrow=3, ncol=3), heights=c(1, 2, 0.4), widths=c(0.25,2, 1))

par(mar=c(0.75, 0, 2.75, 0))
plot(lamptree, show.tip.label=FALSE)

par(mar=c(5, 5, 1, 1))
plot(ClutchSize~ICI, data=byir, col=0, xlab="Inter-clutch interval [days]", ylab="Clutch size", cex.lab=1.5)

#points(x=70, y=87, pch=13, cex=5, col="burlywood", lwd=5)
#points(x=21, y=43, pch=13, cex=5, col="darkgoldenrod1", lwd=5)
#points(x=45, y=460, pch=13, cex=5, col="azure4", lwd=5)
#points(x=26, y=93, pch=13, cex=5, col="chocolate1", lwd=5)
#points(x=18, y=48, pch=13, cex=5, col="darkorchid4", lwd=5)
#points(x=40, y=384, pch=13, cex=5, lwd=5)

csbyici.all<-lm(ClutchSize~ICI, data=byir)
csbyici.all.x<-expand.grid(ICI=seq(from=min(byir$ICI), to=max(byir$ICI), length=1000))
csbyici.all.y<-predict.lm(csbyici.all, newdata=csbyici.all.x, type="response")
csbyici.all.data<-data.frame(csbyici.all.x, csbyici.all.y)
#lines(csbyici.all.y~ICI, data=csbyici.all.data, col="red", lwd=4)

points(ClutchSize~ICI, data=subset(byir, byir$ID=="AltCom_F_001"), col="burlywood", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="AltCom_F_002"), col="burlywood", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="AltCom_F_003"), col="burlywood", pch=16)

points(ClutchSize~ICI, data=subset(byir, byir$ID=="JulOrn_F_001"), col="darkgoldenrod1", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="JulOrn_F_002"), col="darkgoldenrod1", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="JulOrn_F_003"), col="darkgoldenrod1", pch=16)

points(ClutchSize~ICI, data=subset(byir, byir$ID=="LepElo_F_001"), col="azure4", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="LepElo_F_002"), col="azure4", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="LepElo_F_003"), col="azure4", pch=16)

points(ClutchSize~ICI, data=subset(byir, byir$ID=="NeoPul_F_001"), col="chocolate1", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="NeoPul_F_002"), col="chocolate1", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="NeoPul_F_003"), col="chocolate1", pch=16)

points(ClutchSize~ICI, data=subset(byir, byir$ID=="TelTem_F_001"), col="darkorchid4", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="TelTem_F_002"), col="darkorchid4", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="TelTem_F_003"), col="darkorchid4", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="TelTem_F_004"), col="darkorchid4", pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="TelTem_F_005"), col="darkorchid4", pch=16)

points(ClutchSize~ICI, data=subset(byir, byir$ID=="VarMoo_F_001"), pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="VarMoo_F_002"), pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="VarMoo_F_003"), pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="VarMoo_F_004"), pch=16)
points(ClutchSize~ICI, data=subset(byir, byir$ID=="VarMoo_F_005"), pch=16)

csbyici.ac<-lm(ClutchSize~ICI, data=subset(byir, byir$Species=="AltCom"))
csbyici.ac.x<-expand.grid(ICI=seq(from=min(subset(byir$ICI, byir$Species=="AltCom")), to=max(subset(byir$ICI, byir$Species=="AltCom")), length=1000))
csbyici.ac.y<-predict.lm(csbyici.ac, newdata=csbyici.ac.x, type="response")
csbyici.ac.data<-data.frame(csbyici.ac.x, csbyici.ac.y)
lines(csbyici.ac.y~ICI, data=csbyici.ac.data, col="burlywood", lwd=2, lty=2)

csbyici.jo<-lm(ClutchSize~ICI, data=subset(byir, byir$Species=="JulOrn"))
csbyici.jo.x<-expand.grid(ICI=seq(from=min(subset(byir$ICI, byir$Species=="JulOrn")), to=max(subset(byir$ICI, byir$Species=="JulOrn")), length=1000))
csbyici.jo.y<-predict.lm(csbyici.jo, newdata=csbyici.jo.x, type="response")
csbyici.jo.data<-data.frame(csbyici.jo.x, csbyici.jo.y)
lines(csbyici.jo.y~ICI, data=csbyici.jo.data, col="darkgoldenrod1", lwd=2, lty=2)

csbyici.le<-lm(ClutchSize~ICI, data=subset(byir, byir$Species=="LepElo"))
csbyici.le.x<-expand.grid(ICI=seq(from=min(subset(byir$ICI, byir$Species=="LepElo")), to=max(subset(byir$ICI, byir$Species=="LepElo")), length=1000))
csbyici.le.y<-predict.lm(csbyici.le, newdata=csbyici.le.x, type="response")
csbyici.le.data<-data.frame(csbyici.le.x, csbyici.le.y)
lines(csbyici.le.y~ICI, data=csbyici.le.data, col="azure4", lwd=2, lty=2)

csbyici.np<-lm(ClutchSize~ICI, data=subset(byir, byir$Species=="NeoPul"))
csbyici.np.x<-expand.grid(ICI=seq(from=min(subset(byir$ICI, byir$Species=="NeoPul")), to=max(subset(byir$ICI, byir$Species=="NeoPul")), length=1000))
csbyici.np.y<-predict.lm(csbyici.np, newdata=csbyici.np.x, type="response")
csbyici.np.data<-data.frame(csbyici.np.x, csbyici.np.y)
lines(csbyici.np.y~ICI, data=csbyici.np.data, col="chocolate1", lwd=2, lty=2)

csbyici.tt<-lm(ClutchSize~ICI, data=subset(byir, byir$Species=="TelTem"))
csbyici.tt.x<-expand.grid(ICI=seq(from=min(subset(byir$ICI, byir$Species=="TelTem")), to=max(subset(byir$ICI, byir$Species=="TelTem")), length=1000))
csbyici.tt.y<-predict.lm(csbyici.tt, newdata=csbyici.tt.x, type="response")
csbyici.tt.data<-data.frame(csbyici.tt.x, csbyici.tt.y)
lines(csbyici.tt.y~ICI, data=csbyici.tt.data, col="darkorchid4", lwd=2, lty=2)

csbyici.vm<-lm(ClutchSize~ICI, data=subset(byir, byir$Species=="VarMoo"))
csbyici.vm.x<-expand.grid(ICI=seq(from=min(subset(byir$ICI, byir$Species=="VarMoo")), to=max(subset(byir$ICI, byir$Species=="VarMoo")), length=1000))
csbyici.vm.y<-predict.lm(csbyici.vm, newdata=csbyici.vm.x, type="response")
csbyici.vm.data<-data.frame(csbyici.vm.x, csbyici.vm.y)
lines(csbyici.vm.y~ICI, data=csbyici.vm.data, col=1, lwd=2, lty=2)

legend("topright", c("A"), bty="n", cex=1.5)

par(mar=c(0, 0, 2.5, 1))
boxplot(ICI~Species, data=byir, xaxt="n", yaxt="n", outline=T, ylab="",
        col=c(0, 0, 0, 0, 0, 0),
        outcol=c(0, 0, 0, 0, 0),
        border=c(0, 0, 0, 0, 0, 0),
        medcol=c(0, 0, 0, 0, 0, 0), horizontal=T, outpch=c(16, 16, 16, 16, 16, 16))
axis(side=3, at=c(0, 20, 40, 60, 80, 100), c("0", "20", "40", "60", "80", "100"))

boxplot(ICI~Species, data=subset(byir, byir$Species=="AltCom"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=1, col="burlywood", horizontal=T, add=T,
        outpch=c(16), outcol="burlywood", medcol=1, boxwex=1.5)
boxplot(ICI~Species, data=subset(byir, byir$Species=="LepElo"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=2, col="azure4", horizontal=T, add=T,
        outpch=c(16), outcol="azure4", medcol=1, boxwex=1.5)
boxplot(ICI~Species, data=subset(byir, byir$Species=="VarMoo"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=3, col=1, horizontal=T, add=T,
        outpch=c(16), outcol=1, medcol=0, boxwex=1.5)
boxplot(ICI~Species, data=subset(byir, byir$Species=="JulOrn"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=4, col="darkgoldenrod1", horizontal=T, add=T,
        outpch=c(16), outcol="darkgoldenrod1", medcol=1, boxwex=1.5)
boxplot(ICI~Species, data=subset(byir, byir$Species=="NeoPul"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=5, col="chocolate1", horizontal=T, add=T,
        outpch=c(16), outcol="chocolate1", medcol=1, boxwex=1.5)
boxplot(ICI~Species, data=subset(byir, byir$Species=="TelTem"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=6, col="darkorchid4", horizontal=T, add=T,
        outpch=c(16), outcol="darkorchid4", medcol=1, boxwex=1.5)

legend("topright", c("B"), bty="n", cex=1.5)

par(mar=c(0, 0, 1, 1))
plot(ClutchSize~ICI, data=byir, col=0, xaxt="n", yaxt="n", axes=0)
legend("center", c(expression(italic("Altolamprologus compressiceps")), 
                   expression(italic("Julidochromis ornatus")), 
                   expression(italic("Lepidiolamprologus elongatus")), 
                   expression(italic("Neolamprologus pulcher")), 
                   expression(italic("Telmatochromis temporalis")), 
                   expression(italic("Variabilichromis moorii"))),
       cex=1.1,
       pt.bg=c("burlywood", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", "black"),
       bty="n",
       pch=c(16),
       pt.cex=1.5,
       lty=c(1),
       lwd=c(2),
       text.col=c(1),
       pt.lwd=c(1),
       col=c("burlywood", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", "black"))

par(mar=c(0.4, 0, 1, 2.5))
boxplot(ClutchSize~Species, data=byir, xaxt="n", yaxt="n", outline=T, ylab="", xlab="",
        col=c(0, 0, 0, 0, 0, 0),
        outcol=c(0, 0, 0, 0, 0),
        border=c(0, 0, 0, 0, 0, 0),
        medcol=c(0, 0, 0, 0, 0, 0), horizontal=F, outpch=c(16, 16, 16, 16, 16, 16))
axis(side=4, at=c(0, 200, 400, 600, 800), c("0", "200", "400", "600", "800"))

boxplot(ClutchSize~Species, data=subset(byir, byir$Species=="AltCom"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=1, col="burlywood", horizontal=F, add=T,
        outpch=c(16), outcol="burlywood", medcol=1, boxwex=1.5)
boxplot(ClutchSize~Species, data=subset(byir, byir$Species=="LepElo"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=2, col="azure4", horizontal=F, add=T,
        outpch=c(16), outcol="azure4", medcol=1, boxwex=1.5)
boxplot(ClutchSize~Species, data=subset(byir, byir$Species=="VarMoo"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=3, col=1, horizontal=F, add=T,
        outpch=c(16), outcol=1, medcol=0, boxwex=1.5)
boxplot(ClutchSize~Species, data=subset(byir, byir$Species=="JulOrn"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=4, col="darkgoldenrod1", horizontal=F, add=T,
        outpch=c(16), outcol="darkgoldenrod1", medcol=1, boxwex=1.5)
boxplot(ClutchSize~Species, data=subset(byir, byir$Species=="NeoPul"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=5, col="chocolate1", horizontal=F, add=T,
        outpch=c(16), outcol="chocolate1", medcol=1, boxwex=1.5)
boxplot(ClutchSize~Species, data=subset(byir, byir$Species=="TelTem"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=6, col="darkorchid4", horizontal=F, add=T,
        outpch=c(16), outcol="darkorchid4", medcol=1, boxwex=1.5)

legend("topright", c("C"), bty="n", cex=1.5)

par(mar=c(0, 1.5, 0, 4))
plot(lamptree, show.tip.label=FALSE, direction="u")
par(mar=c(5, 5, 1, 1))


###growth###-----
#do the species differ in early growth? model early.m1----
#naive lm
earlygrowth.m1.lm<-lm(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5))
testResiduals(earlygrowth.m1.lm)
#-> okayish fit

#log transformed lm
earlygrowth.m1.log<-lm(log(Length)~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5))
testResiduals(earlygrowth.m1.log)
#-> excellent fit

#species differences in early growth model: early.m1, M4
early.m1<-lmer(log(Length)~Species+(1|IDClutch), data=subset(growthnnt, growthnnt$AgeClass==0.5))
summary(early.m1)
drop1(early.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
qqnorm(resid(early.m1))
qqline(resid(early.m1), col=2)
plot(resid(early.m1)~fitted(early.m1))
res_ind.m1=simulateResiduals(early.m1)
testResiduals(early.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))
###
#-> the species clearly differ in their size at age 0.5
###

#do the species differ in growth trajectories? model growth.m1----
#explorations of growth model

#naive lm
growth.m1.lm<-lm(Length~Age*Species, data=growthnnt)
testResiduals(growth.m1.lm)
#-> not so great model fit

#log transformed lm
growth.m1.log<-lm(log(Length)~Age*Species, data=growthnnt)
testResiduals(growth.m1.log)
#-> excellent fit!

#growth model growth.m1, M5
growth.m1<-lmer(log(Length)~Age*Species+(1|IDClutch), data=growthnnt)
summary(growth.m1)
drop1(growth.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
testResiduals(growth.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))
###
#-> the growth trajectory differs between the species
###

#do the species differ in their density-dependence for growth? model dens.m1----
hist(growthnnt$ResSize)

#naive lm
dens.m1.lm<-lm(ResSize~Density*Species, data=growthnnt)
testResiduals(dens.m1.lm)
#decent fit

#density dependent residual size model dens.m1, M6
dens.m1<-lmer(ResSize~Density*Species+(1|IDClutch), data=growthnnt)
summary(dens.m1)
drop1(dens.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
testResiduals(dens.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))

#plot body size related graphs: Figure 3----
layout(matrix(c(1, 1, 2, 3,
                4, 4, 5, 5), nrow=4, ncol=2), heights=c(1, 1), widths=c(2, 4))

par(mar=c(0, 5, 1, 1))
boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5), xaxt="n", outline=T, ylab="Length [cm SL]", xlab="",
        col=c(0, 0, 0, 0, 0, 0, 0),
        outcol=c(0, 0, 0, 0, 0, 0, 0),
        border=c(0, 0, 0, 0, 0, 0, 0),
        medcol=c(0, 0, 0, 0, 0, 0, 0), horizontal=F, outpch=c(16, 16, 16, 16, 16, 16, 16), cex.lab=1.5)

boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5 & growthnnt$Species=="AltCom"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=1, col="burlywood", horizontal=F, add=T,
        outpch=c(16), outcol="burlywood", medcol=1, boxwex=1.5)
boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5 & growthnnt$Species=="LepElo"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=2, col="azure4", horizontal=F, add=T,
        outpch=c(16), outcol="azure4", medcol=1, boxwex=1.5)
boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5 & growthnnt$Species=="VarMoo"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=3, col=1, horizontal=F, add=T,
        outpch=c(16), outcol=1, medcol=0, boxwex=1.5)
boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5 & growthnnt$Species=="JulOrn"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=4, col="darkgoldenrod1", horizontal=F, add=T,
        outpch=c(16), outcol="darkgoldenrod1", medcol=1, boxwex=1.5)
boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5 & growthnnt$Species=="NeoPul"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=5, col="chocolate1", horizontal=F, add=T,
        outpch=c(16), outcol="chocolate1", medcol=1, boxwex=1.5)
boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5 & growthnnt$Species=="TelTem"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=6, col="darkorchid4", horizontal=F, add=T,
        outpch=c(16), outcol="darkorchid4", medcol=1, boxwex=1.5)
boxplot(Length~Species, data=subset(growthnnt, growthnnt$AgeClass==0.5 & growthnnt$Species=="EreCya"), xaxt="n", yaxt="n", outline=T, ylab="",
        at=7, col="cyan3", horizontal=F, add=T,
        outpch=c(16), outcol="cyan3", medcol=1, boxwex=1.5)

legend("topright", c("A"), bty="n", cex=1.5)

par(mar=c(0, 6, 0, 2))
plot(ourtree, show.tip.label=FALSE, direction="u")

par(mar=c(0, 1, 1, 1))
plot(ClutchSize~ICI, data=byir, col=0, xaxt="n", yaxt="n", axes=0)
legend("right", c(expression(italic("Altolamprologus compressiceps")), 
                  expression(italic("Eretmodus cyanostictus")), 
                  expression(italic("Julidochromis ornatus")), 
                  expression(italic("Lepidiolamprologus elongatus")), 
                  expression(italic("Neolamprologus pulcher")), 
                  expression(italic("Telmatochromis temporalis")), 
                  expression(italic("Variabilichromis moorii"))),
       cex=1.1,
       pt.bg=c("burlywood", "cyan3", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", "black"),
       bty="n",
       pch=c(16),
       pt.cex=1.5,
       lty=c(1),
       lwd=c(2),
       text.col=c(1),
       pt.lwd=c(1),
       col=c("burlywood", "cyan3", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", "black"))

par(mar=c(5, 5, 1, 1))
plot(Length~Age, data=growth, col=0, cex.lab=1.5, ylab="Length [cm SL]", xlab="Age [days]")
points(Length~Age, data=subset(growth, growth$Species=="AltCom"), col="burlywood", pch=16)
points(Length~Age, data=subset(growth, growth$Species=="EreCya"), col="cyan3", pch=16)
points(Length~Age, data=subset(growth, growth$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(Length~Age, data=subset(growth, growth$Species=="LepElo"), col="azure4", pch=16)
points(Length~Age, data=subset(growth, growth$Species=="NeoPul"), col="chocolate1", pch=16)
points(Length~Age, data=subset(growth, growth$Species=="TelTem"), col="darkorchid4", pch=16)
points(Length~Age, data=subset(growth, growth$Species=="VarMoo"), pch=16)

lengthbyage.ac<-lm(Length~Age, data=subset(growth, growth$Species=="AltCom"))
lengthbyage.ac.x<-expand.grid(Age=seq(from=min(subset(growth$Age, growth$Species=="AltCom")), to=max(subset(growth$Age, growth$Species=="AltCom")), length=1000))
lengthbyage.ac.y<-predict.lm(lengthbyage.ac, newdata=lengthbyage.ac.x, type="response")
lengthbyage.ac.data<-data.frame(lengthbyage.ac.x, lengthbyage.ac.y)
lines(lengthbyage.ac.y~Age, data=lengthbyage.ac.data, col="burlywood", lwd=2, lty=2)

lengthbyage.ec<-lm(Length~Age, data=subset(growth, growth$Species=="EreCya"))
lengthbyage.ec.x<-expand.grid(Age=seq(from=min(subset(growth$Age, growth$Species=="EreCya")), to=max(subset(growth$Age, growth$Species=="EreCya")), length=1000))
lengthbyage.ec.y<-predict.lm(lengthbyage.ec, newdata=lengthbyage.ec.x, type="response")
lengthbyage.ec.data<-data.frame(lengthbyage.ec.x, lengthbyage.ec.y)
lines(lengthbyage.ec.y~Age, data=lengthbyage.ec.data, col="cyan3", lwd=2, lty=2)

lengthbyage.jo<-lm(Length~Age, data=subset(growth, growth$Species=="JulOrn"))
lengthbyage.jo.x<-expand.grid(Age=seq(from=min(subset(growth$Age, growth$Species=="JulOrn")), to=max(subset(growth$Age, growth$Species=="JulOrn")), length=1000))
lengthbyage.jo.y<-predict.lm(lengthbyage.jo, newdata=lengthbyage.jo.x, type="response")
lengthbyage.jo.data<-data.frame(lengthbyage.jo.x, lengthbyage.jo.y)
lines(lengthbyage.jo.y~Age, data=lengthbyage.jo.data, col="darkgoldenrod1", lwd=2, lty=2)

lengthbyage.le<-lm(Length~Age, data=subset(growth, growth$Species=="LepElo"))
lengthbyage.le.x<-expand.grid(Age=seq(from=min(subset(growth$Age, growth$Species=="LepElo")), to=max(subset(growth$Age, growth$Species=="LepElo")), length=1000))
lengthbyage.le.y<-predict.lm(lengthbyage.le, newdata=lengthbyage.le.x, type="response")
lengthbyage.le.data<-data.frame(lengthbyage.le.x, lengthbyage.le.y)
lines(lengthbyage.le.y~Age, data=lengthbyage.le.data, col="azure4", lwd=2, lty=2)

lengthbyage.np<-lm(Length~Age, data=subset(growth, growth$Species=="NeoPul"))
lengthbyage.np.x<-expand.grid(Age=seq(from=min(subset(growth$Age, growth$Species=="NeoPul")), to=max(subset(growth$Age, growth$Species=="NeoPul")), length=1000))
lengthbyage.np.y<-predict.lm(lengthbyage.np, newdata=lengthbyage.np.x, type="response")
lengthbyage.np.data<-data.frame(lengthbyage.np.x, lengthbyage.np.y)
lines(lengthbyage.np.y~Age, data=lengthbyage.np.data, col="chocolate1", lwd=2, lty=2)

lengthbyage.tt<-lm(Length~Age, data=subset(growth, growth$Species=="TelTem"))
lengthbyage.tt.x<-expand.grid(Age=seq(from=min(subset(growth$Age, growth$Species=="TelTem")), to=max(subset(growth$Age, growth$Species=="TelTem")), length=1000))
lengthbyage.tt.y<-predict.lm(lengthbyage.tt, newdata=lengthbyage.tt.x, type="response")
lengthbyage.tt.data<-data.frame(lengthbyage.tt.x, lengthbyage.tt.y)
lines(lengthbyage.tt.y~Age, data=lengthbyage.tt.data, col="darkorchid4", lwd=2, lty=2)

lengthbyage.vm<-lm(Length~Age, data=subset(growth, growth$Species=="VarMoo"))
lengthbyage.vm.x<-expand.grid(Age=seq(from=min(subset(growth$Age, growth$Species=="VarMoo")), to=max(subset(growth$Age, growth$Species=="VarMoo")), length=1000))
lengthbyage.vm.y<-predict.lm(lengthbyage.vm, newdata=lengthbyage.vm.x, type="response")
lengthbyage.vm.data<-data.frame(lengthbyage.vm.x, lengthbyage.vm.y)
lines(lengthbyage.vm.y~Age, data=lengthbyage.vm.data, col=1, lwd=2, lty=2)

legend("topleft", c("B"), bty="n", cex=1.5)

plot(ResSize~Density, data=subset(growth, growth$Density<0.51), col=0, cex.lab=1.5, ylab="Residual length", xlab="Density")
points(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="AltCom"), col="burlywood", pch=16)
points(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="EreCya"), col="cyan3", pch=16)
points(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
points(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="LepElo"), col="azure4", pch=16)
points(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="NeoPul"), col="chocolate1", pch=16)
points(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="TelTem"), col="darkorchid4", pch=16)
points(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="VarMoo"), pch=16)

ressizebydensity.ac<-lm(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="AltCom"))
ressizebydensity.ac.x<-expand.grid(Density=seq(from=min(subset(growth$Density, growth$Species=="AltCom")), to=max(subset(growth$Density, growth$Density<0.51 & growth$Species=="AltCom")), length=1000))
ressizebydensity.ac.y<-predict.lm(ressizebydensity.ac, newdata=ressizebydensity.ac.x, type="response")
ressizebydensity.ac.data<-data.frame(ressizebydensity.ac.x, ressizebydensity.ac.y)
lines(ressizebydensity.ac.y~Density, data=ressizebydensity.ac.data, col="burlywood", lwd=2, lty=2)

ressizebydensity.ec<-lm(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="EreCya"))
ressizebydensity.ec.x<-expand.grid(Density=seq(from=min(subset(growth$Density, growth$Species=="EreCya")), to=max(subset(growth$Density, growth$Density<0.51 & growth$Species=="EreCya")), length=1000))
ressizebydensity.ec.y<-predict.lm(ressizebydensity.ec, newdata=ressizebydensity.ec.x, type="response")
ressizebydensity.ec.data<-data.frame(ressizebydensity.ec.x, ressizebydensity.ec.y)
lines(ressizebydensity.ec.y~Density, data=ressizebydensity.ec.data, col="cyan3", lwd=2, lty=2)

ressizebydensity.jo<-lm(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="JulOrn"))
ressizebydensity.jo.x<-expand.grid(Density=seq(from=min(subset(growth$Density, growth$Species=="JulOrn")), to=max(subset(growth$Density, growth$Density<0.51 & growth$Species=="JulOrn")), length=1000))
ressizebydensity.jo.y<-predict.lm(ressizebydensity.jo, newdata=ressizebydensity.jo.x, type="response")
ressizebydensity.jo.data<-data.frame(ressizebydensity.jo.x, ressizebydensity.jo.y)
lines(ressizebydensity.jo.y~Density, data=ressizebydensity.jo.data, col="darkgoldenrod1", lwd=2, lty=2)

ressizebydensity.le<-lm(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="LepElo"))
ressizebydensity.le.x<-expand.grid(Density=seq(from=min(subset(growth$Density, growth$Species=="LepElo")), to=max(subset(growth$Density, growth$Density<0.51 & growth$Species=="LepElo")), length=1000))
ressizebydensity.le.y<-predict.lm(ressizebydensity.le, newdata=ressizebydensity.le.x, type="response")
ressizebydensity.le.data<-data.frame(ressizebydensity.le.x, ressizebydensity.le.y)
lines(ressizebydensity.le.y~Density, data=ressizebydensity.le.data, col="azure4", lwd=2, lty=2)

ressizebydensity.np<-lm(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="NeoPul"))
ressizebydensity.np.x<-expand.grid(Density=seq(from=min(subset(growth$Density, growth$Species=="NeoPul")), to=max(subset(growth$Density, growth$Density<0.51 & growth$Species=="NeoPul")), length=1000))
ressizebydensity.np.y<-predict.lm(ressizebydensity.np, newdata=ressizebydensity.np.x, type="response")
ressizebydensity.np.data<-data.frame(ressizebydensity.np.x, ressizebydensity.np.y)
lines(ressizebydensity.np.y~Density, data=ressizebydensity.np.data, col="chocolate1", lwd=2, lty=2)

ressizebydensity.tt<-lm(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="TelTem"))
ressizebydensity.tt.x<-expand.grid(Density=seq(from=min(subset(growth$Density, growth$Species=="TelTem")), to=max(subset(growth$Density, growth$Density<0.51 & growth$Species=="TelTem")), length=1000))
ressizebydensity.tt.y<-predict.lm(ressizebydensity.tt, newdata=ressizebydensity.tt.x, type="response")
ressizebydensity.tt.data<-data.frame(ressizebydensity.tt.x, ressizebydensity.tt.y)
lines(ressizebydensity.tt.y~Density, data=ressizebydensity.tt.data, col="darkorchid4", lwd=2, lty=2)

ressizebydensity.vm<-lm(ResSize~Density, data=subset(growth, growth$Density<0.51 & growth$Species=="VarMoo"))
ressizebydensity.vm.x<-expand.grid(Density=seq(from=min(subset(growth$Density, growth$Species=="VarMoo")), to=max(subset(growth$Density, growth$Density<0.51 & growth$Species=="VarMoo")), length=1000))
ressizebydensity.vm.y<-predict.lm(ressizebydensity.vm, newdata=ressizebydensity.vm.x, type="response")
ressizebydensity.vm.data<-data.frame(ressizebydensity.vm.x, ressizebydensity.vm.y)
lines(ressizebydensity.vm.y~Density, data=ressizebydensity.vm.data, lwd=2, lty=2)

legend("topleft", c("C"), bty="n", cex=1.5)

###survival###----
#explorations of survival rate distributions
par(mar=c(5, 5, 1, 1), mfrow=c(2, 2))
hist(as.numeric(subset(surv$PropSurv, surv$AgeClass==0.5 & !is.na(surv$ClutchSize))))
hist(as.numeric(subset(surv$PropSurv, surv$AgeClass==1 & !is.na(surv$ClutchSize))))
hist(as.numeric(subset(surv$PropSurv, surv$AgeClass==1.5 & !is.na(surv$ClutchSize))))
hist(as.numeric(subset(surv$PropSurv, surv$AgeClass==2 & !is.na(surv$ClutchSize))))
par(mar=c(5, 5, 5, 1), mfrow=c(1, 1))

#sample sizes for survival analyses
length(unique(subset(surv$IDClutch, !is.na(surv$ClutchSize))))
length(unique(subset(surv$IDClutch, surv$Species=="AltCom" & !is.na(surv$ClutchSize))))
length(unique(subset(surv$IDClutch, surv$Species=="EreCya" & !is.na(surv$ClutchSize))))
length(unique(subset(surv$IDClutch, surv$Species=="JulOrn" & !is.na(surv$ClutchSize))))
length(unique(subset(surv$IDClutch, surv$Species=="LepElo" & !is.na(surv$ClutchSize))))
length(unique(subset(surv$IDClutch, surv$Species=="NeoPul" & !is.na(surv$ClutchSize))))
length(unique(subset(surv$IDClutch, surv$Species=="TelTem" & !is.na(surv$ClutchSize))))
length(unique(subset(surv$IDClutch, surv$Species=="VarMoo" & !is.na(surv$ClutchSize))))

#do the species differ in how group size changes with age? model gs.m1----
#exploring group size models
#naive lm
gs.m1.lm<-lm(GroupSize~log(Age+1)*Species, data=subset(surv, !is.na(surv$ClutchSize)))
testResiduals(gs.m1.lm)
#terrible model fit

#log transformed lm
gs.m1.log<-lm(log(GroupSize)~log(Age+1)*Species, data=subset(surv, !is.na(surv$ClutchSize)))
testResiduals(gs.m1.log)
#much improved model fit

#poisson glm
gs.m1.pglm<-glm(GroupSize~log(Age+1)*Species, data=subset(surv, !is.na(surv$ClutchSize)), family="poisson")
testResiduals(gs.m1.pglm)
#terrible model fit

#negative binomial glm
gs.m1.nb<-glm.nb(GroupSize~log(Age+1)*Species, data=subset(surv, !is.na(surv$ClutchSize)))
testResiduals(gs.m1.nb)
#okay model fit, but overdispersed, whereas log transformed model was not

#group size as a function of age and species identity model: gs.m1, M7
gs.m1<-lmer(log(GroupSize)~log(Age+1)*Species+(1|IDClutch), data=subset(surv, !is.na(surv$ClutchSize)))
summary(gs.m1)
drop1(gs.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
qqnorm(resid(gs.m1))
qqline(resid(gs.m1), col=2)
plot(resid(gs.m1)~fitted(gs.m1))
res_ind.m1=simulateResiduals(gs.m1)
testResiduals(gs.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))

#do the species differ in relative survival rates with age? model surv.m1----
#explorations of model fit
#naive lm
surv.m1.lm<-lm(NumPropSurv~log(Age+1)*Species, data=subset(surv, !is.na(surv$ClutchSize)))
testResiduals(surv.m1.lm)
#excellent model model fit

#survival as a function of age and species identity model: surv.m1, M8
surv.m1<-lmer(NumPropSurv~log(Age+1)*Species+(1|IDClutch), data=subset(surv, !is.na(surv$ClutchSize)))
summary(surv.m1)
drop1(surv.m1, test="Chisq")
par(mfrow=c(1,1), mar=c(5, 5, 3, 3))
qqnorm(resid(surv.m1))
qqline(resid(surv.m1), col=2)
plot(resid(surv.m1)~fitted(surv.m1))
res_ind.m1=simulateResiduals(surv.m1)
testResiduals(surv.m1)
par(mfrow=c(1,1) , mar=c(5, 5, 1, 1))
###
#-> survival generally decreases, but the species differ in this effect with some having worse survival probabilities than others
###

#plot survival related graphs: Figure 4----
par(mar=c(4, 5, 1, 1), mfrow=c(2, 1))
plot(GroupSize~Age, data=surv, col=0, ylab="Number of individuals", xlab="", cex.lab=1.25)

points(GroupSize~Age, data=subset(surv, surv$Species=="AltCom"), col="burlywood", pch=16)
#survbyage.ac<-lm(GroupSize~poly(Age, 2, raw=TRUE), data=subset(surv, surv$Species=="AltCom"))
survbyage.ac<-lm(GroupSize~log(Age+1), data=subset(surv, surv$Species=="AltCom"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="AltCom")), to=max(subset(surv$Age, surv$Species=="AltCom")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="burlywood", lwd=2, lty=2)

points(GroupSize~Age, data=subset(surv, surv$Species=="LepElo"), col="azure4", pch=16)
survbyage.ac<-lm(GroupSize~log(Age+1), data=subset(surv, surv$Species=="LepElo"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="LepElo")), to=max(subset(surv$Age, surv$Species=="LepElo")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="azure4", lwd=2, lty=2)

points(GroupSize~Age, data=subset(surv, surv$Species=="VarMoo"), col=1, pch=16)
survbyage.ac<-lm(GroupSize~log(Age+1), data=subset(surv, surv$Species=="VarMoo"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="VarMoo")), to=max(subset(surv$Age, surv$Species=="VarMoo")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col=1, lwd=2, lty=2)

points(GroupSize~Age, data=subset(surv, surv$Species=="EreCya"), col="cyan3", pch=16)
survbyage.ac<-lm(GroupSize~log(Age+1), data=subset(surv, surv$Species=="EreCya"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="EreCya")), to=max(subset(surv$Age, surv$Species=="EreCya")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="cyan3", lwd=2, lty=2)

points(GroupSize~Age, data=subset(surv, surv$Species=="JulOrn"), col="darkgoldenrod1", pch=16)
survbyage.ac<-lm(GroupSize~log(Age+1), data=subset(surv, surv$Species=="JulOrn"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="JulOrn")), to=max(subset(surv$Age, surv$Species=="JulOrn")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="darkgoldenrod1", lwd=2, lty=2)

points(GroupSize~Age, data=subset(surv, surv$Species=="NeoPul"), col="chocolate1", pch=16)
survbyage.ac<-lm(GroupSize~log(Age+1), data=subset(surv, surv$Species=="NeoPul"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="NeoPul")), to=max(subset(surv$Age, surv$Species=="NeoPul")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="chocolate1", lwd=2, lty=2)

points(GroupSize~Age, data=subset(surv, surv$Species=="TelTem"), col="darkorchid4", pch=16)
survbyage.ac<-lm(GroupSize~log(Age+1), data=subset(surv, surv$Species=="TelTem"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="TelTem")), to=max(subset(surv$Age, surv$Species=="TelTem")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="darkorchid4", lwd=2, lty=2)



legend("topright", c(expression(italic("Altolamprologus compressiceps")), 
                     expression(italic("Eretmodus cyanostictus")), 
                     expression(italic("Julidochromis ornatus")), 
                     expression(italic("Lepidiolamprologus elongatus")), 
                     expression(italic("Neolamprologus pulcher")), 
                     expression(italic("Telmatochromis temporalis")), 
                     expression(italic("Variabilichromis moorii"))),
       cex=0.75,
       pt.bg=c("burlywood", "cyan3", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", "black"),
       bty="n",
       pch=c(16),
       pt.cex=0.75,
       lty=c(1),
       lwd=c(2),
       text.col=c(1),
       pt.lwd=c(1),
       col=c("burlywood", "cyan3", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", "black"))

legend(x=-25, y=900, c("A"), bty="n", cex=1.5)

par(mar=c(5, 5, 0, 1))
plot(PropSurv~Age, data=subset(surv, !is.na(surv$ClutchSize)), col=0, ylab="Proportion alive", xlab="Age [days]", cex.lab=1.25)

points(PropSurv~Age, data=subset(surv, surv$Species=="AltCom" & !is.na(surv$ClutchSize)), col="burlywood", pch=16)
#survbyage.ac<-lm(PropSurv~poly(Age, 2, raw=TRUE), data=subset(surv, surv$Species=="AltCom"))
survbyage.ac<-lm(PropSurv~log(Age+1), data=subset(surv, surv$Species=="AltCom" & !is.na(surv$ClutchSize)))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="AltCom")), to=max(subset(surv$Age, surv$Species=="AltCom")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="burlywood", lwd=2, lty=2)

points(PropSurv~Age, data=subset(surv, surv$Species=="LepElo"), col="azure4", pch=16)
survbyage.ac<-lm(PropSurv~log(Age+1), data=subset(surv, surv$Species=="LepElo"))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="LepElo")), to=max(subset(surv$Age, surv$Species=="LepElo")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="azure4", lwd=2, lty=2)

points(PropSurv~Age, data=subset(surv, surv$Species=="EreCya" & !is.na(surv$ClutchSize)), col="cyan3", pch=16)
survbyage.ac<-lm(PropSurv~log(Age+1), data=subset(surv, surv$Species=="EreCya" & !is.na(surv$ClutchSize)))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="EreCya")), to=max(subset(surv$Age, surv$Species=="EreCya")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="cyan3", lwd=2, lty=2)

points(PropSurv~Age, data=subset(surv, surv$Species=="JulOrn" & !is.na(surv$ClutchSize)), col="darkgoldenrod1", pch=16)
survbyage.ac<-lm(PropSurv~log(Age+1), data=subset(surv, surv$Species=="JulOrn" & !is.na(surv$ClutchSize)))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="JulOrn")), to=max(subset(surv$Age, surv$Species=="JulOrn")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="darkgoldenrod1", lwd=2, lty=2)

points(PropSurv~Age, data=subset(surv, surv$Species=="NeoPul" & !is.na(surv$ClutchSize)), col="chocolate1", pch=16)
survbyage.ac<-lm(PropSurv~log(Age+1), data=subset(surv, surv$Species=="NeoPul" & !is.na(surv$ClutchSize)))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="NeoPul")), to=max(subset(surv$Age, surv$Species=="NeoPul")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="chocolate1", lwd=2, lty=2)

points(PropSurv~Age, data=subset(surv, surv$Species=="TelTem" & !is.na(surv$ClutchSize)), col="darkorchid4", pch=16)
survbyage.ac<-lm(PropSurv~log(Age+1), data=subset(surv, surv$Species=="TelTem" & !is.na(surv$ClutchSize)))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="TelTem")), to=max(subset(surv$Age, surv$Species=="TelTem")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col="darkorchid4", lwd=2, lty=2)

points(PropSurv~Age, data=subset(surv, surv$Species=="VarMoo" & !is.na(surv$ClutchSize)), col=1, pch=16)
survbyage.ac<-lm(PropSurv~log(Age+1), data=subset(surv, surv$Species=="VarMoo" & !is.na(surv$ClutchSize)))
survbyage.ac.x<-expand.grid(Age=seq(from=min(subset(surv$Age, surv$Species=="VarMoo")), to=max(subset(surv$Age, surv$Species=="VarMoo")), length=1000))
survbyage.ac.y<-predict.lm(survbyage.ac, newdata=survbyage.ac.x, type="response")
survbyage.ac.data<-data.frame(survbyage.ac.x, survbyage.ac.y)
lines(survbyage.ac.y~Age, data=survbyage.ac.data, col=1, lwd=2, lty=2)

legend("bottomleft", c("B"), bty="n", cex=1.5)

#plot temporal change in clutch size per individual-----
par(mar=c(5, 5, 1, 1), mfrow=c(3, 2))
plot(ClutchSize~Days, data=subset(byi, byi$Species=="AltCom"), col=0)
lines(ClutchSize~Days, data=subset(byi, byi$ID=="AltCom_F_001"), col="burlywood")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="AltCom_F_002"), col="burlywood")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="AltCom_F_003"), col="burlywood")
abline(lm(ClutchSize~Days, data=subset(byi, byi$Species=="AltCom")), col="burlywood")

plot(ClutchSize~Days, data=subset(byi, byi$Species=="JulOrn"), col=0)
lines(ClutchSize~Days, data=subset(byi, byi$ID=="JulOrn_F_001"), col="darkgoldenrod1")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="JulOrn_F_002"), col="darkgoldenrod1")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="JulOrn_F_003"), col="darkgoldenrod1")
abline(lm(ClutchSize~Days, data=subset(byi, byi$Species=="JulOrn")), col="darkgoldenrod1")

plot(ClutchSize~Days, data=subset(byi, byi$Species=="LepElo"), col=0)
lines(ClutchSize~Days, data=subset(byi, byi$ID=="LepElo_F_001"), col="azure4")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="LepElo_F_002"), col="azure4")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="LepElo_F_003"), col="azure4")
abline(lm(ClutchSize~Days, data=subset(byi, byi$Species=="LepElo")), col="azure4")

plot(ClutchSize~Days, data=subset(byi, byi$Species=="NeoPul"), col=0)
lines(ClutchSize~Days, data=subset(byi, byi$ID=="NeoPul_F_001"), col="chocolate1")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="NeoPul_F_002"), col="chocolate1")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="NeoPul_F_003"), col="chocolate1")
abline(lm(ClutchSize~Days, data=subset(byi, byi$Species=="NeoPul")), col="chocolate1")

plot(ClutchSize~Days, data=subset(byi, byi$Species=="TelTem"), col=0)
lines(ClutchSize~Days, data=subset(byi, byi$ID=="TelTem_F_001"), col="darkorchid4")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="TelTem_F_002"), col="darkorchid4")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="TelTem_F_003"), col="darkorchid4")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="TelTem_F_004"), col="darkorchid4")
lines(ClutchSize~Days, data=subset(byi, byi$ID=="TelTem_F_005"), col="darkorchid4")
abline(lm(ClutchSize~Days, data=subset(byi, byi$Species=="TelTem")), col="darkorchid4")

plot(ClutchSize~Days, data=subset(byi, byi$Species=="VarMoo"), col=0)
lines(ClutchSize~Days, data=subset(byi, byi$ID=="VarMoo_F_001"))
lines(ClutchSize~Days, data=subset(byi, byi$ID=="VarMoo_F_002"))
lines(ClutchSize~Days, data=subset(byi, byi$ID=="VarMoo_F_003"))
lines(ClutchSize~Days, data=subset(byi, byi$ID=="VarMoo_F_004"))
lines(ClutchSize~Days, data=subset(byi, byi$ID=="VarMoo_F_005"))
abline(lm(ClutchSize~Days, data=subset(byi, byi$Species=="VarMoo")), col=1)

#plot temporal change in inter-clutch interval per individual----
par(mar=c(5, 5, 1, 1), mfrow=c(3, 2))
plot(ICI~Days, data=subset(byi, byi$Species=="AltCom"), col=0)
lines(ICI~Days, data=subset(byi, byi$ID=="AltCom_F_001"), col="burlywood")
lines(ICI~Days, data=subset(byi, byi$ID=="AltCom_F_002"), col="burlywood")
lines(ICI~Days, data=subset(byi, byi$ID=="AltCom_F_003"), col="burlywood")
abline(lm(ICI~Days, data=subset(byi, byi$Species=="AltCom")), col="burlywood")

plot(ICI~Days, data=subset(byi, byi$Species=="JulOrn"), col=0)
lines(ICI~Days, data=subset(byi, byi$ID=="JulOrn_F_001"), col="darkgoldenrod1")
lines(ICI~Days, data=subset(byi, byi$ID=="JulOrn_F_002"), col="darkgoldenrod1")
lines(ICI~Days, data=subset(byi, byi$ID=="JulOrn_F_003"), col="darkgoldenrod1")
abline(lm(ICI~Days, data=subset(byi, byi$Species=="JulOrn")), col="darkgoldenrod1")

plot(ICI~Days, data=subset(byi, byi$Species=="LepElo"), col=0)
lines(ICI~Days, data=subset(byi, byi$ID=="LepElo_F_001"), col="azure4")
lines(ICI~Days, data=subset(byi, byi$ID=="LepElo_F_002"), col="azure4")
lines(ICI~Days, data=subset(byi, byi$ID=="LepElo_F_003"), col="azure4")
abline(lm(ICI~Days, data=subset(byi, byi$Species=="LepElo")), col="azure4")

plot(ICI~Days, data=subset(byi, byi$Species=="NeoPul"), col=0)
lines(ICI~Days, data=subset(byi, byi$ID=="NeoPul_F_001"), col="chocolate1")
lines(ICI~Days, data=subset(byi, byi$ID=="NeoPul_F_002"), col="chocolate1")
lines(ICI~Days, data=subset(byi, byi$ID=="NeoPul_F_003"), col="chocolate1")
abline(lm(ICI~Days, data=subset(byi, byi$Species=="NeoPul")), col="chocolate1")

plot(ICI~Days, data=subset(byi, byi$Species=="TelTem"), col=0)
lines(ICI~Days, data=subset(byi, byi$ID=="TelTem_F_001"), col="darkorchid4")
lines(ICI~Days, data=subset(byi, byi$ID=="TelTem_F_002"), col="darkorchid4")
lines(ICI~Days, data=subset(byi, byi$ID=="TelTem_F_003"), col="darkorchid4")
lines(ICI~Days, data=subset(byi, byi$ID=="TelTem_F_004"), col="darkorchid4")
lines(ICI~Days, data=subset(byi, byi$ID=="TelTem_F_005"), col="darkorchid4")
abline(lm(ICI~Days, data=subset(byi, byi$Species=="TelTem")), col="darkorchid4")

plot(ICI~Days, data=subset(byi, byi$Species=="VarMoo"), col=0)
lines(ICI~Days, data=subset(byi, byi$ID=="VarMoo_F_001"))
lines(ICI~Days, data=subset(byi, byi$ID=="VarMoo_F_002"))
lines(ICI~Days, data=subset(byi, byi$ID=="VarMoo_F_003"))
lines(ICI~Days, data=subset(byi, byi$ID=="VarMoo_F_004"))
lines(ICI~Days, data=subset(byi, byi$ID=="VarMoo_F_005"))
abline(lm(ICI~Days, data=subset(byi, byi$Species=="VarMoo")), col=1)


#plot egg mass, clutch size, and inter-clutch interval by species----
par(mar=c(5, 5, 1, 5), mfrow=c(1, 1))
boxplot(ClutchSize~Species, data=byi, xaxt="n", cex.lab=1.5, xlab="", ylab="Clutch size", outline=F,
        at=c(1, 3, 5, 7, 9, 11), xlim=c(0.5, 12.5), ylim=c(0, 1100),
        col=c("burlywood", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", 1),
        medcol=c(1, 1, 1, 1, 1, 0)) 
boxplot((ICI*10)~Species, data=byi, xaxt="n", cex.lab=1.5, xlab="", outline=F, add=T,
        at=c(2, 4, 6, 8, 10, 12), xlim=c(0.5, 12.5), ylim=c(0, 1100),
        col=c("burlywood", "darkgoldenrod1", "azure4", "chocolate1", "darkorchid4", 1),
        medcol=c(1, 1, 1, 1, 1, 0))
text(x=c(0.5, 1.5, 3.5, 5.5, 7.5, 9.5, 11.5), y=c(1075, 1075, 1075, 1075, 1075, 1075), c("FM:", "11.08", "6.08", "28.86", "13.01", "3.27", "34.63"))
text(x=c(0.5, 1.5, 3.5, 5.5, 7.5, 9.5, 11.5), y=c(1025, 1025, 1025, 1025, 1025, 1025), c("EM:", "7.06", "2.33", "2.55", "1.69", "2.66", "1.35"))
axis(side=4, at=c(0, 200, 400, 600, 800, 1000), c("0", "20", "40", "60", "80", "100"))
axis(side=4, at=c(550), tick=F, mgp=c(3, 3, 1), cex.axis=1.5, c("Inter-clutch interval")) 
axis(side=1, at=c(1, 3, 5, 7, 9, 11), c("CS", "CS", "CS", "CS", "CS", "CS"), cex.axis=0.75)
axis(side=1, at=c(2, 4, 6, 8, 10, 12), c("IC", "IC", "IC", "IC", "IC", "IC"), cex.axis=0.75)
axis(side=1, at=c(1.5, 3.5, 5.5, 7.5, 9.5, 11.5), c("AltCom", "JulOrn", "LepElo", "NeoPul", "TelTem", "VarMoo"), tick=F, mgp=c(3, 3, 1), cex.axis=1.25)
