## packages to load
library(lme4)
library(car)
library(MuMIn)

# please save the supplementary dataset as a CSV and upload (provided as a supplementary excel file with two sheets)

#upload female data
f.data=read.csv("Female participation model.csv", header=TRUE)

#upload male data
m.data=read.csv("Male participation model.csv", header=TRUE)

#explanation of variables
#variables shown with "(z)" were z-transformed to a mean of 0 and a standard deviation of 1)
##
#test predictors
	# Bond_partner_present - no/yes indicating whether a social bond partner is present in the intergroup encounter
	# Maternal_kin_present - no/yes indicating whether an adult maternal kin is present in the intergroup encounter
	# Number_female_present (z) - the total number of females present (for males) or other females present (for females) in the intergroup encounter 
	# Number_male_present (z) - the total number of males present (for females) or other males present (for males) in the intergroup encounter

##
#control predictors
		#control predictors in both models:
	# Number_kin_group (z) - the total number of maternal kin in the community
	# Dominance_rank (z) - the dominance rank of the individual with 1 being the highest ranking
	# Age (z) - the age (in years) of the individual
	# Average_dyadic_association (z) - the average dyadic association value with intergroup encounter participants 
	# Food_availability (z) - monthly food availability index
	# Encounter_type - indicating if the encounter was a vocal or contact encounter
	# community - indicating the identity of the social group (i.e., North, South, East)
		#control predictors in the 'female participation model'  only:
	# Pregnancy_days (z) - the number of days into pregnancy 
	# Independent_son - indicating whether a female had a son >8yrs in the community
	# oestrus - indicating whether a female showed maximal sexual swelling
	# Infant_under_2yrs - indicating whether a female had an infant <2yrs
		#control predictors in the 'male participation model' only:
	# Number_offspring (z) - the number of living offspring a male had in the community 

##
#offset term
	# Group_size_log - log transfomed 

##
#random effects
	# Potential_participant
	# Encounter_identity


##set the optimizer 
contr=glmerControl(optimizer="bobyqa", optCtrl=list(maxfun=1000000))

############################
############################
##### Female Participation Model

# check multicollinearity issues using Variance Inflation Factors (package 'car')
vif(lm(Response ~ Bond_partner_present+Maternal_kin_present+Independent_son+Number_kin_group+Dominance_rank+Age+Encounter_type+Average_dyadic_association+
			Number_female_present+Number_male_present+Pregnancy_days+oestrus+Infant_under_2yrs+community+Food_availability, data=f.data))[, 3]^2


model.female= glmer(Response ~ Bond_partner_present+Maternal_kin_present+Number_female_present+Number_male_present+
				Average_dyadic_association+Number_kin_group+Independent_son+Dominance_rank+Age+I(Age^2)+Encounter_type+
				Pregnancy_days+oestrus+Infant_under_2yrs+community+Food_availability+offset(Group_size_log)+
				(1+Dominance_rank+Age+I(Age^2)+Number_female_present+Number_male_present+Food_availability+Number_kin_group+Pregnancy_days+Average_dyadic_association|Potential_participant)+
				(1+Dominance_rank+Age+I(Age^2)+Number_kin_group+Average_dyadic_association|Encounter_identity),
				data=f.data, family="binomial", control=contr)

null.female= glmer(Response ~ 
				Average_dyadic_association+Number_kin_group+Independent_son+Dominance_rank+Age+I(Age^2)+Encounter_type+
				Pregnancy_days+oestrus+Infant_under_2yrs+community+Food_availability+offset(Group_size_log)+
				(1+Dominance_rank+Age+I(Age^2)+Number_female_present+Number_male_present+Food_availability+Number_kin_group+Pregnancy_days+Average_dyadic_association|Potential_participant)+
				(1+Dominance_rank+Age+I(Age^2)+Number_kin_group+Average_dyadic_association|Encounter_identity),
				data=f.data, family="binomial", control=contr)

#full null model comparison
as.data.frame(anova(null.female, model.female, test="Chisq"))

#sumary of model results
summary(model.female)

#use the drop1 function to obtain p values
pvals.female=as.data.frame(drop1(model.female, test="Chisq"))

#Effect sizes (package MuMIn)
r.squaredGLMM(model.female)


##########################
##########################
##### Male Participation Model

# check multicollinearity issues using Variance Inflation Factors (package 'car')
vif(lm(Response ~ Bond_partner_present+Maternal_kin_present+Number_female_present+Number_male_present+
		Number_offspring+Dominance_rank+Age+Encounter_type+Average_dyadic_association+community+Food_availability, data=m.data))[, 3]^2


model.male= glmer(Response ~ Bond_partner_present+Maternal_kin_present+Number_female_present+Number_male_present+
				Average_dyadic_association+Number_offspring+Dominance_rank+Age+I(Age^2)+Encounter_type+community+Food_availability+offset(Group_size_log)+
				(1+Dominance_rank+Age+I(Age^2)+Number_offspring+Number_female_present+Number_male_present+Food_availability+Average_dyadic_association|Potential_participant)+
				(1+Dominance_rank+Age+I(Age^2)+Number_offspring+Average_dyadic_association|Encounter_identity),
				data=m.data, family="binomial", control=contr)

model.male= glmer(Response ~ 
				Average_dyadic_association+Number_offspring+Dominance_rank+Age+I(Age^2)+Encounter_type+community+Food_availability+offset(Group_size_log)+
				(1+Dominance_rank+Age+I(Age^2)+Number_offspring+Number_female_present+Number_male_present+Food_availability+Average_dyadic_association|Potential_participant)+
				(1+Dominance_rank+Age+I(Age^2)+Number_offspring+Average_dyadic_association|Encounter_identity),
				data=m.data, family="binomial", control=contr)


#full null model comparison
as.data.frame(anova(null.male, model.male, test="Chisq"))

#sumary of model results
summary(model.male)

#use the drop1 function to obtain p values
pvals.male=as.data.frame(drop1(model.male, test="Chisq"))

#effect sizes
r.squaredGLMM(model.male)


