---
title: "Musicianship"
author: "Enming Zhang"
date: "2024-06-10"
output: html_document
---

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

```{r}
# Load necessary libraries
library(tidyr)
library(dplyr)
library(lme4)
```
```{r}


# Load the data
data <- read.csv("Musicianship.csv", header = TRUE)

# Display the first few rows and column names to verify structure
head(data)
colnames(data)

# Ensure correct column names
colnames(data)[1:2] <- c('Participant', 'Musician')

# Reshape the data from wide to long format
long_data <- data %>%
  pivot_longer(cols = -c(Participant, Musician), names_to = "Question", values_to = "Response")

# Ensure the data types are correct
long_data$Response <- as.numeric(long_data$Response)
long_data$Musician <- as.factor(long_data$Musician)
long_data$Participant <- as.factor(long_data$Participant)
long_data$Question <- as.factor(long_data$Question)

# Inspect the Response values
summary(long_data$Response)

# Filter out any invalid Response values
long_data <- long_data %>%
  filter(Response %in% c(0, 1))

# Fit the generalized linear mixed-effects model
model_glmer <- glmer(Response ~ Musician + (1 | Participant) + (1 | Question), 
                     data = long_data, family = binomial)

# Display the summary of the model
summary(model_glmer)

```

