This document describes an R pipeline for analyzing Heart Rate Discrimination (HRD) task data and estimating metacognitive sensitivity (meta-d'). This approach allows researchers to assess how accurately individuals judge their own performance on an interoceptive discrimination task.
This package provides two main functions:
process_hrd_data(hrd_data, confbins)
– Prepares HRD data by binning confidence ratings and removing missing values.analyze_hrd_data(hrd_data, nRatings, plot_results, show_traceplot, participant_id)
– Computes metacognitive sensitivity and generates summary statistics.
It also provides an example wrapper script, which can be used to process multiple files:
analyze_study.R
– Processes multiple HRD log files, runs analyze_hrd_data()
on each, and creates a group results dataset.
Note that the user must specify a data directory and the desired number of confidence bins. Will attempt to read the participant ID from the filenames.
Further helper functions can be inspected in the /code directory:
helper_functions.R
- contains basic functions for plotting and processing HRD datacalc_auroc2.R
- adaptated from Matlab, calculates type 2 area under the reciever operating characteristic (AUROC2)trials2counts.R
- adapted from Matlab, formats confidence data as counts.bin_confidence_quantiles.R
- bins continuous confidence ratings in to nRatings - detects overly coarse data and attempts to correct it, unless too extreme, in which case it returns NaNs so the participant can be excluded.fit_metad_individ.R
- fits the Bayesian metacognition model. Note that this is fit to single subjects, not hierarchically.
The nRatings
parameter determines the number of bins for confidence ratings. More bins provide higher resolution but require more trials to stabilize estimates. Recommended settings:
nRatings = 2
: Low confidence vs. high confidence.nRatings = 4
: Standard for metacognitive analysis.nRatings = 6+
: Only if large trial numbers are available.
To run the metacognitive model, you need to install JAGS and the rjags
package:
- Install JAGS: Download Here
- Install
rjags
in R:install.packages("rjags")
The HRD task is used to assess interoceptive accuracy—the ability to perceive one’s own heartbeat. Participants judge whether presented auditory tones match their own heartbeats. This method provides a psychophysical estimate of interoceptive sensitivity and precision, and also collects confidence ratings which can be used to estimate metacognitive parameters.
For more details, see:
Legrand, N., et al. (2022). The heart rate discrimination task: A psychophysical method to estimate the accuracy and precision of interoceptive beliefs. Biological Psychology, 168, 108239.
DOI: 10.1016/j.biopsycho.2021.108239
To evaluate metacognitive ability in HRD, we estimate meta-d’, a measure of how well confidence ratings reflect actual task performance. This is adapted in R from Steve Fleming’s hierarchical Bayesian framework.
For more details, see:
Fleming, S. M. (2017). HMeta-d: Hierarchical Bayesian Estimation of Metacognitive Efficiency from Confidence Ratings. Neuroscience of Consciousness.
DOI: 10.1093/nc/nix007.
For a broader review of metacognition measurement, see:
Fleming, S. M., & Lau, H. C. (2014). How to measure metacognition. Frontiers in Human Neuroscience, 8, 443.
DOI: 10.3389/fnhum.2014.00443.
This function:
- Processes HRD task data
- Fits a metacognitive model using Bayesian estimation
- Computes key metrics: meta-d', AUROC, decision threshold, slope, confidence, and accuracy
- Saves plots for visualization
analyze_hrd_data(hrd_data, nRatings = 4, plot_results = TRUE, show_traceplot = TRUE, participant_id = "unknown")
Argument | Type | Description |
---|---|---|
hrd_data |
DataFrame | HRD dataset for one subject |
nRatings |
Integer | Number of confidence rating bins |
plot_results |
Boolean | Whether to display and save plots |
show_traceplot |
Boolean | Whether to generate a traceplot |
participant_id |
String | Subject identifier for saving figures |
A data frame containing:
estimated_threshold
: Decision threshold from last trial, estimated by psiestimated_slope
: Estimated decision slope, estimated by psid
: Interoceptive sensivity (d')mean_accuracy
: Average accuracy across trialsmean_confidence
: Average confidence ratingauroc
: Type-2 Area Under the ROC Curve (non-parametric metacognitive discrimination measure)metad
: Meta-d' (metacognitive sensitivity)mratio
: Meta-d' / d' ratio (metacognitive efficiency)
# Load HRD data
hrd_data <- read_delim("data/SS9909HRD_final.txt", delim = ",")
# Analyze subject data
results <- analyze_hrd_data(hrd_data, nRatings = 4, plot_results = TRUE, show_traceplot = TRUE, participant_id = "SS9909")
# View results
print(results)
These figures help assess data quality:
- What it shows: A) Distribution of confidence ratings and B) evolution of the staircase across trials. Alpha is the delta-BPM threshold, and inspection of this plot can reveal extreme behavior or poorly stabilized thresholds.
- How to use it: Typically, should show higher confidence on correct trials, reasonably distributed confidence ratings (extreme skew can impair or make meta-d' estimation impossible), and a converged staircase.
- What it shows: The estimated posterior distribution of meta-d’, showing uncertainty around the estimated parameter. More trials will typically reduce uncertainty.
- How to use it: Optional trace plots ('fuzzy catapillars') can also be inspected for convergence issues, as well as R-hat values. An unfuzzy plot can indicate issues with convergence.