#Code for "Offspring fertility and grandchild survival  enhanced by maternal grandmothers in a pre-industrial human society"
#Chapman SN, Lahdenperä M, Pettay JE, Lynch RF, Lummaa V

#NOTES#
#1 - datasets are attached as supplementary .csv files, available alongside the manuscript online
#2 - one row in each file does not necessarily equate to one individual
#3 - mgm = maternal grandmother/mother, pgm = paternal grandmother/mother-in-law 
#4 - convergence error warning messages come from the use of nloptwrap
#5 - apologies for any unclear code. If you have questions/comments, please direct them to the corresponding author (SN Chapman)

#REQUIRED PACKAGES
library(lme4)
library(afex)

#FILES
#Fertility file
dfFERT <- read.csv("Chapman_fertility_data_ESM.csv", stringsAsFactors = F, na.strings = NA)
#Survival file
dfSURV <- read.csv("Chapman_survival_data_ESM.csv", stringsAsFactors = F, na.strings = NA)

#ANALYSIS 1 - FERTILITY: AGE AT FIRST BIRTH----
#variables:
#birth - birth status of the individual, 1 = birth, 0 = no birth
#age - age of the focal individul
#mumtype - mother/mother-in-law presence, coded as a.none for neither alive, b.both for both alive, c.mum for only mother alive, d.mil for only mother-in-law alive
#bOrder - birth order of the focal individual
#living_sibs - number of living siblings
#mumid - (grand)mother ID
#ageFig - age of the focal individual (NOT mean-centred) for use in figures

#select relevant columns in file
dfAF <- dfFERT[,c(1:7)]
#note: due to added NAs on columns 1:7, mixed() will not work unless these are removed
dfAF <- dfAF[complete.cases(dfAF),]

afbMod <- glmer(birth ~ age * mumtype + I(age^2) + bOrder + living_sibs + (1|mumid), data = dfAF, family = binomial(link = 'logit'), control = glmerControl(optimizer = 'nloptwrap', optCtrl = list(maxfun = 150000)))


afbModMix <- mixed(birth ~ age * mumtype + I(age^2) + bOrder + living_sibs + (1|mumid), data = dfAF, family = binomial(link = 'logit'), control = glmerControl(optimizer = 'nloptwrap', optCtrl = list(maxfun = 250000)), method = 'LRT')

#ANALYSIS 2 - FERTILITY: BIRTH SPACING----
#variables:
#birth - birth status of the individual, 1 = birth, 0 = no birth
#age - age of the focal individul
#tlb - mean-centred time since last birth in years
#mumtype - mother/mother-in-law presence, coded as a.none for neither alive, b.both for both alive, c.mgm for only mother alive, d.pgm for only mother-in-law alive
#prevkid - survival status of the previous child, 1 = alive, 0 = dead
#kidBord - birth order of the next child to be born
#mumid - (grand)mother ID
#ID - focal individual ID
#bcohort - birth cohort of the first born child
#tlbFig - time since last birth in years (NOT mean-centered) for use in figures

#select relevant columns in file
ibiDF <- dfFERT[,c(8:18)]

ibiMod <- glmer(birth2 ~ age2*tlb2*mumtype2 + tlb2*prevkid2 + tlb2*kidBord2*mumtype2 + (1|mumid2/ID2) + (1|bcohort2), data = ibiDF, family = binomial (link = 'logit'), control = glmerControl(optimizer = 'nloptwrap', optCtrl = list(maxfun = 150000)))

ibiModMix <- mixed(birth2 ~ age2*tlb2*mumtype2 + tlb2*prevkid2 + tlb2*kidBord2*mumtype2 + (1|mumid2/ID2) + (1|bcohort2), data = ibiDF, family = binomial (link = 'logit'), control = glmerControl(optimizer = 'nloptwrap', optCtrl = list(maxfun = 150000)), method = "LRT")

#ANALYSIS 3 - SURVIVAL: CHILDHOOD AGE CLASSES----
#variables:
#survstatus - survival status in that year for the focal individual, 1 = alive, 0 = dead
#GMtype - grandmother presence, both for both alive, none for neither alive, MGM for maternal,  PGM for paternal
#age - age in years
#twin - 1 for singleton, 2 for twin 
#chsoclass - childhood social class, landed or landless
#sex - 0 male, 1 female
#mstatus - mother status: alive, dead, disappeared from records
#parishArea - area of Finland: inl = Pirkanmaa, kar = Karelia, arch = Archipelago, pul = Northern Ostrobothnia
#mumid - mother id
#mgmid - maternal grandmother id
#bcohort - birth cohort (ten-year bins)
#livesibnum - number of living siblings 

#0-2
df02 <- dfSURV[,c(1:11)]

df02$GMtype <- relevel(as.factor(df02$GMtype), ref = 'none')

mvp.02 <- glmer(survstatus ~ GMtype + age + twin + chsoclass + sex + mstatus + parishArea + (1|mgmid/mumid) + (1|bcohort), data = df02, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#2-5
df25 <- dfSURV[,c(12:18)]

df25$GMtype2 <- relevel(as.factor(df25$GMtype2), ref = 'none')

mvp.25 <- glmer(survstatus2 ~ GMtype2 + age2 + mstatus2 + (1|mgmid2/mumid2) + (1|bcohort2), data = df25, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#5-15
df515 <- dfSURV[,c(19:26)]

df515$GMtype3 <- relevel(as.factor(df515$GMtype3), ref = 'none')

mvp.515 <- glmer(survstatus3 ~ GMtype3 + age3 + livesibnum3 + mstatus3 + (1|mgmid3/mumid3) + (1|bcohort3), data = df515, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#note: singular fit from random effects structure for 2-5. Outputs reported in supplementary and odds ratios in the main manuscript are from the above code with the full random effects structure (to keep it the same across the models)
#removing the 'offending' random effect gives similar results:
mvp.25ALT <- glmer(survstatus2 ~ GMtype2 + age2 + mstatus2 + (1|mgmid2) + (1|bcohort2), data = df25, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#ANALYSIS 4 - SURVIVAL: LINEAGE EXCLUSIVITY----
#variables:
#survstatus - survival status in that year for the focal individual, 1 = alive, 0 = dead
#GM - grandmother presence, both for both alive, none for neither alive, MGM for maternal with no paternal grandchildren, MGM.P for maternal with paternal grandchildren, PGM for paternal with no maternal grandchildren, PGM.M for paternal with maternal grandchildren
#age - age in years
#twin - 1 for singleton, 2 for twin 
#chsoclass - childhood social class, landed or landless
#sex - 0 male, 1 female
#mstatus - mother status: alive, dead, disappeared from records
#parishArea - area of Finland: inl = Pirkanmaa, kar = Karelia, arch = Archipelago, pul = Northern Ostrobothnia
#mumid - mother id
#mgmid - maternal grandmother id
#bcohort - birth cohort (ten-year bins)
#livesibnum - number of living siblings 

#0-2
dfyng <- dfSURV[,c(27:37)]

mvp.yng <- glmer(survstatus4 ~ GM4 + age4 + twin4 + chsoclass4 + sex4 + mstatus4 + parishArea4 + (1|mgmid4/mumid4) + (1|bcohort4), data = dfyng, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#2-5
dfmid <- dfSURV[,c(38:44)]
mvp.mid <- glmer(survstatus5 ~ GM5 + age5 + mstatus5 + (1|mgmid5/mumid5) + (1|bcohort5), data = dfmid, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#5-15
dfold <- dfSURV[,c(45:52)]
mvp.old <- glmer(survstatus6 ~ GM6 + age6 + livesibnum6 + mstatus6 + (1|mgmid6/mumid6) + (1|bcohort6), data = dfold, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#note: singular fit from random effects structure for 2-5 and 5-15. Outputs reported in supplementary and odds ratios in the main manuscript are from the above code with the full random effects structure (to keep it the same across the models)
#removing the 'offending' random effects gives similar results:
mvp.midALT <- glmer(survstatus5 ~ GM5 + age5 + mstatus5 + (1|mgmid5) + (1|bcohort5), data = dfmid, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

mvp.oldALT <- glmer(survstatus6 ~ GM6 + age6 + livesibnum6 + mstatus6 + (1|mgmid6/mumid6), data = dfold, family = binomial(link = "logit"), control = glmerControl(optimizer = "nloptwrap", optCtrl = list(maxfun = 250000)))

#ODDS RATIOS----
#get odds ratios from all above models - this will also pull the afbMod and ibiMod, but these can be ignored
modNames <- Filter(function(x) 'glmerMod' %in% class(get(x)), ls()) #makes a list of all glmerMod objects in the environment
L1 <- lapply(modNames, function(x) get(x)) #gets all named glmerMod objects in L1 and puts them in a list

ORs <- list() #create empty list for odds ratios

for(i in 1:length(L1)){ #loops through for each glmerMod in L1
  crr_frame = L1[[i]] #set current item in list as "crr_frame"
  se = sqrt(diag(vcov(crr_frame))) #get standard errors (for conf intervals)
  tab = cbind(Est = fixef(crr_frame), LL = fixef(crr_frame) - 1.96 * se, UL = fixef(crr_frame) + 1.96 * se) #make a table of fixed effects and standard errors from  glmerMod
  ORs[[i]] = exp(tab) #exponentiate values to gets odds ratios - these are put in the "ORs" list object
  names(ORs)[[i]] = paste0(modNames[i]) #rename each item in the "ORs" list with the model name
}

#to view odds ratios for fixed effects (with 95% confidence intervals), type e.g. ORs$dfTOD

#FIGURE 1----
#requires running of ANALYSIS 1
#obtain model-predicted values for each observation
dfAF$pred <- predict(afbMod, newdata = dfAF, type = 'response', allow.new.levels = T)

par(mar = c(2,2,0.1,0.1), oma = c(2,2.5,1,1))
plot(dfAF$pred~dfAF$ageFig, type = "n", ylab = "", xlab = "", axes = F, yaxs = 'i', xaxs = 'i', ylim = c(0,0.4))

lines(smooth.spline(x = dfAF[which(dfAF$mumtype == 'a.none'),]$ageFig, y = dfAF[which(dfAF$mumtype == 'a.none'),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = dfAF[which(dfAF$mumtype == 'b.both'),]$ageFig, y = dfAF[which(dfAF$mumtype == 'b.both'),]$pred), lwd = 2, col = 'black')
lines(smooth.spline(x = dfAF[which(dfAF$mumtype == 'c.mum'),]$ageFig, y = dfAF[which(dfAF$mumtype == 'c.mgm'),]$pred), lwd = 2, col = 'dodgerblue1')
lines(smooth.spline(x = dfAF[which(dfAF$mumtype == 'd.mil'),]$ageFig, y = dfAF[which(dfAF$mumtype == 'd.pgm'),]$pred), lwd = 2, col = 'dodgerblue1', lty = 2)

box(col = "black")
axis(side = 1, at = seq(16, 44, 4), labels = seq(16, 44, 4), cex.axis = 0.9)
axis(side = 2, at = seq(0, 0.5, 0.1), labels = seq(0, 0.5, 0.1), las = 1, cex.axis = 0.9)

mtext(side = 1, line = 2.5, 'Age', cex = 0.9)
mtext(side = 2, line = 3, 'Probability of reproducing', cex = 0.9)

#FIGURE 2----
#requires running of ANALYSIS 2
#obtain model-predicted values for each observation
ibiDF$pred <- predict(ibiMod, newdata = ibiDF, type = 'response', allow.new.levels = T)

par(mfrow = c(3,2), oma = c(4,3.25,0.1,0))

#Panel 1 - Age 25
par(mar = c(0.1,2,0.1,0.1))
plot(ibiDF$pred~ibiDF$tlbFig, type = "n", xlim = c(0.5,10), axes = F, xaxs = 'i', yaxs = 'i')
box(col = "black")
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 1, cex.axis = 0.9)
text(x = 1, y = 0.95, "A")
abline(v = 5, lty = 2)
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$pred), lwd = 2, col = 'black')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$pred), lwd = 2, col = 'dodgerblue1')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 25),]$pred), lwd = 2, col = 'dodgerblue1', lty = 2)

#Panel 2 - Age 30
par(mar = c(0.1,0.1,0.1,2))
plot(ibiDF$pred~ibiDF$tlbFig, type = "n", xlim = c(0.5,10), axes = F, xaxs = 'i', yaxs = 'i')
box(col = "black")
text(x = 1, y = 0.95, "B")
abline(v = 5, lty = 2)
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$pred), lwd = 2, col = 'black')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$pred), lwd = 2, col = 'dodgerblue1')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 30),]$pred), lwd = 2, col = 'dodgerblue1', lty = 2)


#Panel 3 - age 35
par(mar = c(0.1,2,0.1,0.1))
plot(ibiDF$pred~ibiDF$tlbFig, type = "n", xlim = c(0.5,10), axes = F, xaxs = 'i', yaxs = 'i')
box(col = "black")
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 1, cex.axis = 0.9)
text(x = 1, y = 0.95, "C")
abline(v = 5, lty = 2)
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$pred), lwd = 2, col = 'black')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$pred), lwd = 2, col = 'dodgerblue1')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$ageFig2 == 35),]$pred), lwd = 2, col = 'dodgerblue1', lty = 2)

#Panel 4 - birth orders < 4
par(mar = c(0.1,0.1,0.1,2))
plot(ibiDF$pred~ibiDF$tlbFig, type = "n", xlim = c(0.5,10), axes = F, xaxs = 'i', yaxs = 'i')
box(col = "black")
text(x = 1, y = 0.95, "D")
abline(v = 5, lty = 2)
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$pred), lwd = 2, col = 'black')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$pred), lwd = 2, col = 'dodgerblue1')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 <= 4),]$pred), lwd = 2, col = 'dodgerblue1', lty = 2)

#Panel 5 - birth orders >4
par(mar = c(0.1,2,0.1,0.1))
plot(ibiDF$pred~ibiDF$tlbFig, type = "n", xlim = c(0.5,10), axes = F, xaxs = 'i', yaxs = 'i')
box(col = "black")
axis(side = 1, at = seq(0, 10, 1), labels = seq(0, 10, 1), cex.axis = 0.9)
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 1, cex.axis = 0.9)
text(x = 1, y = 0.95, "E")
abline(v = 5, lty = 2)
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$pred), lwd = 2, col = 'black')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$pred), lwd = 2, col = 'dodgerblue1')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$tlbFig, y = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F & ibiDF$kidBord2 > 4),]$pred), lwd = 2, col = 'dodgerblue1', lty = 2)

#Panel 6 - status of previous child
par(mar = c(0.1,0.1,0.1,2))
plot(ibiDF$pred~ibiDF$tlbFig, type = "n", xlim = c(0.5,10), axes = F, xaxs = 'i', yaxs = 'i')
box(col = "black")
axis(side = 1, at = seq(0, 10, 1), labels = seq(0, 10, 1), cex.axis = 0.9)
text(x = 1, y = 0.95, "F")

abline(v = 5, lty = 2)
lines(smooth.spline(x = ibiDF[which(ibiDF$prevkid2 == 0 & is.na(ibiDF$pred) == F),]$tlbFig, y = ibiDF[which(ibiDF$prevkid2 == 0 & is.na(ibiDF$pred) == F),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = ibiDF[which(ibiDF$prevkid2 == 1 & is.na(ibiDF$pred) == F),]$tlbFig, y = ibiDF[which(ibiDF$prevkid2 == 1 & is.na(ibiDF$pred) == F),]$pred), lwd = 2, col = 'black')

mtext(side = 2, line = 1.5, "Probability of reproduction", cex = 0.9, outer = T)
mtext(side = 1, line = 2.5, "Years since last birth", cex = 0.9, outer = T)

#FIGURE 3----
#values in the data.frame object below are taken from the odds ratios (or) and lower (low) and upper (up) confidence intervals on these odds ratios (ANALYSIS 3)
plot3 <- data.frame(name = as.character(c('BOTH', 'MGM', 'PGM')), or = c(1.078,1.118,0.940,1.312,1.428,1.118,1.213,0.930,0.955), up = c(1.340,1.409,1.191,1.680,1.841,1.447,1.724,1.248,1.311), low = c(0.867,0.887,0.742,1.025,1.107,0.863,0.853,0.694,0.695),  frame = c(rep('yng',3), rep('mid',3), rep('old',3)))

par(oma = c(3,0.1,0.1,0.1), mar = c(1,0.1,0.1,0.1))
plot(c(1:10)~c(10:1), type = 'n', axes = F, ylim = c(0.5,9.5), xlim = c(0,2), ylab = '', xlab = '', xaxs = 'i', yaxs = 'i')
abline(h = c(9.5, 6.5, 3.5), lty = 3)
abline(v = 1, lty = 2)

axis(side = 1, at = c(0,0.5,1,1.5,2), labels = c(0,0.5,1,1.5,2), cex = 0.8)
mtext(side = 1, 'Odds ratio', line = 2.25)

text(x = 0.01, y = 9, '0-2', cex = 0.9, pos = 4)
text(x = 0.01, y = 6, '2-5', cex = 0.9, pos = 4)
text(x = 0.01, y = 3, '5-15', cex = 0.9, pos = 4)

for(i in seq_along(plot3$name)){
  lines(x = c(plot3[i,3], plot3[i,4]), y = c(10-i, 10-i), col = 'gray', lwd = 2)
  points(x = plot3[i,2], y = 10-i, pch = 21, bg = 'gray')
  text(x = 0.2, y = 10-i, plot3[i,1], cex = 0.7, pos = 4)
}

#FIGURE 4----
#values in the data.frame object below are taken from the odds ratios (or) and lower (low) and upper (up) confidence intervals on these odds ratios (ANALYSIS 4)
plot4 <- data.frame(name = as.character(rep(c('BOTH', 'MGM', 'MGM+PGM', 'PGM','PGM+MGM'),3)), or = c(1.077, 1.092, 1.195, 0.908, 1.105, 1.324, 1.341, 1.964, 1.050, 1.573,1.127, 0.769,1.392,0.897,1.027), up = c(1.339,1.394,1.762,1.165,1.684,1.699,1.760,3.233,1.387,2.60,1.626,1.066,2.503,1.299,1.802), low = c(0.866,0.855,0.811,0.708,0.725,1.032,1.022,1.193,0.795,0.952,0.782,0.555,0.775,0.620,0.585),  frame = c(rep('yng',5), rep('mid',5), rep('old',5)))

par(oma = c(3,0.1,0.1,0.1), mar = c(1,0.1,0.1,0.1))
plot(c(1:10)~c(10:1), type = 'n', axes = F, ylim = c(0.5,15.5), xlim = c(0,3.4), ylab = '', xlab = '', xaxs = 'i', yaxs = 'i')
abline(h = c(10.5, 5.5), lty = 3)
abline(v = 1, lty = 2)

axis(side = 1, at = c(0,0.5,1,1.5,2, 3, 3.5), labels = c(0,0.5,1,1.5,2,3,3.5), cex = 0.8)
mtext(side = 1, 'Odds ratio', line = 2.25)

text(x = 3, y = 15, '0-2', cex = 0.8, pos = 4)
text(x = 3, y = 10, '2-5', cex = 0.8, pos = 4)
text(x = 3, y = 5, '5-15', cex = 0.8, pos = 4)

for(i in seq_along(plot4$name)){
  lines(x = c(plot4[i,3], plot4[i,4]), y = c(16-i, 16-i), col = 'gray', lwd = 2)
  points(x = plot4[i,2], y = 16-i, pch = 21, bg = 'gray')
  text(x = 0.01, y = 16-i, plot4[i,1], cex = 0.6, pos = 4)
}


#FIGURE S1----
par(mfrow = c(1,1), mar = c(2,2,0.1,0.1), oma = c(2,2.5,1,1))
plot(ibiDF$pred~ibiDF$kidBord, type = "n", xlim = c(1.5,10), ylim = c(0, 0.5), axes = F, xaxs = 'i', yaxs = 'i')
box(col = "black")
axis(side = 1, at = seq(0, 10, 1), labels = seq(0, 10, 1), cex.axis = 0.9)
axis(side = 2, at = seq(0, 1, 0.1), labels = seq(0, 1, 0.1), las = 1, cex.axis = 0.9)


abline(v = 5, lty = 2)
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F),]$kidBord, y = ibiDF[which(ibiDF$mumtype2 == 'a.none' & is.na(ibiDF$pred) == F),]$pred), lwd = 2, col = 'gray')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F),]$kidBord, y = ibiDF[which(ibiDF$mumtype2 == 'b.both' & is.na(ibiDF$pred) == F),]$pred), lwd = 2, col = 'black')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F),]$kidBord, y = ibiDF[which(ibiDF$mumtype2 == 'c.mgm' & is.na(ibiDF$pred) == F),]$pred), lwd = 2, col = 'dodgerblue1')
lines(smooth.spline(x = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F),]$kidBord, y = ibiDF[which(ibiDF$mumtype2 == 'd.pgm' & is.na(ibiDF$pred) == F),]$pred), lwd = 2, col = 'dodgerblue1', lty = 2)

mtext(side = 1, line = 2.5, 'Birth order', cex = 0.9)
mtext(side = 2, line = 3, 'Probability of reproducing', cex = 0.9)

