Skip to content

Commit ea5ed29

Browse files
committed
Updates to figures
1 parent b375048 commit ea5ed29

8 files changed

+32
-16
lines changed

code/analyze_hrd_data.R

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,42 @@ source("code/trials2counts.R")
1515
source("code/fit_metad_indiv.R")
1616
source("code/calc_auroc2.R")
1717

18-
analyze_hrd_data <- function(hrd_data, nRatings = 4, plot_results = TRUE, show_traceplot = TRUE) {
18+
analyze_hrd_data <- function(hrd_data, nRatings = 4, plot_results = TRUE, show_traceplot = TRUE, participant_id = "unknown") {
1919

2020
# Ensure factors are correctly formatted
2121
hrd_data <- make_factors(hrd_data)
2222

2323
# Process HRD data with the specified number of rating bins
2424
processed_hrd_data <- process_hrd_data(hrd_data, nRatings)
2525

26+
# Ensure figs/ directory exists
27+
if (!dir.exists("figs")) {
28+
dir.create("figs")
29+
}
30+
2631
# Generate and display plots if requested
2732
if (plot_results) {
2833
confidence_hist <- plot_confidence_histogram(hrd_data)
2934
trial_alpha_plot <- plot_trial_alpha(hrd_data)
30-
print(confidence_hist + trial_alpha_plot)
35+
combined_plot <- confidence_hist + trial_alpha_plot
36+
37+
print(combined_plot)
38+
39+
# Save figure
40+
ggsave(filename = sprintf("figs/%s_confidence_trial_alpha.png", participant_id), plot = combined_plot, width = 8, height = 4, dpi = 150)
3141
}
3242

3343
# Extract inputs for trials2counts
3444
stimID <- processed_hrd_data$Signal
3545
response <- processed_hrd_data$Response
3646
rating <- processed_hrd_data$ConfidenceBinned
3747

48+
# Remove NA values before trials2counts
49+
valid_trials <- complete.cases(stimID, response, rating)
50+
stimID <- stimID[valid_trials]
51+
response <- response[valid_trials]
52+
rating <- rating[valid_trials]
53+
3854
# Convert trials to counts
3955
counts <- trials2counts(stimID, response, rating, nRatings)
4056
nR_S1 <- counts[[1]]
@@ -71,25 +87,23 @@ analyze_hrd_data <- function(hrd_data, nRatings = 4, plot_results = TRUE, show_t
7187
# Compute mean confidence
7288
mean_confidence <- mean(hrd_data$Confidence, na.rm = TRUE)
7389

74-
# Compute mean accuracy
75-
76-
# Ensure ResponseCorrect is numeric (convert from factor levels "FALSE"/"TRUE" to 0/1)
90+
# Ensure ResponseCorrect is numeric
7791
hrd_data$ResponseCorrect <- as.numeric(hrd_data$ResponseCorrect) - 1
7892

7993
# Compute mean accuracy
8094
mean_accuracy <- mean(hrd_data$ResponseCorrect, na.rm = TRUE)
8195

82-
8396
# Extract final EstimatedThreshold and EstimatedSlope values
8497
estimated_threshold <- tail(hrd_data$EstimatedThreshold, 1)
8598
estimated_slope <- tail(hrd_data$EstimatedSlope, 1)
8699

87100
# Print results
88101
message(sprintf("Metacognition scores: AUROC = %.2f, MRatio = %.2f, Mean Confidence = %.2f", auroc, mratio, mean_confidence))
89102

90-
# Show traceplot if requested
103+
# Show traceplot if requested and save it
91104
if (show_traceplot) {
92105
traceplot(output)
106+
ggsave(filename = sprintf("figs/%s_traceplot.png", participant_id), width = 8, height = 4, dpi = 300)
93107
}
94108

95109
# Generate posterior distribution plot
@@ -106,23 +120,25 @@ analyze_hrd_data <- function(hrd_data, nRatings = 4, plot_results = TRUE, show_t
106120

107121
if (plot_results) {
108122
print(post_plot)
123+
ggsave(filename = sprintf("figs/%s_posterior_meta_d.png", participant_id), plot = post_plot, width = 6, height = 4, dpi = 150)
109124
}
110125

111126
# Return results as a dataframe
112127
results_df <- data.frame(
113128
mean_confidence = mean_confidence,
129+
estimated_threshold = estimated_threshold,
130+
estimated_slope = estimated_slope,
131+
mean_accuracy = mean_accuracy,
132+
auroc = auroc,
114133
d = d1,
115134
metad = metad,
116-
mratio = mratio,
117-
auroc = auroc,
118-
mean_accuracy = mean_accuracy,
119-
estimated_threshold = estimated_threshold,
120-
estimated_slope = estimated_slope
135+
mratio = mratio
121136
)
122137

123138
return(results_df)
124139
}
125140

141+
126142
# Example usage:
127143
# hrd_data <- read_delim("path/to/data.txt", delim = ",")
128144
# results <- analyze_hrd_data(hrd_data, nRatings = 4, plot_results = TRUE, show_traceplot = TRUE)

code/analyze_study.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ for (file in log_files) {
2929
hrd_data <- read_delim(file, delim = ",")
3030

3131
# Apply the HRD analysis function
32-
result <- analyze_hrd_data(hrd_data, nRatings = 4, plot_results = FALSE, show_traceplot = FALSE)
32+
result <- analyze_hrd_data(hrd_data, nRatings = 4, plot_results = TRUE, show_traceplot = FALSE, participant_id = participant_id)
3333

3434
# Add participant ID and session type
3535
result <- result %>%

code/helper_functions.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ library(tidyverse)
77
#'
88
#' @return A ggplot2 theme object.
99
theme_custom <- function() {
10-
theme_minimal(base_size = 14) +
10+
theme_minimal(base_size = 10) +
1111
theme(
1212
plot.title = element_text(face = "bold", hjust = 0.5),
1313
axis.title = element_text(face = "bold"),
50.1 KB
Loading

figs/SS9909_posterior_meta_d.png

30.4 KB
Loading
50.1 KB
Loading

figs/unknown_posterior_meta_d.png

32.7 KB
Loading

group_results.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"participant_id","session_type","estimated_threshold","estimated_slope","mean_accuracy","mean_confidence","auroc","d","metad","mratio"
2-
"SS9909","PRE",-41.1313575764749,22.1337813305331,0.475,64.975,0.552656104380242,-0.0619316234553173,-0.0582300985384358,0.940232070300045
3-
"SS9909","POST",-24.3720289784148,15.6519614790519,0.4875,69.506329113924,0.489089184060721,0.642754952497923,0.182892710102267,0.284545003335246
2+
"SS9909","PRE",-41.1313575764749,22.1337813305331,0.475,64.975,0.552656104380242,-0.0619316234553173,-0.0107351866295183,0.173339338298851
3+
"SS9909","POST",-24.3720289784148,15.6519614790519,0.4875,69.506329113924,0.489089184060721,0.642754952497923,0.141847292936391,0.220686425495646

0 commit comments

Comments
 (0)