---
title: "ESM3_Robert_et_al_DurationAnalysis"
author: "Théo Robert, Vivek Nityananda"
date: "2023-09-22"
output: html_document
---

```{r setup, include=FALSE}


#Robert et al, 2023
##########################################################################################
#look at total amount of time (totalframes) difference between first 6 and last 6 choices#
##########################################################################################


#clear working memory
rm(list=ls())


library("blme")
library("glmmTMB")#if it gives an error message, install the Matrix package separately to force the install of the most recent version. Then re-install TMB.
library("DHARMa")
library("lme4")


#loading data
df=read.csv('C:\\Users\\nvn6\\OneDrive - Newcastle University\\Documents\\LearningAttentionPaper\\Revision\\beeSearchDataRevised.csv')

names(df)

#Specifying factors
df$regionType=factor(df$regionType, levels=c("reward","distractor","other"))
df$flowerColour=factor(df$flowerColour, levels=c("Blue", "Yellow"))
df$trainingStage=factor(df$trainingStage, levels=c("First Six","Last Six"))

#keep only the rewarded data as the total number of frames is just repeated across other "flower types"
dfReward=subset(df,df$regionType=="reward")

hist(dfReward$totalFrames)


###############################################################################
#split between first and second task


#Split the dataset per task number
dfRewardFirst=subset(df,df$regionType=="reward" & df$experiment=="First")
dfRewardSecond=subset(df,df$regionType=="reward" & df$experiment=="Second")


hist(dfRewardFirst$totalFrames)
hist(dfRewardSecond$totalFrames)

```

```{r firstTaskAnalysis, include=TRUE}
######################
#analyse the first task

#null model
MTot_NullFirst=glmmTMB(totalFrames~1+(1|bee), data=dfRewardFirst, family=nbinom2())
hist(resid(MTot_NullFirst))
testZeroInflation(MTot_NullFirst)
SimOutputMTot_NullFirst=simulateResiduals(fittedModel = MTot_NullFirst, plot = T)
plot(SimOutputMTot_NullFirst,quantreg=T)
testDispersion(SimOutputMTot_NullFirst)


#effect of training stage
MTot_CondiFirst=glmmTMB(totalFrames~trainingStage+(1|bee), data=dfRewardFirst, family=nbinom2())
hist(resid(MTot_CondiFirst))
testZeroInflation(MTot_CondiFirst)
SimOutputMTot_CondiFirst=simulateResiduals(fittedModel = MTot_CondiFirst, plot = T)
plot(SimOutputMTot_CondiFirst,quantreg=T)
testDispersion(SimOutputMTot_CondiFirst)
plotResiduals(MTot_CondiFirst, dfRewardFirst$trainingStage)
plotResiduals(MTot_CondiFirst, dfRewardFirst$flowerColour)
anova(MTot_NullFirst,MTot_CondiFirst)
summary(MTot_CondiFirst)#***


#effect of flower colour
MTot_FlowerFirst=glmmTMB(totalFrames~flowerColour+(1|bee), data=dfRewardFirst, family=nbinom2())
hist(resid(MTot_FlowerFirst))
testZeroInflation(MTot_FlowerFirst)
SimOutputMTot_FlowerFirst=simulateResiduals(fittedModel = MTot_FlowerFirst, plot = T)
plot(SimOutputMTot_FlowerFirst,quantreg=T)
testDispersion(SimOutputMTot_FlowerFirst)
plotResiduals(MTot_FlowerFirst, dfRewardFirst$flowerColour)
anova(MTot_NullFirst,MTot_FlowerFirst)#NS



#trainingStage and flower colour
MTot_CondiAndFlowerFirst=glmmTMB(totalFrames~trainingStage+flowerColour+(1|bee), data=dfRewardFirst, family=nbinom2())
hist(resid(MTot_CondiAndFlowerFirst))
testZeroInflation(MTot_CondiAndFlowerFirst)
SimOutputMTot_CondiAndFlowerFirst=simulateResiduals(fittedModel = MTot_CondiAndFlowerFirst, plot = T)
plot(SimOutputMTot_CondiAndFlowerFirst,quantreg=T)
testDispersion(SimOutputMTot_CondiAndFlowerFirst)
plotResiduals(MTot_CondiAndFlowerFirst, dfRewardFirst$trainingStage)
plotResiduals(MTot_CondiAndFlowerFirst, dfRewardFirst$flowerColour)
anova(MTot_CondiAndFlowerFirst,MTot_CondiFirst)#NS



#Full model
MTot_FullFirst=glmmTMB(totalFrames~trainingStage*flowerColour+(1|bee), data=dfRewardFirst, family=nbinom2())
hist(resid(MTot_FullFirst))
testZeroInflation(MTot_FullFirst)
SimOutputMTot_FullFirst=simulateResiduals(fittedModel = MTot_FullFirst, plot = T)
plot(SimOutputMTot_FullFirst,quantreg=T)
testDispersion(SimOutputMTot_FullFirst)
plotResiduals(MTot_FullFirst, dfRewardFirst$trainingStage)
plotResiduals(MTot_FullFirst, dfRewardFirst$flowerColour)
anova(MTot_CondiAndFlowerFirst,MTot_FullFirst)
anova(MTot_CondiFirst,MTot_FullFirst) #NS
summary(MTot_FullFirst)

```


```{r secondTaskAnalysis, include=TRUE}
#null model
MTot_NullSecond=glmmTMB(totalFrames~1+(1|bee), data=dfRewardSecond, family=nbinom2())
hist(resid(MTot_NullSecond))
testZeroInflation(MTot_NullSecond)
SimOutputMTot_NullSecond=simulateResiduals(fittedModel = MTot_NullSecond, plot = T)
plot(SimOutputMTot_NullSecond,quantreg=T)
testDispersion(SimOutputMTot_NullSecond)


#effect of trainingStage
MTot_CondiSecond=glmmTMB(totalFrames~trainingStage+(1|bee), data=dfRewardSecond, family=nbinom2())
hist(resid(MTot_CondiSecond))
testZeroInflation(MTot_CondiSecond)
SimOutputMTot_CondiSecond=simulateResiduals(fittedModel = MTot_CondiSecond, plot = T)
plot(SimOutputMTot_CondiSecond,quantreg=T)
testDispersion(SimOutputMTot_CondiSecond)
plotResiduals(MTot_CondiSecond, dfRewardSecond$trainingStage)
plotResiduals(MTot_CondiSecond, dfRewardSecond$flowerColour)
anova(MTot_NullSecond,MTot_CondiSecond)#***


#effect of flower colour
MTot_FlowerSecond=glmmTMB(totalFrames~flowerColour+(1|bee), data=dfRewardSecond, family=nbinom2())
hist(resid(MTot_FlowerSecond))
testZeroInflation(MTot_FlowerSecond)
SimOutputMTot_FlowerSecond=simulateResiduals(fittedModel = MTot_FlowerSecond, plot = T)
plot(SimOutputMTot_FlowerSecond,quantreg=T)
testDispersion(SimOutputMTot_FlowerSecond)
plotResiduals(MTot_FlowerSecond, dfRewardSecond$flowerColour)
anova(MTot_NullSecond,MTot_FlowerSecond)#NS



#trainingStage and flower colour
MTot_CondiAndFlowerSecond=glmmTMB(totalFrames~trainingStage+flowerColour+(1|bee), data=dfRewardSecond, family=nbinom2())
hist(resid(MTot_CondiAndFlowerSecond))
testZeroInflation(MTot_CondiAndFlowerSecond)
SimOutputMTot_CondiAndFlowerSecond=simulateResiduals(fittedModel = MTot_CondiAndFlowerSecond, plot = T)
plot(SimOutputMTot_CondiAndFlowerSecond,quantreg=T)
testDispersion(SimOutputMTot_CondiAndFlowerSecond)
plotResiduals(MTot_CondiAndFlowerSecond, dfRewardSecond$trainingStage)
plotResiduals(MTot_CondiAndFlowerSecond, dfRewardSecond$flowerColour)
anova(MTot_CondiAndFlowerSecond,MTot_CondiSecond)#NS



#Full model
MTot_FullSecond=glmmTMB(totalFrames~trainingStage*flowerColour+(1|bee), data=dfRewardSecond, family=nbinom2())
hist(resid(MTot_FullSecond))
testZeroInflation(MTot_FullSecond)
SimOutputMTot_FullSecond=simulateResiduals(fittedModel = MTot_FullSecond, plot = T)
plot(SimOutputMTot_FullSecond,quantreg=T)
testDispersion(SimOutputMTot_FullSecond)
plotResiduals(MTot_FullSecond, dfRewardSecond$trainingStage)
plotResiduals(MTot_FullSecond, dfRewardSecond$flowerColour)
anova(MTot_CondiAndFlowerSecond,MTot_FullSecond)
anova(MTot_CondiSecond,MTot_FullSecond)
summary(MTot_FullSecond)

```

