@@ -15,26 +15,42 @@ source("code/trials2counts.R")
15
15
source(" code/fit_metad_indiv.R" )
16
16
source(" code/calc_auroc2.R" )
17
17
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 " ) {
19
19
20
20
# Ensure factors are correctly formatted
21
21
hrd_data <- make_factors(hrd_data )
22
22
23
23
# Process HRD data with the specified number of rating bins
24
24
processed_hrd_data <- process_hrd_data(hrd_data , nRatings )
25
25
26
+ # Ensure figs/ directory exists
27
+ if (! dir.exists(" figs" )) {
28
+ dir.create(" figs" )
29
+ }
30
+
26
31
# Generate and display plots if requested
27
32
if (plot_results ) {
28
33
confidence_hist <- plot_confidence_histogram(hrd_data )
29
34
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 )
31
41
}
32
42
33
43
# Extract inputs for trials2counts
34
44
stimID <- processed_hrd_data $ Signal
35
45
response <- processed_hrd_data $ Response
36
46
rating <- processed_hrd_data $ ConfidenceBinned
37
47
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
+
38
54
# Convert trials to counts
39
55
counts <- trials2counts(stimID , response , rating , nRatings )
40
56
nR_S1 <- counts [[1 ]]
@@ -71,25 +87,23 @@ analyze_hrd_data <- function(hrd_data, nRatings = 4, plot_results = TRUE, show_t
71
87
# Compute mean confidence
72
88
mean_confidence <- mean(hrd_data $ Confidence , na.rm = TRUE )
73
89
74
- # Compute mean accuracy
75
-
76
- # Ensure ResponseCorrect is numeric (convert from factor levels "FALSE"/"TRUE" to 0/1)
90
+ # Ensure ResponseCorrect is numeric
77
91
hrd_data $ ResponseCorrect <- as.numeric(hrd_data $ ResponseCorrect ) - 1
78
92
79
93
# Compute mean accuracy
80
94
mean_accuracy <- mean(hrd_data $ ResponseCorrect , na.rm = TRUE )
81
95
82
-
83
96
# Extract final EstimatedThreshold and EstimatedSlope values
84
97
estimated_threshold <- tail(hrd_data $ EstimatedThreshold , 1 )
85
98
estimated_slope <- tail(hrd_data $ EstimatedSlope , 1 )
86
99
87
100
# Print results
88
101
message(sprintf(" Metacognition scores: AUROC = %.2f, MRatio = %.2f, Mean Confidence = %.2f" , auroc , mratio , mean_confidence ))
89
102
90
- # Show traceplot if requested
103
+ # Show traceplot if requested and save it
91
104
if (show_traceplot ) {
92
105
traceplot(output )
106
+ ggsave(filename = sprintf(" figs/%s_traceplot.png" , participant_id ), width = 8 , height = 4 , dpi = 300 )
93
107
}
94
108
95
109
# Generate posterior distribution plot
@@ -106,23 +120,25 @@ analyze_hrd_data <- function(hrd_data, nRatings = 4, plot_results = TRUE, show_t
106
120
107
121
if (plot_results ) {
108
122
print(post_plot )
123
+ ggsave(filename = sprintf(" figs/%s_posterior_meta_d.png" , participant_id ), plot = post_plot , width = 6 , height = 4 , dpi = 150 )
109
124
}
110
125
111
126
# Return results as a dataframe
112
127
results_df <- data.frame (
113
128
mean_confidence = mean_confidence ,
129
+ estimated_threshold = estimated_threshold ,
130
+ estimated_slope = estimated_slope ,
131
+ mean_accuracy = mean_accuracy ,
132
+ auroc = auroc ,
114
133
d = d1 ,
115
134
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
121
136
)
122
137
123
138
return (results_df )
124
139
}
125
140
141
+
126
142
# Example usage:
127
143
# hrd_data <- read_delim("path/to/data.txt", delim = ",")
128
144
# results <- analyze_hrd_data(hrd_data, nRatings = 4, plot_results = TRUE, show_traceplot = TRUE)
0 commit comments