---
title: "ESM4_Robert_et_al2023_FirstExperimentAnalysis"
author: "Théo Robert, Vivek Nityananda"
date: "2023-09-22"
output: html_document
---

```{r setup, include= FALSE}


rm(list=ls())
library('DHARMa')
library('glmmTMB')

#Loading data
df=read.csv("C:\\Users\\nvn6\\OneDrive - Newcastle University\\Documents\\LearningAttentionPaper\\Revision\\beeSearchDataRevised.csv")
propFrames=df$frames/df$visualSearchFrames
df=cbind(df,propFrames)
df$propFrames=df$propFrames/df$regionArea
logProp=log(df$propFrames)
df=cbind(df,logProp)


#Specifying factors
df$regionType=factor(df$regionType, levels=c("reward","distractor", "other"))
df$flowerColour=factor(df$flowerColour, levels=c("Yellow", "Blue"))
df$trainingStage=factor(df$trainingStage, levels=c("First Six","Last Six"))

```

```{r analysis, include= TRUE}
#Overall model testing for the effects of location, colour and learning stage - with yellow flowers and the first six choices as the intercept
m_all=glm(logProp~trainingStage*flowerColour*regionType, data=subset(df, experiment == "First"))
SimOutputMTot_NullFirst=simulateResiduals(fittedModel=m_all, plot = T)
testDispersion(SimOutputMTot_NullFirst)
summary(m_all)

#Reordering and running the model again - with blue flowers and the first six choices as the intercept
df$flowerColour=factor(df$flowerColour, levels=c("Blue","Yellow"))
df$trainingStage=factor(df$trainingStage, levels=c("First Six","Last Six"))
m_all=glm(logProp~trainingStage*flowerColour*regionType, data=subset(df, experiment == "First"))
summary(m_all)

#Reordering and running the model again - with yellow flowers and the last six choices as the intercept
df$flowerColour=factor(df$flowerColour, levels=c("Yellow", "Blue"))
df$trainingStage=factor(df$trainingStage, levels=c("Last Six","First Six"))
m_all=glm(logProp~trainingStage*flowerColour*regionType, data=subset(df, experiment == "First"))
summary(m_all)

#Reordering and running the model again - with blue flowers and the last six choices as the intercept
df$flowerColour=factor(df$flowerColour, levels=c("Blue","Yellow"))
df$trainingStage=factor(df$trainingStage, levels=c("Last Six","First Six"))
m_all=glm(logProp~trainingStage*flowerColour*regionType, data=subset(df, experiment == "First"))
summary(m_all)

```
