rm(list=ls())
#load packages
library(readxl)
library(car)
library(glmmTMB)
library(DHARMa)
library(tidyverse)

### Exp. 1 LD first then HD ----
# LD = low web density; HD = high web density; fdensity = environment variable (LD or HD)
# safety = safety section silk strand density; prey = prey-capture section silk density; overall = overall silk density
# group = grouping variable that assigned which spider were exposed to each other
# spider= unique spider identifier
webdata <- as.data.frame(read_excel("web data.xlsx"))# Load dataset from an Excel file
webdata$fdensity1 <- factor(webdata$fdensity1, levels = c("LD", "HD"))# Create density exposures as a factor with 2 levels (LD and HD)
webdata$spider1 <- as.character(c(seq(1, 45), seq(1, 45)))# Convert 'spider1' variable to character type using seq() function
View(webdata) # View and summarize the 'webdata' dataframe
summary(webdata)
attach(webdata) # Attach 'webdata' dataframe to the search path

###Safety investment of Exp. 1
# Fit a GLMM (Generalized Linear Mixed Model) with TWEEDIE family 
LDHDs <- glmmTMB(safety1 ~ fdensity1 + (1 | group1/spider1), data = webdata, family = tweedie())
# Test for significance using Type III ANOVA
Anova(LDHDs, type = "III")

##Prey-capture investment of Exp. 1
# Fit a GLMM (Generalized Linear Mixed Model) with TWEEDIE family
LDHDp <- glmmTMB(prey1 ~ fdensity1 + (1 | group1/spider1), data = webdata, family = tweedie())
# Test for significance using Type III ANOVA
Anova(LDHDp, type = "III")
##Overall web density of Exp. 1
# Fit a GLMM (Generalized Linear Mixed Model) with TWEEDIE family
LDHDo <- glmmTMB(overall1 ~ fdensity1 + (1 | group1/spider1), data = webdata, family = tweedie())
# Test for significance using Type III ANOVA
Anova(LDHDo, type = "III")

###test assumptions for Exp. 1 models Residual checks with DHARMa package
#Prey
obj <- simulateResiduals(LDHDp, plot = F)
plot(obj, quantreg = F)
#safety
obj <- simulateResiduals(LDHDs, plot = F)
plot(obj, quantreg = F)
# overall web density
obj <- simulateResiduals(LDHDo, plot = F)
plot(obj, quantreg = F)

### obtain means for each group
aggregate(safety1 ~ fdensity1, data = webdata, FUN = mean) # safety
aggregate(prey1 ~ fdensity1, data = webdata, FUN = mean) # prey
aggregate(overall1 ~ fdensity1, data = webdata, FUN = mean) #overall web density


### Exp. 2 HD first then LD -----
#see annotation above
webdata$fdensity2<-factor(webdata$fdensity2, levels = c("HD", "LD"))
summary(webdata$fdensity2)
webdata$spider2<-as.character(c(seq(1,45),seq(1,45)))

##Safety 2 
HDLDs <- glmmTMB(safety2 ~ fdensity2 + (1 | group2/spider2), data = webdata, family = tweedie())
Anova(HDLDs, type = "III")
##prey 2
HDLDp <- glmmTMB(prey2 ~ fdensity2 + (1 | group2/spider2), data = webdata, family = tweedie())
Anova(HDLDp, type = "III")
##overall 2
HDLDo <- glmmTMB(overall2 ~ fdensity2 + (1 | group2/spider2), data = webdata, family = tweedie())
Anova(HDLDo, type = "III")

###test assumptions for Exp. 2 models: Residual checks with DHARMa package
#Prey
obj <- simulateResiduals(HDLDp, plot = F)
plot(obj, quantreg = F)
#safety
obj <- simulateResiduals(HDLDs, plot = F)
plot(obj, quantreg = F)
# overall web density
obj <- simulateResiduals(HDLDo, plot = F)
plot(obj, quantreg = F)

### obtain means for each group
aggregate(safety2 ~ fdensity2, data = webdata, FUN = mean) # safety
aggregate(prey2 ~ fdensity2, data = webdata, FUN = mean) # prey
aggregate(overall2 ~ fdensity2, data = webdata, FUN = mean) #overall web density

### Exp. 3 LD first then LDPher -----
#see annotation above
#LDPher = low web density with synthetic pheromone mimicking high web density
webdata3 <- webdata[complete.cases(webdata[,c("fdensity3", "safety3", "group3", "prey3", "overall3", "pher3")]),]
webdata3$fdensity3<-factor(webdata3$fdensity3, levels = c("LD", "LDPher"))
summary(webdata3$fdensity3)
webdata3$spider3<-as.character(c(seq(1,43),seq(1,43)))

##Safety 3
LDLDPhers<- glmmTMB(safety3 ~ fdensity3 + (1 | group3/spider3), data = webdata3, family = tweedie())
Anova(LDLDPhers, type = "III")

##prey 3 
LDLDPherp<- glmmTMB(prey3 ~ fdensity3 + (1 | group3/spider3), data = webdata3, family = tweedie())
Anova(LDLDPherp, type = "III")

##overall 3
LDLDPhero<- glmmTMB(overall3 ~ fdensity3 + (1 | group3/spider3), data = webdata3, family = tweedie())
Anova(LDLDPhero, type = "III")

###test assumptions for Exp. 3 models: Residual checks with DHARMa package
#safety
obj <- simulateResiduals(LDLDPhers, plot = F)
plot(obj, quantreg = F)
#prey
obj <- simulateResiduals(LDLDPherp, plot = F)
plot(obj, quantreg = F)
#overall
obj <- simulateResiduals(LDLDPhero, plot = F)
plot(obj, quantreg = F)

#obtain means for each group
aggregate(safety3 ~ fdensity3, data = webdata3, FUN = mean)
aggregate(prey3 ~ fdensity3, data = webdata3, FUN = mean)
aggregate(overall3 ~ fdensity3, data = webdata3, FUN = mean)


#Contact pheromone analysis
#see annotations above
#pher = contact pheromone titer per web
##Exp. 1 LD-> HD contact pheromone
LDHDph<- glmmTMB(pher1 ~ fdensity1 + (1 | group1/spider1), data = webdata, family = tweedie())
Anova(LDHDph, type = "III")
## Exp. 2 HD->LD contact pheromone
HDLDph<- glmmTMB(pher2 ~ fdensity2 + (1 | group2/spider2), data = webdata, family = tweedie())
Anova(HDLDph, type = "III")
##Exp. 3 LD-> LD+pheromone
LDLDPherph<- glmmTMB(pher3 ~ fdensity3 + (1 | group3/spider3), data = webdata3, family = tweedie())
Anova(LDLDPherph, type = "III")

# test assumptions
obj <- simulateResiduals(LDHDph, plot = F)
plot(obj, quantreg = F)
obj <- simulateResiduals(HDLDph, plot = F)
plot(obj, quantreg = F)
obj <- simulateResiduals(LDLDPherph, plot = F)
plot(obj, quantreg = F)

# call means for each group
aggregate(pher1 ~ fdensity1, data = webdata, FUN = mean)
aggregate(pher2 ~ fdensity2, data = webdata, FUN = mean)
aggregate(pher3 ~ fdensity3, data = webdata3, FUN = mean)

###Exp. 4--- Mate calling investment expressed as the ratio of breakdown product (amide) and the sum of the breakdown product and the contact pheromone titer
#see annotations above
#bratio = breakdown ratio 
#exposure = social environment
call <- as.data.frame(read_excel("web data.xlsx", sheet = 2))
# create density exposures as factor (2 Levels)
call$exposure<-factor(call$exposure, levels = c("LD", "LDPher"))
attach(call)
mcall<- glmmTMB(bratio ~ exposure + (1 | group), data = call, family = tweedie())
Anova(mcall, type = "III")
obj <- simulateResiduals(mcall, plot = F)
plot(obj, quantreg = F)
aggregate(bratio ~ exposure, data = call, FUN = mean)