KBB Visualizing the MMN - LeoLedesma237/LeoWebsite GitHub Wiki

Overview

The function of this script is to visualize every subject's MMN and to display an MMN of the grand average. This code is broken down into four parts.

  • Part 1: Loading ERP data and data cleaning
  • Part 2: Calculating the MMN for Cz channel
  • Part 3: Displaying the MMN for each subject
  • Part 4: Displaying the grand mean MMN

Part 1: Loading in deviant and standard ERP data

library(tidyverse)
library(ggplot2)

# Set working directory
setwd("Z:/Leo/KBB_EEG_Pilot/6_ERPLAB/Exported ERP")

# Obtain all deviant and standard files separately
Deviant.Tone.files <- list.files(pattern = "Deviant 1200Hz Tone")
Standard.Tone.files <- list.files(pattern = "Standard 1000Hz Tone")

# Save the names/IDs of each file
Names <- sub("_.*", "", Deviant.Tone.files)

# Read in the files separately
deviant.list <- list()
standard.list <- list()

for(ii in 1:length(Deviant.Tone.files)) {
  
  # load in files
  current.deviant <- read.csv(Deviant.Tone.files[ii], sep = "\t")
  current.standard <- read.csv(Standard.Tone.files[ii], sep = "\t")

  # Rename Time to Channel
  current.deviant <- rename(current.deviant, Channel = time)
  current.standard <- rename(current.standard, Channel = time)
  
  # Remove variable X
  current.deviant <- select(current.deviant, - X)
  current.standard <- select(current.standard, - X)
  
  # Save into the list
  deviant.list[ii](/LeoLedesma237/LeoWebsite/wiki/ii) <- current.deviant
  standard.list[ii](/LeoLedesma237/LeoWebsite/wiki/ii) <- current.standard
}

Part 2: Extracting Fz information + creating MMN

# Extract Fz data

data <- list()

for(ii in 1:length(deviant.list)) {
  
  # Extract the datasets from the list one by one
  current.deviant <- deviant.list[ii](/LeoLedesma237/LeoWebsite/wiki/ii)
  current.standard <- standard.list[ii](/LeoLedesma237/LeoWebsite/wiki/ii)
  
  current.deviant <- filter(current.deviant, Channel == "Fz") 
  current.standard <- filter(current.standard, Channel  == "Fz")
  
  # Remove the Channel variable
  current.deviant <- select(current.deviant, - Channel)
  current.standard <- select(current.standard, - Channel)
  
  # Obtain the colMeans
  current.deviant.CM <- colMeans(current.deviant)
  current.standard.CM <- colMeans(current.standard)
  
  # Create a time variable from the names of these variables
  Time = seq(from = -100, to = 348, by = 2)
  
  # Calculate the MMN (according to google and ChatGPT it is deviant - standard)
  MMN <-  current.deviant.CM - current.standard.CM
  
  # Join everything into a dataframe
  joined.data <- data.frame(Time = Time,
                            Deviant = current.deviant.CM,
                            Standard = current.standard.CM,
                            MMN = MMN) 
  
  # Make the data longer
  data[ii](/LeoLedesma237/LeoWebsite/wiki/ii) <- pivot_longer(joined.data, cols = -Time, names_to = "ERP", values_to = "Voltage")
  
}

Part 3: Visualizing the MMN at Cz for every subject individually

# Plot the MMN at Cz individually 
for(ii in 1:length(data)) {

  # Save each plot into a list
  plot.list <- list()
  
  plot.list[ii](/LeoLedesma237/LeoWebsite/wiki/ii) <- data[ii](/LeoLedesma237/LeoWebsite/wiki/ii) %>%
    ggplot(aes(Time, Voltage, color = ERP)) +
    geom_hline(yintercept=0, color = "black") +
    geom_vline(xintercept=0, color = "black") +
    geom_point(size = 1) +
    geom_line(size = 1) +
    labs(title = paste(Names[ii],"MMN Tones at Cz"),
         x = "Time (ms)",
         y = "MicroVolts") +
    theme_classic()
    
  # Print the graphs from the list created
  print(plot.list[ii](/LeoLedesma237/LeoWebsite/wiki/ii))

}

Part 4: Visualizing the Grand MMN from all subjects

# Creating an averaged Tone MMN at Cz

Voltage.list <- lapply(data, function(x) x$Voltage)
mean.Voltage <- rowMeans(do.call(cbind, Voltage.list))

# Use the same information from the first two variables in data 1 to create an averaged dataset
averaged.data <- data.frame(Time = data[1](/LeoLedesma237/LeoWebsite/wiki/1)$Time,
                            ERP = data[1](/LeoLedesma237/LeoWebsite/wiki/1)$ERP,
                            mean.Voltage = mean.Voltage)

# Graph the averaged data
averaged.data %>%
  ggplot(aes(Time, mean.Voltage, color = ERP)) +
  geom_hline(yintercept=0, color = "black") +
  geom_vline(xintercept=0, color = "black") +
  geom_point(size = 1) +
  geom_line(size = 1) +
  labs(title = paste("Averaged MMN Tones at Cz (n = ",length(data),")", sep =""),
       x = "Time (ms)",
       y = "MicroVolts") +
  theme_classic()