---
title: "Cognitive  writing process characteristics in Alzheimer's disease"
output: html_document
---

```{r setup, include = FALSE}
options(scipen = 999) # number notation
```

```{r packages, error = FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(readr)
library(lmerTest)
library(lme4)
library(car)
library(multcomp)
library(emmeans)
library(performance)
library(see)          
```

# Overall process measures
## Load files
```{r}
Data_participant_level <- read_delim(
  "Participant_level.csv", 
  ";", 
  escape_double = FALSE, 
  trim_ws = TRUE
  )

Data_participant_level$group <- recode(
  Data_participant_level$group2,
  '"gezonde ouderen"="1"; "CI"="2"'
  )
```

## Time on task
```{r}
Data_participant_level$time_on_task_sz <- scale(
  Data_participant_level$time_on_task_s
  )

hist(Data_participant_level$time_on_task_sz)
```

```{r}
TimeOnTask_lmer_mod1 <- lmer(
  time_on_task_sz ~ 1 +
    task + 
    median_char_char + 
    (1|participant_code),
  data = Data_participant_level
  )

TimeOnTask_lmer_mod2 <- lmer(
  time_on_task_sz ~ 1 + 
    task + 
    median_char_char + 
    group + 
    (1|participant_code),
  data = Data_participant_level
  )

anova(TimeOnTask_lmer_mod1, TimeOnTask_lmer_mod2)
```

```{r}
summary(TimeOnTask_lmer_mod2)
```

## Number of characters per minute
```{r}
Data_participant_level$number_of_char_per_minuten <- log(
  Data_participant_level$number_of_char_per_minute
  )

hist(Data_participant_level$number_of_char_per_minute)
hist(Data_participant_level$number_of_char_per_minuten)
```

```{r}
NChar_lmer_mod1 <- lmer(
  number_of_char_per_minuten ~ 1 + 
    task + 
    median_char_char + 
    (1|participant_code),
  data = Data_participant_level
)

NChar_lmer_mod2 <- lmer(
  number_of_char_per_minuten ~ 1 + 
    task + 
    median_char_char + 
    group + 
    (1|participant_code),
  data = Data_participant_level
)

anova(NChar_lmer_mod1, NChar_lmer_mod2)
```

```{r}
summary(NChar_lmer_mod2)
```

# Pausing behavior
## Load files
```{r}
Data_participant_level <- read_delim(
  "Participant_level.csv", 
  ";", 
  escape_double = FALSE, 
  trim_ws = TRUE
  )

Data_participant_level$group <- recode(
  Data_participant_level$group2,
  '"gezonde ouderen"="1"; "CI"="2"'
  )
```

## Number of pauses per minute
```{r}
Data_participant_level$number_of_pauses_per_minute_PT30n <- log(
  Data_participant_level$number_of_pauses_per_minute_PT30
  )

hist(Data_participant_level$number_of_pauses_per_minute_PT30)
hist(Data_participant_level$number_of_pauses_per_minute_PT30n)
```

```{r}
NPauses_lmer_mod1 <- lmer(
  number_of_pauses_per_minute_PT30n ~ 1 + 
    task + 
    median_char_char + 
    (1|participant_code),
  data = Data_participant_level
  )

NPauses_lmer_mod2 <- lmer(
  number_of_pauses_per_minute_PT30n ~ 1 + 
    task + 
    median_char_char + 
    group + 
    (1|participant_code),
  data = Data_participant_level
  )

anova(NPauses_lmer_mod1, NPauses_lmer_mod2)
```

```{r}
summary(NPauses_lmer_mod2)
```

## Proportion of pause time
```{r}
Data_participant_level$proportion_of_pause_time_PT2000z <- scale(
  Data_participant_level$proportion_of_pause_time_PT2000
  )

hist(Data_participant_level$proportion_of_pause_time_PT2000z)
```

```{r}
PropPT_lmer_mod1 <- lmer(
  proportion_of_pause_time_PT2000z ~ 1 + 
    task + 
    median_char_char + 
    (1|participant_code),
  data = Data_participant_level
  )

PropPT_lmer_mod2 <- lmer(
  proportion_of_pause_time_PT2000z ~ 1 + 
    task + 
    median_char_char + 
    group + 
    (1|participant_code),
  data = Data_participant_level
  )

anova(PropPT_lmer_mod1, PropPT_lmer_mod2)
```

```{r}
summary(PropPT_lmer_mod2)
```

## Number of P-bursts per minute
```{r}
hist(Data_participant_level$number_of_P_bursts_per_minute_PB2000)
```

```{r}
NPbursts_lmer_mod1 <- lmer(
  number_of_P_bursts_per_minute_PB2000 ~ 1 + 
    task + 
    median_char_char + 
    (1|participant_code),
  data = Data_participant_level
  )

NPbursts_lmer_mod2 <- lmer(
  number_of_P_bursts_per_minute_PB2000 ~ 1 + 
    task + 
    median_char_char + 
    group + 
    (1|participant_code),
  data = Data_participant_level
  )

anova(NPbursts_lmer_mod1, NPbursts_lmer_mod2)
```

```{r}
summary(NPbursts_lmer_mod2)
```

## Duration of P-bursts
```{r}
Data_participant_level$duration_of_P_bursts_s_PB2000z <- scale(
  Data_participant_level$duration_of_P_bursts_s_PB2000
  )

hist(Data_participant_level$duration_of_P_bursts_s_PB2000z)
```

```{r}
DurPbursts_lmer_mod1 <- lmer(
  duration_of_P_bursts_s_PB2000z ~ 1 + 
    task + 
    median_char_char + 
    (1|participant_code),
  data = Data_participant_level
  )

DurPbursts_lmer_mod2 <- lmer(
  duration_of_P_bursts_s_PB2000z ~ 1 + 
    task + 
    median_char_char + 
    group + 
    (1|participant_code),
  data = Data_participant_level
  )

anova(DurPbursts_lmer_mod1, DurPbursts_lmer_mod2)
```

```{r}
summary(DurPbursts_lmer_mod2)
```

## Pause time between words
### Load files
```{r}
Data_word_level <- read_delim(
  "Linguistic_analysis_word_level.csv", 
  ";", 
  escape_double = FALSE, 
  trim_ws = TRUE
  )
```

### Data reduction
```{r}
# Rename residual categories of parts-of-speech to 'other' and remove
Data_word_level$PoSA_checked2 <- recode(
  Data_word_level$PoSA_checked, 
  '"-"="andere";"1"="andere";"LET"="andere";"SPEC"="andere";"TW"="andere"'
  )

Data_word_level <- Data_word_level[Data_word_level$PoSA_checked2!="andere", ]

# Delete NAs
Data_word_level <- na.omit(
  Data_word_level[c(
    "BetweenWords", 
    "Participant", 
    "Task", 
    "Median_char_char", 
    "Group3", 
    "PoSA_checked2")]
  )
```

### Data preparation
```{r}
# Normalize pause times
Data_word_level$BetweenWordsn <- log(
  Data_word_level$BetweenWords
  )

hist(Data_word_level$BetweenWords)
hist(Data_word_level$BetweenWordsn)

# Calculate z score
Data_word_level$BetweenWordsnz <- scale(
  Data_word_level$BetweenWordsn
  )

# Order parts-of-speech categories
Data_word_level$PoSA_checked2 <- factor(
  Data_word_level$PoSA_checked2, 
  levels = c(
    "LID", "BW", "ADJ", "N", "VNW", "VZ", "WW", "VG")
  )
```

### Model comparisons
```{r}
PTBetWords_lmer_mod0 <- lmer(
  BetweenWordsnz ~ 1 + 
    Task + 
    Median_char_char + 
    (1|Participant),
  data = Data_word_level,
  REML = F
  )

PTBetWords_lmer_mod1 <- lmer(
  BetweenWordsnz ~ 1 + 
    Task + 
    Median_char_char + 
    Group3 + 
    (1|Participant),
  data = Data_word_level,
  REML = F
  )

PTBetWords_lmer_mod2 <- lmer(
  BetweenWordsnz ~ 1 + 
    Task + 
    Median_char_char + 
    Group3 + 
    PoSA_checked2 + 
    (1|Participant),
  data = Data_word_level,
  REML = F
  )

PTBetWords_lmer_mod3 <- lmer(
  BetweenWordsnz ~ 1 + 
    Task + 
    Median_char_char + 
    Group3 + 
    PoSA_checked2 + 
    Group3*PoSA_checked2 + 
    (1|Participant),
  data = Data_word_level, 
  REML = F
  )

anova(PTBetWords_lmer_mod0, PTBetWords_lmer_mod1, PTBetWords_lmer_mod2, PTBetWords_lmer_mod3)
```

```{r}
summary(PTBetWords_lmer_mod2)

summary(
  glht(
    PTBetWords_lmer_mod2, 
    linfct=mcp(PoSA_checked2="Tukey"))
  )
```