11# Hierarchical-Interoception
22
3- This repository contains the complete analysis pipeline for hierarchical psychometric function modeling applied to interoceptive tasks. The project implements Bayesian hierarchical models for analyzing Heart Rate Discrimination Task (HRDT) and Respiratory Resistance Sensitivity Task (RRST) data, with comprehensive power analysis capabilities and interactive visualization tools.
3+ This repository contains the complete analysis pipeline for hierarchical psychometric function modeling applied to intero & exteroceptive tasks.
4+ The project implements Bayesian hierarchical models for analyzing Heart Rate Discrimination Task (HRDT) and Respiratory Resistance Sensitivity Task (RRST) data,
5+ with comprehensive power analysis and interactive visualization tools.
46
57## Table of Contents
68
791 . [ Technical Details of the Hierarchical Model] ( #technical-details-of-the-hierarchical-model )
8- 2 . [ Data Simulation Workflow] ( #data-simulation-workflow )
9- 3 . [ Reproducing Key Figures] ( #reproducing-key-figures )
10- 4 . [ Shiny App Deployment and Usage] ( #shiny-app-deployment-and-usage )
10+ 2 . [ Using the BRMS Demo] ( #using-the-brms-demo )
11+ 3 . [ Shiny App Deployment and Usage] ( #shiny-app-deployment-and-usage )
1112
1213## Technical Details of the Hierarchical Model
1314
@@ -17,7 +18,7 @@ The hierarchical model implements a psychometric function with three key paramet
1718
18191 . ** Threshold (α)** : The stimulus intensity at which the probability of correct response is 0.5
19202 . ** Slope (β)** : The steepness of the psychometric function, indicating sensitivity
20- 3 . ** Lapse rate (λ)** : The asymptotic error rate for extreme stimulus intensities
21+ 3 . ** Lapse rate (λ)** : The upper (1-$\lambda$) and lower asymptotes.
2122
2223### Mathematical Framework
2324
@@ -27,8 +28,8 @@ $$P(response) = \lambda + (1 - 2\lambda) \cdot \left(0.5 + 0.5 \cdot \text{erf}\
2728
2829Where:
2930- $x$ is the stimulus intensity
30- - $\alpha$ is the threshold parameter
31- - $\beta$ is the slope parameter (constrained to be positive)
31+ - $\alpha$ is the threshold
32+ - $\beta$ is the slope (constrained to be positive)
3233- $\lambda$ is the lapse rate (constrained to [ 0, 0.5] )
3334- $\text{erf}$ is the error function
3435
@@ -44,48 +45,40 @@ The model implements a three-level hierarchy:
4445
4546The model is implemented in Stan with the following key components:
4647
47- ``` stan
48+ ```
4849// Group-level parameters
4950vector[N] gm; // Group means for all parameters
50- vector<lower = 0>[N] tau_u; // Between-participant scales
51+ vector<lower = 0>[N] tau_u; // Between-participant varability
5152matrix[N, S] z_expo; // Participant deviations
5253
53- // Transformed parameters
54+ // Individual psychometric parameters
5455vector[S] alpha_int = (gm[1] + (tau_u[1] * z_expo[1,]))';
5556vector[S] beta_int = (gm[2] + (tau_u[2] * z_expo[2,]))';
5657vector[S] lapse = (inv_logit(gm[3] + (tau_u[3] * z_expo[3,])) / 2)';
5758```
5859
5960### Prior Specifications
6061
61- The model uses empirically informed priors derived from previous studies:
62-
63- - ** Threshold (α)** : Normal(-8.67, 0.52×10) for intercept, Normal(0, 11.23) for treatment effect
64- - ** Slope (β)** : Normal(-2.3, 0.02×10) for intercept, Normal(0, 0.34) for treatment effect
65- - ** Lapse rate (λ)** : Normal(-4.32, 0.29)
66-
67- ### Model Comparison
68-
69- The repository supports both Gaussian and Gumbel cumulative distribution functions:
70-
71- - ** Gaussian** : Uses the error function (erf) for the cumulative normal
72- - ** Gumbel** : Uses the exponential function for the cumulative Gumbel
62+ We recommend using our empirically informed priors derived from the ~ 500 subject study these can be added in the stancode as follows::
63+ ```
64+ gm[1] ~ normal(-8.67, 5.2); //group mean threshold
65+ gm[2] ~ normal(-2.3,0.2); //group mean slope (log)
66+ gm[3] ~ normal(-4.32, 0.29); //group mean lapse rate (logit)
7367
74- Model comparison is performed using leave-one-out cross-validation (LOO-CV) with moment matching.
68+ ```
7569
76- ## Data Simulation Workflow
7770
78- ### Using the BRMS Demo
71+ ## Using the BRMS Demo
7972
8073The ` apps & wrappers/BRMS demo.Rmd ` provides a complete workflow for data simulation and analysis:
8174
82751 . ** Data Simulation** : Generate synthetic data with known parameters
83762 . ** Model Specification** : Define the hierarchical psychometric function
84773 . ** Model Fitting** : Fit using BRMS with Stan backend
85- 4 . ** Diagnostics** : Check convergence and sampling quality
86- 5 . ** Inference ** : Extract posterior distributions and credible intervals
78+ 4 . ** Model Diagnostics** : Check convergence and sampling quality
79+ 5 . ** Model interpretation ** : Extract posterior distributions and credible intervals
8780
88- ### Simulation Parameters
81+ Below we show the general workflow of the demo. We first simulate data from the following parameters
8982
9083``` r
9184# Design parameters
@@ -102,7 +95,8 @@ mu_lambda_HRDT <- -4.2
10295sd_lambda_HRDT <- 2
10396```
10497
105- ### Model Specification in BRMS
98+ Then specifying the model in brms:
99+
106100
107101``` r
108102bff_HRDT = bf(
@@ -117,7 +111,7 @@ bff_HRDT = bf(
117111)
118112```
119113
120- ### Running the Analysis
114+ Running this model on the simulated data:
121115
122116``` r
123117fit_eip_HRDT <- brm(
@@ -137,61 +131,6 @@ fit_eip_HRDT <- brm(
137131)
138132```
139133
140- ## Reproducing Key Figures
141-
142- ### Figure 1: Psychometric Function Forms
143-
144- ** File** : ` figures_scripts/Figure1.Rmd `
145-
146- This figure compares different psychometric function formulations:
147-
148- ``` r
149- PFs <- tibble(x = seq(- 6 , 11 , .001 )) %> %
150- mutate(
151- Gaussian = pnorm(x - 2 ),
152- Gumbel = 1 - exp(- 10 ^ (0.4 * (x - 2.4 )))
153- ) %> %
154- pivot_longer(cols = ! x ) %> %
155- rename(Formulation = name )
156- ```
157-
158- ** To reproduce** :
159- ``` r
160- source(" figures_scripts/Figure1.Rmd" )
161- ```
162-
163- ### Figure 3: Model Recovery
164-
165- ** File** : ` figures_scripts/Figure3.Rmd `
166-
167- This figure shows model recovery performance across different generative models:
168-
169- ``` r
170- # Load model recovery results
171- load(here :: here(" results" , " model_and_parameter_recovery.RData" ))
172- model_recovery <- results
173-
174- # Calculate recovery statistics
175- mod_comp <- tibble(
176- winning_model = rep(NaN , 400 ),
177- generative_model = rep(NaN , 400 ),
178- any_pareto_k = rep(NaN , 400 ),
179- significant = rep(NaN , 400 ),
180- meanrhat = rep(NaN , 400 ),
181- sum_div = rep(NaN , 400 )
182- )
183- ```
184-
185- ** To reproduce** :
186- ``` r
187- source(" figures_scripts/Figure3.Rmd" )
188- ```
189-
190- ### Additional Figures
191-
192- - ** Figure 4** : Parameter recovery plots (` figures_scripts/Figure4.Rmd ` )
193- - ** Figure 5** : Power analysis results (` figures_scripts/Figure5.Rmd ` )
194- - ** Figure 6** : Hierarchical vs. simple t-test comparison (` figures_scripts/Figure6.Rmd ` )
195134
196135## Shiny App Deployment and Usage
197136
@@ -212,89 +151,55 @@ The Shiny app (`apps & wrappers/shiny app.R`) provides an interactive interface
212151install.packages(c(" shiny" , " tidyverse" , " flextable" , " posterior" ))
213152```
214153
215- 2 . ** Run the App** :
154+ 2 . ** Running the App** :
155+ Either enter this in the R-console
216156``` r
217157shiny :: runApp(" apps & wrappers/shiny app.R" )
218158```
159+ or go to the "apps & wrappers" directory and run the ` shiny app.R ` script
219160
220- #### Server Deployment
221-
222- 1 . ** Prepare for Deployment** :
223- ``` r
224- # Ensure all data files are accessible
225- # Check that results/power analysis/Extracted/all_draws.csv exists
226- ```
227161
228- 2 . ** Deploy to Shiny Server** :
229- ``` bash
230- # Copy files to server
231- scp -r " apps & wrappers/" user@server:/path/to/shiny/apps/
232- scp -r results/ user@server:/path/to/shiny/data/
233- ```
234162
235163### App Usage
236164
237165#### Power Grid Panel
238166
239- 1 . ** Select Parameter** : Choose between "Threshold" or "Slope"
240- 2 . ** Set Effect Size** : Specify the desired effect size (0.0 to 1.0)
241- 3 . ** Adjust Sample Ranges** : Use sliders to set subject and trial ranges
167+ 1 . ** Parameter** : Choose between "Threshold" or "Slope"
168+ 2 . ** Effect Size** : Specify the desired effect size (0.0 to 1.0)
169+ 3 . ** Adjust Sample and trial Ranges** : Use sliders to set subject and trial ranges
2421704 . ** Choose Power Level** : Select desired power (0.80, 0.90, or 0.95)
2431715 . ** Interpret Results** : Contour lines show combinations achieving target power
244172
245173#### Effect Size vs. Power Panel
246174
247- 1 . ** Set Sample Size** : Specify number of subjects and trials
248- 2 . ** View Power Curve** : See how power changes with effect size
249- 3 . ** Identify Minimum Effect** : Find the smallest detectable effect size
175+ 1 . ** Parameter** : Choose between "Threshold" or "Slope"
176+ 2 . ** Set Sample Size** : Specify number of subjects and trials
177+ 3 . ** View Power Curve** : See how power changes with effect size
178+ 3 . ** Interpret Results** : Psychometric functions show (efffect size by power).
250179
251180#### Manual Input Panel
252181
253- 1 . ** Enter Parameters** : Specify exact sample sizes and effect size
254- 2 . ** Submit Query** : Get precise power estimates
182+ 1 . ** Enter Parameters** : Specify exact sample sizes and effect size and parameter
183+ 2 . ** Submit Query** : Get precise power estimates for above
2551843 . ** View Results** : See power for hierarchical, simple t-test, and uncertainty propagation methods
256185
257- ### Data Requirements
258-
259- The app requires the following data files:
260- - ` results/power analysis/Extracted/all_draws.csv ` : Power analysis results
261- - ` results/power analysis/Extracted/all_data.csv ` : Summary statistics
262-
263- ### Customization
264-
265- To modify the app for different analyses:
266-
267- 1 . ** Update Data Source** : Modify the ` all_data ` loading in the server function
268- 2 . ** Add New Parameters** : Extend the parameter selection options
269- 3 . ** Customize Plots** : Modify the ggplot aesthetics and themes
270- 4 . ** Add New Panels** : Create additional tabPanels for new functionality
271-
272186## File Structure
273187
274188```
275189Hierarchical-Interoception/
276- ├── Analysis/ # Main analysis scripts
277- ├── apps & wrappers/ # BRMS demo and Shiny app
278- ├── Data/ # Raw data files
279- ├── figures/ # Generated figures
280- ├── figures_scripts/ # Figure generation scripts
281- ├── results/ # Analysis results and power analysis
282- ├── scripts/ # Utility functions and main scripts
283- ├── simulated_data/ # Simulated datasets
284- └── stanmodels/ # Stan model definitions
190+ ├── Analysis/ # Model comparison for the HRDT
191+ ├── apps & wrappers/ # BRMS demo and Shiny app
192+ ├── Data/ # Raw data files
193+ ├── figures/ # Generated figures
194+ ├── figures_scripts/ # Figure generation scripts
195+ ├── results/ # Analysis results and power analysis
196+ ├── scripts/ # Utility functions and main scripts
197+ ├── simulated_data/ # Simulated datasets (model recovery and power analysis)
198+ └── stanmodels/ # Stan model definitions (VMP-data refit, poweranalysis and model recovery)
285199```
286200
287201## Dependencies
288202
289- ### Required R Packages
290- ``` r
291- install.packages(c(
292- " cmdstanr" , " tidyverse" , " posterior" , " bayesplot" ,
293- " tidybayes" , " rstan" , " brms" , " pracma" , " shiny" ,
294- " flextable" , " loo" , " furrr"
295- ))
296- ```
297-
298203### Stan Installation
299204``` r
300205# Install cmdstanr
@@ -310,9 +215,9 @@ cmdstanr::install_cmdstan()
310215If you use this code in your research, please cite:
311216
312217```
313- Courtin, A.S., & Fischer Ehmsen, J. (2025). Hierarchical Bayesian modeling of interoceptive psychometric functions. [Paper DOI to be added]
218+ Courtin, A.S., & Fischer Ehmsen, J. (2025). Hierarchical Bayesian modeling of interoceptive psychometric functions. https://doi.org/10.1101/2025.08.27.672360
314219```
315220
316221## Contact
317222
318- For questions or issues with this repository, please contact the authors or open an issue on the repository page.
223+ For questions or issues with this repository, please contact the authors or open an issue on the repository page.
0 commit comments