---
title: "Thesis Statistics_3"
author: "Yundi Xu"
date: "2025-06-06"
output: html_document
---
Read Tables
```{r}
library(tidyverse)
library(lmerTest)
library(lme4)
library(dplyr)

data_intensity_sentence_all <- read.table("data/intensity sentence.txt", header = TRUE, stringsAsFactors = TRUE)
data_intensity_sentence_all

data_intensity_segment_all <- read.table("data/intensity segment.txt", header = TRUE, stringsAsFactors = TRUE)
data_intensity_segment_all <- subset(data_intensity_segment_all, !data_intensity_segment_all$segments%in% (c("SP","AP")))
data_intensity_segment_all

data_phoneme_all <- read.table("data/Phoneme.txt", header = TRUE, stringsAsFactors = TRUE)
data_phoneme_all$jitter <- as.numeric(as.character(data_phoneme_all$jitter))
data_phoneme_all$shimmer <- as.numeric(as.character(data_phoneme_all$shimmer))
data_phoneme_all

```

Set Contrasts
```{r}
CODE_VOCAL <- +0.5
CODE_ORIGINAL <- -0.5
CODE_CLONE <- 0
CODE_CLONE1 <- -0.5
CODE_CLONE2 <- +0.5

CODE_AVERAGE_VOCAL <- -1/3
CODE_AVERAGE_CLONE <- +2/3
CODE_AVERAGE_ORIGINAL <- -1/3

contrasts <- cbind(
                  c(CODE_CLONE, CODE_ORIGINAL, CODE_VOCAL),
                  c(CODE_AVERAGE_CLONE,CODE_AVERAGE_ORIGINAL,CODE_AVERAGE_VOCAL)
                  )

colnames(contrasts) <- cbind(c("-O+V"),
                             c("-VO+C"))

contrast1 <- cbind(c(CODE_CLONE1, CODE_VOCAL))
colnames(contrast1) <- c("-C+V")
contrast2 <- cbind(c(CODE_CLONE2, CODE_ORIGINAL))
colnames(contrast2) <- c("-O+C")

```

Intensity and Spectral Tilts (ternary) at the Segment Level
```{r}
contrasts(data_intensity_segment_all$source) <- contrasts
contrasts(data_intensity_segment_all$source)

model_intensity_segment <- lmerTest::lmer(medium_difference.dB. ~ source +  (source| song) + (1 | id), data = data_intensity_segment_all, REML = TRUE)
fixedEffects_is <- lme4::fixef(model_intensity_segment)
cbind(names(fixedEffects_is),
      lmerTest::contest(model_intensity_segment,
                        diag(length(fixedEffects_is)),joint = FALSE))
summary(model_intensity_segment)

model_spectralTilt_segment <- lmerTest::lmer(spectralTilt ~ source + (source| song) + (1| id), data = data_intensity_segment_all, REML = TRUE)
fixedEffects_ss <- lme4::fixef(model_spectralTilt_segment)
cbind(names(fixedEffects_ss),
      lmerTest::contest(model_spectralTilt_segment,
                        diag(length(fixedEffects_ss)),joint = FALSE))
summary(model_spectralTilt_segment)

```

Intensity and spectral tilt (between vocal and cloned versions) at the Segment Level
```{r}

data_intensity_segment_all1 <- data_intensity_segment_all %>%
  filter(source %in% c("V", "C"))
data_intensity_segment_all1$source <- droplevels(data_intensity_segment_all1$source)

contrasts(data_intensity_segment_all1$source) <- contrast1
contrasts(data_intensity_segment_all1$source)

model_intensity_segment1 <- lmerTest::lmer(medium_difference.dB. ~ source +  (source| song) + (1 | id), data = data_intensity_segment_all1, REML = TRUE)
fixedEffects_is1 <- lme4::fixef(model_intensity_segment1)
cbind(names(fixedEffects_is1),
      lmerTest::contest(model_intensity_segment1,
                        diag(length(fixedEffects_is1)),joint = FALSE))
summary(model_intensity_segment1)

model_spectralTilt_segment1 <- lmerTest::lmer(spectralTilt ~ source + (source| song) + (1| id), data = data_intensity_segment_all1, REML = TRUE)
fixedEffects_ss1 <- lme4::fixef(model_spectralTilt_segment1)
cbind(names(fixedEffects_ss1),
      lmerTest::contest(model_spectralTilt_segment1,
                        diag(length(fixedEffects_ss1)),joint = FALSE))
summary(model_spectralTilt_segment1)

```

Intensity and spectral tilt (between original and cloned versions)  at the Segment Level
```{r}

data_intensity_segment_all2 <- data_intensity_segment_all %>%
  filter(source %in% c("C", "O"))
data_intensity_segment_all2$source <- droplevels(data_intensity_segment_all2$source)

contrasts(data_intensity_segment_all2$source) <- contrast2
contrasts(data_intensity_segment_all2$source)

model_intensity_segment2 <- lmerTest::lmer(medium_difference.dB. ~ source +  (source| song) + (1 | id) , data = data_intensity_segment_all2, REML = TRUE)
fixedEffects_is2 <- lme4::fixef(model_intensity_segment2)
cbind(names(fixedEffects_is2),
      lmerTest::contest(model_intensity_segment2,
                        diag(length(fixedEffects_is2)),joint = FALSE))
summary(model_intensity_segment2)

model_spectralTilt_segment2 <- lmerTest::lmer(spectralTilt ~ source + (source| song) + (1| id), data = data_intensity_segment_all2, REML = TRUE)
fixedEffects_ss2 <- lme4::fixef(model_spectralTilt_segment2)
cbind(names(fixedEffects_ss2),
      lmerTest::contest(model_spectralTilt_segment2,
                        diag(length(fixedEffects_ss2)),joint = FALSE))
summary(model_spectralTilt_segment2)

```
Intensity and Spectral Tilts (Ternary) at the Sentence Level
```{r}
contrasts(data_intensity_sentence_all$source) <- contrasts
contrasts(data_intensity_sentence_all$source)

model_intensity_sentence <- lmerTest::lmer(range_of_intensity.dB. ~ source +  (source| song) + (1 | id), data = data_intensity_sentence_all, REML = TRUE)
fixedEffects_i_s <- lme4::fixef(model_intensity_sentence)
cbind(names(fixedEffects_i_s),
      lmerTest::contest(model_intensity_sentence,
                        diag(length(fixedEffects_i_s)),joint = FALSE))
summary(model_intensity_sentence)

model_spectralTilt_sentence <- lmerTest::lmer(spectralTilt ~ source + (source| song) + (1| id), data = data_intensity_sentence_all, REML = TRUE)
fixedEffects_s_s <- lme4::fixef(model_spectralTilt_sentence)
cbind(names(fixedEffects_s_s),
      lmerTest::contest(model_spectralTilt_sentence,
                        diag(length(fixedEffects_s_s)),joint = FALSE))
summary(model_spectralTilt_sentence)

```

Intensity and spectral tilt (between cover and cloned versions)  at the Sentence Level
```{r}

data_intensity_sentence_all1 <- data_intensity_sentence_all %>%
  filter(source %in% c("C", "V"))
data_intensity_sentence_all1$source <- droplevels(data_intensity_sentence_all1$source)

contrasts(data_intensity_sentence_all1$source) <- contrast1
contrasts(data_intensity_sentence_all1$source)

model_intensity_sentence1 <- lmerTest::lmer(range_of_intensity.dB. ~ source +  (source| song) + (1 | id), data = data_intensity_sentence_all1, REML = TRUE)
fixedEffects_i_s1 <- lme4::fixef(model_intensity_sentence1)
cbind(names(fixedEffects_i_s1),
      lmerTest::contest(model_intensity_sentence1,
                        diag(length(fixedEffects_i_s1)),joint = FALSE))
summary(model_intensity_sentence1)

model_spectralTilt_sentence1 <- lmerTest::lmer(spectralTilt ~ source + (source| song) + (1| id), data = data_intensity_sentence_all1, REML = TRUE)
fixedEffects_s_s1 <- lme4::fixef(model_spectralTilt_sentence1)
cbind(names(fixedEffects_s_s1),
      lmerTest::contest(model_spectralTilt_sentence1,
                        diag(length(fixedEffects_s_s1)),joint = FALSE))
summary(model_spectralTilt_sentence1)

```

Intensity and spectral tilt (between original and cloned versions)  at the Sentence Level
```{r}

data_intensity_sentence_all2 <- data_intensity_sentence_all %>%
  filter(source %in% c("C", "O"))
data_intensity_sentence_all2$source <- droplevels(data_intensity_sentence_all2$source)

contrasts(data_intensity_sentence_all2$source) <- contrast2
contrasts(data_intensity_sentence_all2$source)

model_intensity_sentence2 <- lmerTest::lmer(range_of_intensity.dB. ~ source +  (source| song) + (1 | id), data = data_intensity_sentence_all2, REML = TRUE)
fixedEffects_i_s2 <- lme4::fixef(model_intensity_sentence2)
cbind(names(fixedEffects_i_s2),
      lmerTest::contest(model_intensity_sentence2,
                        diag(length(fixedEffects_i_s2)),joint = FALSE))
summary(model_intensity_sentence2)

model_spectralTilt_sentence2 <- lmerTest::lmer(spectralTilt ~ source + (source| song) + (1| id), data = data_intensity_sentence_all2, REML = TRUE)
fixedEffects_s_s2 <- lme4::fixef(model_spectralTilt_sentence2)
cbind(names(fixedEffects_s_s2),
      lmerTest::contest(model_spectralTilt_sentence2,
                        diag(length(fixedEffects_s_s2)),joint = FALSE))
summary(model_spectralTilt_sentence2)

```


jitter, shimmer, cpp, lmer (ternary)
```{r}

contrasts(data_phoneme_all$source) <- contrasts
contrasts(data_phoneme_all$source)

model_jitter <- lmerTest::lmer(jitter ~  source + (source| song) + (1| id), data = data_phoneme_all, REML = TRUE)
fixedEffects_jitter <- lme4::fixef(model_jitter)
cbind(names(fixedEffects_jitter),
      lmerTest::contest(model_jitter,
                        diag(length(fixedEffects_jitter)),joint = FALSE))
summary(model_jitter)

model_shimmer <- lmerTest::lmer(shimmer ~ source + (source| song) + (1| id), data = data_phoneme_all, REML = TRUE)
fixedEffects_shimmer <- lme4::fixef(model_shimmer)
cbind(names(fixedEffects_shimmer),
      lmerTest::contest(model_shimmer,
                        diag(length(fixedEffects_shimmer)),joint = FALSE))
summary(model_shimmer)

model_cpp <- lmerTest::lmer(CPP.dB. ~ source + (source| song) + (1| id), data = data_phoneme_all, REML = TRUE)
fixedEffects_cpp <- lme4::fixef(model_cpp)
cbind(names(fixedEffects_cpp),
      lmerTest::contest(model_cpp,
                        diag(length(fixedEffects_cpp)),joint = FALSE))
summary(model_cpp)

model_hnr <- lmerTest::lmer(hnr.dB.  ~ source + (source| song) + (1| id), data = data_phoneme_all, REML = TRUE)
fixedEffects_hnr <- lme4::fixef(model_hnr)
cbind(names(fixedEffects_hnr),
      lmerTest::contest(model_hnr,
                        diag(length(fixedEffects_hnr)),joint = FALSE))
summary(model_hnr)
```





jitter, shimmer, cpp, lmer (between vocal and cloned versions)
```{r}
data_phoneme_all_filtered1 <- data_phoneme_all %>%
  filter(source %in% c("V", "C"))
data_phoneme_all_filtered1$source <- droplevels(data_phoneme_all_filtered1$source)

contrasts(data_phoneme_all_filtered1$source) <- contrast1
contrasts(data_phoneme_all_filtered1$source)

model_jitter1 <- lmerTest::lmer(jitter ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered1, REML = TRUE)
fixedEffects_jitter1 <- lme4::fixef(model_jitter1)
cbind(names(fixedEffects_jitter1),
      lmerTest::contest(model_jitter1,
                        diag(length(fixedEffects_jitter1)),joint = FALSE))
summary(model_jitter1)

model_shimmer1 <- lmerTest::lmer(shimmer ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered1, REML = TRUE)
fixedEffects_shimmer1 <- lme4::fixef(model_shimmer1)
cbind(names(fixedEffects_shimmer1),
      lmerTest::contest(model_shimmer1,
                        diag(length(fixedEffects_shimmer1)),joint = FALSE))
summary(model_shimmer1)

model_cpp1 <- lmerTest::lmer(CPP.dB. ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered1, REML = TRUE)
fixedEffects_cpp1 <- lme4::fixef(model_cpp1)
cbind(names(fixedEffects_cpp1),
      lmerTest::contest(model_cpp1,
                        diag(length(fixedEffects_cpp1)),joint = FALSE))
summary(model_cpp1)

model_hnr1 <- lmerTest::lmer(hnr.dB.  ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered1, REML = TRUE)
fixedEffects_hnr1 <- lme4::fixef(model_hnr1)
cbind(names(fixedEffects_hnr1),
      lmerTest::contest(model_hnr1,
                        diag(length(fixedEffects_hnr1)),joint = FALSE))
summary(model_hnr1)
```

jitter, shimmer, cpp, lmer (between original and cloned versions)
```{r}
data_phoneme_all_filtered2 <- data_phoneme_all %>%
  filter(source %in% c("O", "C"))
data_phoneme_all_filtered2$source <- droplevels(data_phoneme_all_filtered2$source)

contrasts(data_phoneme_all_filtered2$source) <- contrast2
contrasts(data_phoneme_all_filtered2$source)

model_jitter2 <- lmerTest::lmer(jitter ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered2, REML = TRUE)
fixedEffects_jitter2 <- lme4::fixef(model_jitter2)
cbind(names(fixedEffects_jitter2),
      lmerTest::contest(model_jitter2,
                        diag(length(fixedEffects_jitter2)),joint = FALSE))
summary(model_jitter2)

model_shimmer2 <- lmerTest::lmer(shimmer ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered2, REML = TRUE)
fixedEffects_shimmer2 <- lme4::fixef(model_shimmer2)
cbind(names(fixedEffects_shimmer2),
      lmerTest::contest(model_shimmer2,
                        diag(length(fixedEffects_shimmer2)),joint = FALSE))
summary(model_shimmer2)

model_cpp2 <- lmerTest::lmer(CPP.dB. ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered2, REML = TRUE)
fixedEffects_cpp2 <- lme4::fixef(model_cpp2)
cbind(names(fixedEffects_cpp2),
      lmerTest::contest(model_cpp2,
                        diag(length(fixedEffects_cpp2)),joint = FALSE))
summary(model_cpp2)

model_hnr2 <- lmerTest::lmer(hnr.dB.  ~ source + (source| song) + (1| id), data = data_phoneme_all_filtered2, REML = TRUE)
fixedEffects_hnr2 <- lme4::fixef(model_hnr2)
cbind(names(fixedEffects_hnr2),
      lmerTest::contest(model_hnr2,
                        diag(length(fixedEffects_hnr2)),joint = FALSE))
summary(model_hnr2)
```
