Compute individual 2IFC CIs from trial-level responses
Source:R/ci_from_responses_2ifc.R
ci_from_responses_2ifc.RdThin wrapper around rcicr::batchGenerateCI2IFC() that handles
the known rcicr-integration gotchas and exposes a uniform return
shape across the 2IFC and Brief-RC paths.
Use this when you have 2IFC trial-level responses and want the package to compute individual CIs ready for the reliability metrics.
Usage
ci_from_responses_2ifc(
responses,
rdata_path = NULL,
stimuli = NULL,
baseimage = NULL,
participant_col = "participant_id",
stimulus_col = "stimulus",
response_col = "response",
scaling = "autoscale",
keep_rendered = FALSE,
targetpath = tempfile("rcisignal_2ifc_"),
save_as_png = FALSE
)Arguments
- responses
A data frame with one row per trial. Must contain
participant_col(producer id), the column named bystimulus_col(stimulus id, integer, index into the rcicr noise pool), andresponse_colwith values in{-1, +1}.- rdata_path
Path to the
.Rdatafile produced byrcicr::generateStimuli2IFC(). Eitherrdata_pathorstimulimust be supplied; if both are givenstimuliwins.- stimuli
In-memory stimuli list as returned in
$stimulibysimulate_2ifc_data(). Use this in place ofrdata_pathwhen the simulation has been saved withsaveRDS()and reloaded in a different R session: the path stored on$rdata_pathno longer resolves, but$stimuliis self-contained. Internally the list is written to a fresh tempdir-backed.Rdatabefore the call into rcicr.- baseimage
Base image label used at stimulus-generation time. If
NULL, tries to read the single base stored inrdata_path.- participant_col, stimulus_col, response_col
Column names in
responses.- scaling
rcicr scaling option; one of
"autoscale","independent","constant","none". Passed through torcicr::batchGenerateCI2IFC(). Scaling only affects the$combined(rendered) image, not$ci, so the returned$signal_matrixis the raw mask regardless of this argument.- keep_rendered
If
TRUE, also extract rcicr's$combinedimage (base + scaled noise) per producer and stack into$rendered_ci. DefaultFALSE. Visualisation only.- targetpath
Where rcicr writes PNGs. Defaults to an auto-cleaned tempdir so the working directory is not polluted.
- save_as_png
Whether rcicr writes individual CI PNGs. Defaults to
FALSEfor speed in a pure reliability pipeline.
Value
A list with signal_matrix, optionally rendered_ci,
participants, img_dims, scaling, and rcicr_result.
Details
What the wrapper does for you:
Attaches
foreach,tibble,dplyrat runtime (%dopar%/tribble/%>%are not namespace-prefixed inside rcicr).Matches the
.Rdataextension case-insensitively (rcicr writes lowercase on some filesystems).Extracts the per-participant CI noise component (
$ci) from the rcicr result list and stacks it into a pixels x participants signal matrix, already base-subtracted, ready forrel_*().When
keep_rendered = TRUE, also extracts the rendered$combinedimage and stacks it as$rendered_cifor visualisation (display only, not for downstream stats).
rcicr must be installed (it is a Suggests dep; install with
remotes::install_github("rdotsch/rcicr") if needed).
Reading the result
$signal_matrixis the raw mask (rcicr's$ciper producer). This is what everyrel_*function expects.$rendered_ci, present whenkeep_rendered = TRUE, is thebase + scaling(mask)image rcicr would have written to PNG. Visualisation only.$rcicr_resultis the raw return value ofrcicr::batchGenerateCI2IFC().
Common mistakes
Passing
$rendered_citorel_*functions; it carries the scaling step, which distorts variance-based metrics.Forgetting that the warning about Mode 1 also applies if you later read
$rendered_ciback from PNG instead of using$signal_matrixdirectly.
Examples
if (FALSE) { # \dontrun{
# In your study, you replace the simulated inputs with your own:
# - `sim$data` -> your responses data frame (CSV via read_responses())
# - `sim$rdata_path` -> path to the rcicr stimuli .Rdata file used to
# generate your stimuli.
# The `simulate_2ifc_data()` helper just produces the same shapes so the
# example below is runnable end-to-end.
sim <- simulate_2ifc_data(n_per_condition = 10, n_trials = 60, seed = 1)
res <- ci_from_responses_2ifc(sim$data, rdata_path = sim$rdata_path)
dim(res$signal_matrix) # n_pixels x n_producers
rel_split_half(res$signal_matrix, n_permutations = 200L, seed = 1)
# Same call, but using the portable in-memory stimuli list -- the
# form that survives saveRDS()/readRDS() across R sessions.
res2 <- ci_from_responses_2ifc(sim$data, stimuli = sim$stimuli)
} # }