And libraries!
All_data <- read.csv("all_SS_ET_step2.csv", header = T)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Summarized_ETdata <- All_data %>%
group_by(participant,condsFile,trial) %>%
summarise(averagePupilChange=mean(change_from_baseline,na.rm = T),
baseline=first(baseline)
)
## `summarise()` has grouped output by 'participant', 'condsFile'. You can
## override using the `.groups` argument.
boxplot(averagePupilChange ~ trial, data = Summarized_ETdata,
main = "Average pupil change per trial",
xlab = "Trial (hard first, then easy, then hard, etc)", ylab = "Average change from baseline",
col = "lightgray", ylim = c(-1.5, 1.0))
# Add points for each participant's average
Summarized_ETdata$trial <- as.factor(Summarized_ETdata$trial)
points(jitter(as.numeric(Summarized_ETdata$trial)),
Summarized_ETdata$averagePupilChange,
col = "blue", pch = 16, cex = 0.7)
# Calculate mean for each condition
means <- tapply(Summarized_ETdata$averagePupilChange, Summarized_ETdata$trial, mean)
# Add mean values as text labels above each box
text(x = 1:length(means), y = means + 0.4,
labels = round(means, 2), col = "red", cex = 1, font = 2)
Super cool! We even see fatigue/boredom taking a hold towards the end!
# Example 1
# First we filter the data into a single chunk to make things easier
Trace_data <- All_data %>%
filter(participant == "7" &
trial == 3 &
!is.na(d_time))
plot(Trace_data$calFrame, Trace_data$change_from_baseline,
type = "l", # 'l' specifies a line plot
col = "blue", # Line color
lwd = 2, # Line width
main = "Pupil change from baseline, Participant 7, Trial 3 (Hard)",
xlab = "Frame",
ylab = "Pupil change",
ylim = c(-1.0, 2.0)) # Set y-axis limits
# Add grid lines for a cleaner look
grid()
# Example 2
# First we filter the data into a single chunk to make things easier
Trace_data <- All_data %>%
filter(participant == "19" &
trial == 4 &
!is.na(d_time))
plot(Trace_data$calFrame, Trace_data$change_from_baseline,
type = "l", # 'l' specifies a line plot
col = "blue", # Line color
lwd = 2, # Line width
main = "Pupil change from baseline, Participant 19, Trial 4 (Easy)",
xlab = "Frame",
ylab = "Pupil change",
ylim = c(-1.0, 2.0)) # Set y-axis limits
# Add grid lines for a cleaner look
grid()
# Example 3
Trace_data <- All_data %>%
filter(participant == "3" &
trial == 5 &
!is.na(d_time))
plot(Trace_data$calFrame, Trace_data$change_from_baseline,
type = "l", # 'l' specifies a line plot
col = "blue", # Line color
lwd = 2, # Line width
main = "Pupil change from baseline, Participant 3, Trial 5 (Hard)",
xlab = "Frame",
ylab = "Pupil change",
ylim = c(-1.0, 2.0)) # Set y-axis limits
# Add grid lines for a cleaner look
grid()
# Specify difficulty level
All_data <- All_data %>%
mutate(difficulty = ifelse(trial %% 2 == 1, "hard", "easy"))
# Make a frame counter for each trial
All_data <- All_data %>%
group_by(participant, trial) %>%
mutate(counter = row_number()) %>%
ungroup()
# Step 1: Aggregate data by participant, difficulty, frame
average_trace <- All_data %>%
filter(!is.na(d_time)) %>%
group_by(participant, difficulty, counter) %>%
summarize(mean_change = mean(change_from_baseline, na.rm = TRUE), .groups = "drop")
# Example 1
Trace_data <- average_trace %>%
filter(participant == "9")
# Get the unique trials for this participant and condition
levels <- unique(Trace_data$difficulty)
# Set up the plot area with labels and limits
plot(NULL, xlim = range(Trace_data$counter), ylim = c(-0.5, 0.5),
xlab = "Frame", ylab = "Average pupil change",
main = paste("Participant 9"))
# Choose a set of colors for each trial
colors <- rainbow(length(levels))
# Plot each trial's average trace as a separate line in the plot
for (i in seq_along(levels)) {
levels_data <- subset(Trace_data, difficulty == levels[i])
lines(levels_data$counter, levels_data$mean_change, col = colors[i], lwd = 1)
}
# Add a legend to differentiate each trial
legend("topright", legend = paste("levels", levels), col = colors, lwd = 2)
# Example 2
Trace_data <- average_trace %>%
filter(participant == "2")
# Get the unique trials for this participant and condition
levels <- unique(Trace_data$difficulty)
# Set up the plot area with labels and limits
plot(NULL, xlim = range(Trace_data$counter), ylim = c(-0.5, 0.5),
xlab = "Frame", ylab = "Average pupil change",
main = paste("Participant 2"))
# Choose a set of colors for each trial
colors <- rainbow(length(levels))
# Plot each trial's average trace as a separate line in the plot
for (i in seq_along(levels)) {
levels_data <- subset(Trace_data, difficulty == levels[i])
lines(levels_data$counter, levels_data$mean_change, col = colors[i], lwd = 1)
}
# Add a legend to differentiate each trial
legend("topright", legend = paste("levels", levels), col = colors, lwd = 2)
# Example 3
Trace_data <- average_trace %>%
filter(participant == "18")
# Get the unique trials for this participant and condition
levels <- unique(Trace_data$difficulty)
# Set up the plot area with labels and limits
plot(NULL, xlim = range(Trace_data$counter), ylim = c(-0.5, 0.5),
xlab = "Frame", ylab = "Average pupil change",
main = paste("Participant 18"))
# Choose a set of colors for each trial
colors <- rainbow(length(levels))
# Plot each trial's average trace as a separate line in the plot
for (i in seq_along(levels)) {
levels_data <- subset(Trace_data, difficulty == levels[i])
lines(levels_data$counter, levels_data$mean_change, col = colors[i], lwd = 1)
}
# Add a legend to differentiate each trial
legend("topright", legend = paste("levels", levels), col = colors, lwd = 2)
So cool to see this for real with my own data!
# Filter, group, and calculate the averages
average_trace <- All_data %>%
filter(!is.na(d_time)) %>%
group_by(difficulty, counter) %>%
summarize(mean_change = mean(change_from_baseline, na.rm = TRUE), .groups = "drop")
# Get the unique difficulty levels
levels <- unique(average_trace$difficulty)
# Set up the plot area with labels and limits
plot(NULL, xlim = range(average_trace$counter), ylim = c(-0.5, 0.5),
xlab = "Frame", ylab = "Average pupil change",
main = "All Participants")
# Choose a set of colors for each level of difficulty
colors <- rainbow(length(levels))
# Plot each level's average trace as a separate line in the plot
for (i in seq_along(levels)) {
levels_data <- subset(average_trace, difficulty == levels[i])
lines(levels_data$counter, levels_data$mean_change, col = colors[i], lwd = 2)
}
# Add a legend to differentiate each level
legend("topright", legend = levels, col = colors, lwd = 2)