Display data:

```{r}
options (width=200)
my_data <- read.csv("anonymizedData.tsv", sep = "\t")
my_data
```

Run the model:

```{r}
library(lme4)
contrast <- cbind (c(-1/2, +1/2))

colnames (contrast) <- c("-English+Mandarin")
my_data$language.group <- as.factor (my_data$language.group)
contrasts (my_data$language.group) <- contrast
modelAccuracy <- glmer (correct ~ language.group + (1 | participant), data = my_data, family = "binomial")
summary (modelAccuracy)
confint (modelAccuracy)
```

Calculate separate probabilities for each language group:

```{r}
#Mandarin
log_odds_mandarin <- 3.2643 + 0.5 * 0.6560
prob_mandarin <- exp(log_odds_mandarin) / (1 + exp(log_odds_mandarin))
prob_mandarin

#English
log_odds_english <- 3.2643 - 0.5 * 0.6560
prob_english <- exp(log_odds_english) / (1 + exp(log_odds_english))
prob_english
```

Calculate the odds ratio of the language effect:

```{r}
log_odds <- c(-0.5219077, 1.970285)
odds_ratios <- exp(log_odds)
print(odds_ratios)
```

Plot for quantitative analysis
```{r}
# Load required library
library(ggplot2)

# Create a data frame with the given data
data <- data.frame(
  Language = c("English", "Mandarin"),
  Average = c(prob_english, prob_mandarin)
)

# Create the bar plot
ggplot(data, aes(x = Language, y = Average, fill = Language)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = c("skyblue", "orange")) +
  labs(title = "Average Language Group Scores",
       x = "Language",
       y = "Average Score") +
  theme_minimal()

```

Plot for exploratory observation
```{r}

library(ggplot2)

data <- data.frame(
  participant = paste0("p", 1:25),
  language_group = c(rep("English", 12), rep("Mandarin", 13)),
  correct = c(27, 27, 25, 23, 22, 23, 24, 25, 25, 27, 27, 26, 27, 27, 27, 27, 26, 25, 26, 26, 25, 18, 27, 27, 26),
  incorrect = c(0, 0, 2, 4, 5, 4, 3, 2, 2, 0, 0, 1, 0, 0, 0, 0, 1, 2, 1, 1, 2, 9, 0, 0, 1)
)
english_data <- subset(data, language_group == "English")
mandarin_data <- subset(data, language_group == "Mandarin")

english_data$participant <- factor(english_data$participant, levels = unique(english_data$participant))
mandarin_data$participant <- factor(mandarin_data$participant, levels = unique(mandarin_data$participant))

ggplot(english_data, aes(x = participant, y = correct, fill = "Correct")) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_bar(aes(y = -incorrect, fill = "Incorrect"), stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("Correct" = "darkolivegreen3", "Incorrect" = "pink2")) +
  labs(title = "English",
       x = "Participant",
       y = "Count") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  theme(legend.position = "bottom")

ggplot(mandarin_data, aes(x = participant, y = correct, fill = "Correct")) +
  geom_bar(stat = "identity", position = "dodge") +
  geom_bar(aes(y = -incorrect, fill = "Incorrect"), stat = "identity", position = "dodge") +
  scale_fill_manual(values = c("Correct" = "darkolivegreen3", "Incorrect" = "pink2")) +
  labs(title = "Mandarin",
       x = "Participant",
       y = "Count") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  theme(legend.position = "bottom")

```