---
title: "A high-throughput and sensitive method for food preference assays in the Argentine
  ant"
author: "Thomas Wagner, Henrique Galante and Tomer J. Czaczkes"
date: "2024-11-22"
output:
  pdf_document: default
  html_document: default
---

# Libraries
```{r, warning = FALSE}
suppressPackageStartupMessages({ 
require(readxl) 
require(ggplot2)
require(car)
require(glmmTMB)
require(DHARMa)
require(lme4)
require(MASS)
require(pwr)
library(rvg)
library(Hmisc)
library (ggplot2)
library (lme4)
library (nlme) 
library (multcomp)
library (readxl)
library (grid)
library (gridExtra)
library (dplyr)
library (scales)
library (lattice)
library (MASS)
library(rvg)
library(Hmisc)
library(simr)
library(betareg)  
library(lmtest)
})
```


# Import data

```{r,  warning = FALSE}

dualfeeder <- read_excel("C:/Users/LocalAdmin/Documents/ThomasWagnerPhD/PhD/MyPublication/PhD/DeltaFeederPaper/Pest Management/New/Power/DF_D2_Sucrose_Quinine_ControlsSide.xlsx")



```

# Lateralisation test

## Data management

First we will only include experiments in which ants did quinine vs sucurose.
Then we will throw out all the ants which hit the control solution first.
Then we will make an indicator variable telling us if quinine is on the right (1) or the left (0).

```{r}


dualfeeder$time_initial_choice_s = ifelse(dualfeeder$Initial_Decision == "L", dualfeeder$Drinking_Time_Left_s, dualfeeder$Drinking_Time_Right_s)

dualfeeder$prop_time_initial_choice = dualfeeder$time_initial_choice_s / (dualfeeder$Drinking_Time_Left_s + dualfeeder$Drinking_Time_Right_s)



dualfeeder$prop_time_initial_choice = ifelse(dualfeeder$prop_time_initial_choice == 1, NA, dualfeeder$prop_time_initial_choice)

dualfeeder$solution_initial_choice = ifelse(dualfeeder$Initial_Decision == "L", dualfeeder$Solution_Left, dualfeeder$Solution_Right)
dualfeeder$solution_initial_choice = ifelse((dualfeeder$Solution_Left == "1M Sucrose") & (dualfeeder$Solution_Right == "1M Sucrose"), "1M_L", dualfeeder$solution_initial_choice)
dualfeeder$solution_initial_choice = ifelse((dualfeeder$Solution_Left == "1M Sucrose") & (dualfeeder$Solution_Right == "1M Sucrose") & (dualfeeder$Initial_Decision == "R"), "1M_R", dualfeeder$solution_initial_choice)

table(dualfeeder$Initial_Decision)

onlyvsquinine <- subset(dualfeeder, Experiment == "1M_Quinine")

dataforlateralisation <- subset (onlyvsquinine, solution_initial_choice != "0.5M Sucrose")
dataforlateralisation <- subset (onlyvsquinine, solution_initial_choice != "1M Sucrose")

dataforlateralisation <- dataforlateralisation %>%
  mutate(Quinine_on_right = as.integer(Solution_Left == "1M Sucrose"))


```

## Exploratory figure

```{r}
ggplot(dataforlateralisation, aes(x = as.factor(Quininepair), y = prop_time_initial_choice, fill = factor(Quinine_on_right))) +
  geom_bar(stat = "summary", fun = "mean", position = position_dodge()) +
  labs(x = "Quinine Pair", y = "Mean Prop Time Initial Choice", fill = "Quinine on Right") +
  scale_fill_discrete(labels = c("No", "Yes")) +
  theme_minimal()
```

No obvious difference. 


## Statistical test

We are using a Beta regression to test whether quinine level and (importantly) quinine side predicts our outcome.
The ReducedModel represents the model without our tested potential predictor variable, "Quinine_on_right". To evaluate its significance, we compare this model to the complete model, lateralModel, which includes "Quinine_on_right". This comparison allows us to test whether the inclusion of "Quinine_on_right" leads to a significant improvement in model fit, potentially indicating a lateralisation effect.

```{r}
lateralModel <- betareg(prop_time_initial_choice ~ as.factor(Quininepair) + Quinine_on_right,
                        data = dataforlateralisation)

summary(lateralModel)

reducedModel <- betareg(prop_time_initial_choice ~ as.factor(Quininepair),
                        data = dataforlateralisation)





lrtest(lateralModel, reducedModel)


```

There is no evidence to suggest that lateralisation occurred, as the results were not statistically significant.