---
title: "LMM_all"
output: html_document
date: "2024-02-17"
---

```{r setup, include=FALSE}

# loading packages
install.packages("Matrix")

library(openxlsx)

library(performance)
library(lme4)
library(lmerTest)
library(tidyverse)
library(datawizard)
library(Matrix)

# loading the datasheet
data <- read.xlsx("C:\\Users\\......")


# scale the numeric variable
data$trial_ <- standardize(data$trial)

print(str(data))

```

## R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

```{r cars}

# extract the whole subset and build the model_whole_knowledge

data.whole = subset(data, AOI == "whole")

model_whole_knowledge <- lmer(TFD ~ trial_  + species + demonstrator + knowledge*stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.whole, REML= TRUE)

# print out the results
print(drop1(model_whole_knowledge, test = "Chisq"))
print(summary(model_whole_knowledge))

print(check_model(model_whole_knowledge))



# extract the dipping stimuli subset and build the model

data.dipping_stimuli = subset(data.whole, stimuli == "dipping")

model_whole_dipping_stimuli <- lmer(TFD ~ trial_ + knowledge + species + demonstrator + firstshow + (1 + trial_ |ID), data=data.dipping_stimuli, REML=TRUE)

# print out the results
print(drop1(model_whole_dipping_stimuli, test = "Chisq"))
print(summary(model_whole_dipping_stimuli))

print(check_model(model_whole_dipping_stimuli))


# extract the sucking stimuli subset and build the model

data.sucking_stimuli = subset(data.whole, stimuli == "sucking")

model_whole_sucking_stimuli <- lmer(TFD ~ trial_ + knowledge + species + demonstrator + firstshow + (1 + trial_ |ID), data=data.sucking_stimuli, REML=TRUE)

# print out the results
print(drop1(model_whole_sucking_stimuli, test = "Chisq"))
print(summary(model_whole_sucking_stimuli))

print(check_model(model_whole_sucking_stimuli))

```

```{r cars}


# extract the actionw subset and build the model_actionw_knowledge

data.actionw = subset(data, AOIrate == "actionw")

model_actionw_knowledge <- lmer(TFDrate ~ trial_  + species + demonstrator + knowledge*stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.actionw, REML=TRUE)

# print out the results
print(drop1(model_actionw_knowledge, test = "Chisq"))
print(summary(model_actionw_knowledge))

print(check_model(model_actionw_knowledge))


```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE}

# extract the facew subset and build the model_facew_knowledge

data.facew = subset(data, AOIrate == "facew")

model_facew_knowledge <- lmer(TFDrate ~ trial_  + species + demonstrator + knowledge*stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.facew, REML=TRUE)

# print out the results
print(drop1(model_facew_knowledge, test = "Chisq"))
print(summary(model_facew_knowledge))

print(check_model(model_facew_knowledge))

```

Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

```{r}

# extract the foodw subset and build the model_foodw_knowledge

data.foodw = subset(data, AOIrate == "foodw")

model_foodw_knowledge <- lmer(TFDrate ~ trial_  + species + demonstrator + knowledge*stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.foodw, REML=TRUE)

# print out the results
print(drop1(model_foodw_knowledge, test = "Chisq"))
print(summary(model_foodw_knowledge))

print(check_model(model_foodw_knowledge))

```

```{r}

# extract the whole subset and build the model_whole_behavior

data.whole = subset(data, AOI == "whole")

model_whole_behavior <- lmer(TFD ~ trial_  + species + demonstrator + behavior + stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.whole, REML=TRUE)

# print out the results
print(drop1(model_whole_behavior, test = "Chisq"))
print(summary(model_whole_behavior))

print(check_model(model_whole_behavior))

```

```{r}

# extract the actionw subset and build the model_actionw_behavior

data.actionw = subset(data, AOIrate == "actionw")

model_actionw_behavior <- lmer(TFDrate ~ trial_  + species + demonstrator + behavior + stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.actionw, REML=TRUE)

# print out the results
print(drop1(model_actionw_behavior, test = "Chisq"))
print(summary(model_actionw_behavior))

print(check_model(model_actionw_behavior))

```

```{r}

# extract the facew subset and build the model_facew_behavior

data.facew = subset(data, AOIrate == "facew")

model_facew_behavior <- lmer(TFDrate ~ trial_  + species + demonstrator + behavior + stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.facew, REML=TRUE)

# print out the results
print(drop1(model_facew_behavior, test = "Chisq"))
print(summary(model_facew_behavior))

print(check_model(model_facew_behavior))


```

```{r}

# extract the foodw subset and build the model_foodw_behavior

data.foodw = subset(data, AOIrate == "foodw")

model_foodw_behavior <- lmer(TFDrate ~ trial_  + species + demonstrator + behavior + stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.foodw, REML=TRUE)

# print out the results
print(drop1(model_foodw_behavior, test = "Chisq"))
print(summary(model_foodw_behavior))

print(check_model(model_foodw_behavior))


```

```{r}

# extract the eyew subset and build the model_eyew_knowledge

data.eyew = subset(data, AOIrate == "eyew")

model_eyew_knowledge <- lmer(TFDrate ~ trial_  + species + demonstrator + knowledge*stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.eyew, REML=TRUE)

# print out the results
print(drop1(model_eyew_knowledge, test = "Chisq"))
print(summary(model_eyew_knowledge))

print(check_model(model_eyew_knowledge))


```
``

```{r}


# extract the mouthw subset and build the model_mouthw_knowledge

data.mouthw = subset(data, AOIrate == "mouthw")

model_mouthw_knowledge <- lmer(TFDrate ~ trial_  + species + demonstrator + knowledge*stimuli + firstshow + (1 + trial_ + stimuli|ID), data=data.mouthw, REML=TRUE)

# print out the results
print(drop1(model_mouthw_knowledge, test = "Chisq"))
print(summary(model_mouthw_knowledge))

print(check_model(model_mouthw_knowledge))


```
