| Title: | TVA Models in 'Stan' using 'R' and 'StanTVA' |
|---|---|
| Description: | 'Stan' implementation of the Theory of Visual Attention (TVA; Bundesen, 1990; <doi:10.1037/0033-295X.97.4.523>) and numerous convenience functions for generating, compiling, fitting, and analyzing TVA models. |
| Authors: | Maximilian M. Rabe [aut, cre] (ORCID: <https://orcid.org/0000-0002-2556-5644>), Søren Kyllingsbæk [aut] (ORCID: <https://orcid.org/0000-0002-4100-437X>) |
| Maintainer: | Maximilian M. Rabe <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.4.0 |
| Built: | 2026-06-10 12:00:46 UTC |
| Source: | https://github.com/mmrabe/rstantva |
Returns the StanTVA parameter aliases for the underlying RStan fit.
## S4 method for signature 'stantvafit' alias(object)## S4 method for signature 'stantvafit' alias(object)
object |
The StanTVA fit object. |
A character vector of parameter aliases.
## Not run: al <- alias(fit) al ## End(Not run)## Not run: al <- alias(fit) al ## End(Not run)
Returns the model coefficients (sum of fixed + random effects, grouped by random factor) for a StanTVA fit object.
## S4 method for signature 'stantvafit' coef(object)## S4 method for signature 'stantvafit' coef(object)
object |
The StanTVA fit object. |
The model coefficients, grouped by random factors.
## Not run: fixef <- coef(fit) fixef ## End(Not run)## Not run: fixef <- coef(fit) fixef ## End(Not run)
Returns posterior samples from a fitted RStanTVA model.
## S4 method for signature 'stantvafit' extract(object, pars, ...)## S4 method for signature 'stantvafit' extract(object, pars, ...)
object |
The RStanTVA fit. |
pars |
(Optional) A character vector of variable names to extract. |
... |
Additional arguments passed to |
See rstan::extract() for details.
## Not run: f <- read_stantva_fit("fit.rds") extract(f, "C_Intercept") ## End(Not run)## Not run: f <- read_stantva_fit("fit.rds") extract(f, "C_Intercept") ## End(Not run)
Returns the fitted values for latent model parameters. This is identical to calling predict() without new data.
## S4 method for signature 'stantvafit' fitted(object, variables = names(object@stanmodel@code@df))## S4 method for signature 'stantvafit' fitted(object, variables = names(object@stanmodel@code@df))
object |
The StanTVA fit object. |
variables |
The names of the parameters to retrieve. |
The fitted values.
## Not run: p <- fitted(fit, variables = c("C","K")) colMeans(p$C) ## End(Not run)## Not run: p <- fitted(fit, variables = c("C","K")) colMeans(p$C) ## End(Not run)
Returns the fixed effects for a StanTVA fit object.
## S4 method for signature 'stantvafit' fixef(object)## S4 method for signature 'stantvafit' fixef(object)
object |
The StanTVA fit object. |
The fixed effects.
## Not run: fixed_effects <- fixef(fit) fixed_effects ## End(Not run)## Not run: fixed_effects <- fixef(fit) fixed_effects ## End(Not run)
Returns the pointwise log-likelihood of a StanTVA fit.
## S4 method for signature 'stantvafit' logLik(object)## S4 method for signature 'stantvafit' logLik(object)
object |
The StanTVA fit. |
The pointwise log likelihood.
## Not run: loglik <- logLik(model, data, params) loglik ## End(Not run)## Not run: loglik <- logLik(model, data, params) loglik ## End(Not run)
Extracts the Stan code from a StanTVA model object.
model_code(object, type) ## S4 method for signature 'stanmodel' model_code(object, type = c("stan", "stan2", "cpp")) ## S4 method for signature 'stanfit' model_code(object, type)model_code(object, type) ## S4 method for signature 'stanmodel' model_code(object, type = c("stan", "stan2", "cpp")) ## S4 method for signature 'stanfit' model_code(object, type)
object |
A StanTVA model object or fit. |
type |
The type of code to return ( |
A RStanTVA model code object (stan), or a string containing the code (stan2 or cpp).
model_code(stanmodel): method
model_code(stanfit): Extract code from a model fit
## Not run: model <- stantva_model(locations = 2) model_code(model) ## End(Not run)## Not run: model <- stantva_model(locations = 2) model_code(model) ## End(Not run)
Returns the names of the fitted model parameters.
## S4 method for signature 'stantvafit' names(x)## S4 method for signature 'stantvafit' names(x)
x |
The StanTVA fit. |
The list of parameter names and aliases.
## Not run: f <- read_stantva_fit("fit.rds") names(f) ## End(Not run)## Not run: f <- read_stantva_fit("fit.rds") names(f) ## End(Not run)
Obtain a point estimate by maximizing the joint posterior from the StanTVA model.
optimizing(object, ...) ## S4 method for signature 'stantvamodel' optimizing(object, data, init, seed = sample.int(.Machine$integer.max, 1), ...)optimizing(object, ...) ## S4 method for signature 'stantvamodel' optimizing(object, data, init, seed = sample.int(.Machine$integer.max, 1), ...)
object |
The StanTVA model object. |
... |
Further arguments passed to |
data |
The data to which the model should be fitted, usually a |
init |
How to initialize the individual chains, see |
seed |
Seed for random number generation and chain initialization |
A list, representing the maximum-likelihood estimate, see rstan::optimizing().
optimizing(stantvamodel): method
Returns the predictions for latent model parameters.
## S4 method for signature 'stantvafit' predict( object, newdata, variables = names(model@code@df), model = object@stanmodel )## S4 method for signature 'stantvafit' predict( object, newdata, variables = names(model@code@df), model = object@stanmodel )
object |
The StanTVA fit object. |
newdata |
The new data (leave empty to use fitted data). |
variables |
The names of the parameters to predict. |
The predictions.
## Not run: p <- predict(fit, variables = c("C","K")) colMeans(p$C) ## End(Not run)## Not run: p <- predict(fit, variables = c("C","K")) colMeans(p$C) ## End(Not run)
Returns the random effects for a StanTVA fit object.
## S4 method for signature 'stantvafit' ranef(object)## S4 method for signature 'stantvafit' ranef(object)
object |
The StanTVA fit object. |
The fixed effects.
## Not run: random_effects <- ranef(fit) random_effects ## End(Not run)## Not run: random_effects <- ranef(fit) random_effects ## End(Not run)
Reads a StanTVA fit object from one or more files. If multiple files are given, the fits will be combined into a single fit object (e.g., combining separately fitted chains).
read_stantva_fit(files)read_stantva_fit(files)
files |
The file names. |
The StanTVA fit object.
## Not run: fit <- read_stantva_fit(c("chain1.rds", "chain2.rds")) fit ## End(Not run)## Not run: fit <- read_stantva_fit(c("chain1.rds", "chain2.rds")) fit ## End(Not run)
Reads TVA data from a file.
read_tva_data(file, set = LETTERS, ...)read_tva_data(file, set = LETTERS, ...)
file |
The file name. |
set |
The set of items. |
... |
Additional arguments passed to |
A TVA data object, which inherits from data.frame.
## Not run: data <- read_tva_data("data.dat") data ## End(Not run)## Not run: data <- read_tva_data("data.dat") data ## End(Not run)
Draw samples from the model defined by object.
sampling(object, ...) ## S4 method for signature 'stantvamodel' sampling( object, data, init = "random", seed = sample.int(.Machine$integer.max, 1), ..., backend = c("rstan", "cmdstanr", "cmdstanr_mpi"), cpp_options = if (match.arg(backend) == "cmdstanr") list(stan_threads = object@code@config$parallel) else if (match.arg(backend) == "cmdstanr_mpi") list(CXX = "mpicxx", TBB_CXX_TYPE = "gcc", STAN_MPI = TRUE) )sampling(object, ...) ## S4 method for signature 'stantvamodel' sampling( object, data, init = "random", seed = sample.int(.Machine$integer.max, 1), ..., backend = c("rstan", "cmdstanr", "cmdstanr_mpi"), cpp_options = if (match.arg(backend) == "cmdstanr") list(stan_threads = object@code@config$parallel) else if (match.arg(backend) == "cmdstanr_mpi") list(CXX = "mpicxx", TBB_CXX_TYPE = "gcc", STAN_MPI = TRUE) )
object |
The StanTVA model object. |
... |
Further arguments passed to the sampling handler of the specified backend. |
data |
The data to which the model should be fitted, usually a |
init |
How to initialize the individual chains, see |
seed |
Seed for random number generation and chain initialization |
backend |
Which backend to use for fitting (default: |
cpp_options |
Which options to pass to |
Returns a stantva_fit object, which inherits from stanfit, representing the fit of object to data.
sampling(stantvamodel): method
Display the content of the StanTVA code object in the console.
## S4 method for signature 'stantvacode' show(object)## S4 method for signature 'stantvacode' show(object)
object |
The StanTVA code object. |
Returns object invisibly but the function is usually only called for its side effects.
Prints a StanTVA fit object.
## S4 method for signature 'stantvafit' show(object) ## S4 method for signature 'stantvafit' print(x, digits_summary = 2, ...)## S4 method for signature 'stantvafit' show(object) ## S4 method for signature 'stantvafit' print(x, digits_summary = 2, ...)
object |
The StanTVA fit object. |
x |
The StanTVA fit object. |
digits_summary |
The number of significant digits to display in posterior summaries. |
... |
Currently not used. |
Returns x. Usually called for its side effects (printing to the console).
show(stantvafit): Alias
## Not run: print(fit) ## End(Not run)## Not run: print(fit) ## End(Not run)
Prints a StanTVA model object.
## S4 method for signature 'stantvamodel' show(object)## S4 method for signature 'stantvamodel' show(object)
object |
The StanTVA model object. |
The printed object.
## Not run: model <- stantva_model(locations = 4) show(model) ## End(Not run)## Not run: model <- stantva_model(locations = 4) show(model) ## End(Not run)
This function may be used to read an RStan or CmdStan fit from CSV files. Note that you also need to provide the fitted model.
stancsv2stantvafit(csv_file, data, model, contrasts = list())stancsv2stantvafit(csv_file, data, model, contrasts = list())
csv_file |
The CSV file to be read. |
data |
The data to which the model was fitted. |
model |
The fitted model as an StanTVA model or StanTVA code object. |
contrasts |
Any contrasts specified to factors in the data set. |
The StanTVA fit object.
## Not run: data <- read_tva_data("data.dat") model <- stantva_code(locations = 6) fit <- stancsv2stantvafit("chain1.csv", data, model) fit ## End(Not run)## Not run: data <- read_tva_data("data.dat") model <- stantva_code(locations = 6) fit <- stancsv2stantvafit("chain1.csv", data, model) fit ## End(Not run)
Creates a StanTVA model code object.
stantva_code( formula = NULL, locations, task = c("wr", "pr"), regions = list(), C_mode = c("equal", "locations", "regions"), w_mode = c("locations", "regions", "equal"), t0_mode = c("constant", "gaussian", "exponential", "shifted_exponential"), K_mode = c("bernoulli", "free", "binomial", "betabinomial", "hypergeometric", "probit"), max_K = locations, fixed = NULL, parallel = isTRUE(rstan_options("threads_per_chain") > 1L), save_log_lik = FALSE, priors = NULL, sanity_checks = TRUE, debug_neginf_loglik = FALSE )stantva_code( formula = NULL, locations, task = c("wr", "pr"), regions = list(), C_mode = c("equal", "locations", "regions"), w_mode = c("locations", "regions", "equal"), t0_mode = c("constant", "gaussian", "exponential", "shifted_exponential"), K_mode = c("bernoulli", "free", "binomial", "betabinomial", "hypergeometric", "probit"), max_K = locations, fixed = NULL, parallel = isTRUE(rstan_options("threads_per_chain") > 1L), save_log_lik = FALSE, priors = NULL, sanity_checks = TRUE, debug_neginf_loglik = FALSE )
formula |
Optional formulas for nested and hierarchical model parameters. |
locations |
The number of display locations (items). |
task |
The task. Currently implemented: |
regions |
An optional list of groups of display locations (regions). |
C_mode |
The mode/family for the |
w_mode |
The mode/family for the |
t0_mode |
The mode/family for the |
K_mode |
The mode for the |
max_K |
The upper bound of |
fixed |
Named vector or list of parameters that are not to be sampled/fitted but fixed to their respective values. |
parallel |
(logical) Whether to use parallel chains. |
save_log_lik |
(logical) Whether to save the log likelihood (needed for likelihood-based model comparison such as loo). |
priors |
The priors. |
sanity_checks |
(logical) Whether to perform sanity checks. |
debug_neginf_loglik |
(logical) Whether to debug negative infinity log likelihood. |
The StanTVA model code object.
model <- stantva_code(locations = 4, task = "pr") modelmodel <- stantva_code(locations = 4, task = "pr") model
Creates a StanTVA model object.
stantva_model(..., stan_options = list())stantva_model(..., stan_options = list())
... |
Additional arguments passed to |
stan_options |
The Stan options, passed to |
The StanTVA model object.
## Not run: model <- stantva_model(locations = 2, task = "pr") model ## End(Not run)## Not run: model <- stantva_model(locations = 2, task = "pr") model ## End(Not run)
Returns the path to the StanTVA directory.
stantva_path()stantva_path()
This function is used internally by the stantva_model() method.
The path to the StanTVA directory.
path <- stantva_path() pathpath <- stantva_path() path
StanTVA code class
codeThe generated Stan code.
configA list of model configuration parameters, as passed to stantva_code() or stantva_model().
include_pathThe path to the StanTVA includes (usually identical to stantva_path()).
dfThe degrees of freedom of the model parameters.
dimThe dimensions of the model parameters.
versionThe RStanTVA package version that was used to generate this model fit.
priorsPriors for the model parameters.
initializersStan code for the generation of initial samples
StanTVA fit class
stanmodelThe StanTVA model object that was fitted to the data.
dataThe data to which the StanTVA model was fitted.
StanTVA model class
codeThe StanTVA code object that was used to compile this model.
initializersCompiled functions for generation of initial samples
Summarize the distributions of estimated parameters and derived quantities using the posterior draws.
## S4 method for signature 'stantvafit' summary(object, pars, ...)## S4 method for signature 'stantvafit' summary(object, pars, ...)
object |
The RStanTVA fit. |
pars |
(Optional) A character vector of variable names to extract. |
... |
Additional arguments passed to |
See rstan::summary() for details.
## Not run: f <- read_stantva_fit("fit.rds") summary(f, "C_Intercept", probs = c(.025, .975)) ## End(Not run)## Not run: f <- read_stantva_fit("fit.rds") summary(f, "C_Intercept", probs = c(.025, .975)) ## End(Not run)
Applies a function over all stimulus/distractor configurations and averages results.
tva_integrate(fun, locations = max(nS), nS, nD, T, ...)tva_integrate(fun, locations = max(nS), nS, nD, T, ...)
fun |
Function to evaluate. |
locations |
Number of locations. |
nS |
Number of stimuli. |
nD |
Number of distractors. |
T |
Exposure durations. |
... |
Additional arguments passed to |
Numeric vector or matrix of averaged values.
Computes probability of correctly reporting each item.
tva_item_prob( tva_data, tva_model, tva_fit, item = seq_len(ncol(tva_data$S)), log_p = FALSE )tva_item_prob( tva_data, tva_model, tva_fit, item = seq_len(ncol(tva_data$S)), log_p = FALSE )
tva_model |
TVA model object. |
tva_fit |
Fitted TVA model parameters. |
item |
Items to evaluate. |
log_p |
Logical; return log-probabilities if TRUE. |
Matrix of probabilities.
Visualizes expected score as a function of exposure duration.
tva_plot_predicted_score( nS, nD, tva_model, tva_fit, scores = 1:tva_model@code@config$locations, xlim = c(0, 200), n = 101 )tva_plot_predicted_score( nS, nD, tva_model, tva_fit, scores = 1:tva_model@code@config$locations, xlim = c(0, 200), n = 101 )
nS |
Number of stimuli. |
nD |
Number of distractors. |
tva_model |
TVA model. |
tva_fit |
Fitted parameters. |
scores |
Scores to plot. |
xlim |
Range of exposure durations. |
n |
Number of grid points. |
A ggplot object.
Visualizes score probability densities across exposure durations.
tva_plot_score_densities( nS, nD, tva_model, tva_fit, scores = 0:tva_model@code@config$locations, xlim = c(0, 200), n = 101 )tva_plot_score_densities( nS, nD, tva_model, tva_fit, scores = 0:tva_model@code@config$locations, xlim = c(0, 200), n = 101 )
nS |
Number of stimuli. |
nD |
Number of distractors. |
tva_model |
TVA model. |
tva_fit |
Fitted parameters. |
scores |
Scores to plot. |
xlim |
Range of exposure durations. |
n |
Number of grid points. |
locations |
Number of locations. |
A ggplot object.
Computes the expected score based on TVA score probabilities.
tva_predict_score( tva_data, tva_model, tva_fit, scores = 1:tva_model@code@config$locations )tva_predict_score( tva_data, tva_model, tva_fit, scores = 1:tva_model@code@config$locations )
tva_model |
TVA model object. |
tva_fit |
Fitted TVA model parameters. |
Numeric vector of expected scores.
Computes processing rates v for given stimulus (S), distractor (D),
and exposure durations (T) using a fitted TVA model.
tva_processing_rates(tva_data, tva_model, tva_fit)tva_processing_rates(tva_data, tva_model, tva_fit)
tva_model |
TVA model object. |
tva_fit |
Fitted TVA model parameters. |
S |
Integer matrix indicating stimulus presence. |
D |
Integer matrix indicating distractor presence. |
T |
Numeric vector of exposure durations. |
A matrix of processing rates.
True parameters for TVA recovery study
tva_recoverytva_recovery
An object of class grouped_df (inherits from tbl_df, tbl, data.frame) with 11700 rows and 9 columns.
True parameters for TVA recovery study
tva_recovery_true_paramstva_recovery_true_params
An object of class list of length 5.
This function generates by-trial descriptive statistics, see 'Value' below.
tva_report(data)tva_report(data)
data |
The TVA report data as a |
The function returns a transmuted data.frame/tibble with columns condition (copied from data), exposure (copied from data$T), n_items, n_targets, n_distractors, and score (number of correctly reported items).
## Not run: tva_report(tva_recovery)## Not run: tva_report(tva_recovery)
Computes the probability of a response pattern R.
tva_response_prob(tva_data, tva_model, tva_fit, log_p = FALSE)tva_response_prob(tva_data, tva_model, tva_fit, log_p = FALSE)
tva_model |
TVA model object. |
tva_fit |
Fitted TVA model parameters. |
log_p |
Logical; return log-probabilities if TRUE. |
R |
Integer matrix of responses. |
Numeric vector of probabilities.
Calculates the probability of observing specific scores.
tva_score_prob( tva_data, tva_model, tva_fit, score = 0:ncol(tva_data$S), log_p = FALSE )tva_score_prob( tva_data, tva_model, tva_fit, score = 0:ncol(tva_data$S), log_p = FALSE )
tva_model |
TVA model object. |
tva_fit |
Fitted TVA model parameters. |
score |
Integer vector of scores. |
log_p |
Logical; return log-probabilities if TRUE. |
Matrix of probabilities (or log-probabilities).
Simulates trial-wise TVA responses.
tva_simulate_response( tva_data, tva_model, tva_fit, seed = sample.int(.Machine$integer.max, 1) )tva_simulate_response( tva_data, tva_model, tva_fit, seed = sample.int(.Machine$integer.max, 1) )
tva_model |
TVA model object. |
tva_fit |
Fitted TVA model parameters. |
seed |
Random seed. |
Matrix of simulated responses.
Simulates scores by summing simulated responses.
tva_simulate_score( tva_data, tva_model, tva_fit, seed = sample.int(.Machine$integer.max, 1) )tva_simulate_score( tva_data, tva_model, tva_fit, seed = sample.int(.Machine$integer.max, 1) )
tva_model |
TVA model object. |
tva_fit |
Fitted TVA model parameters. |
seed |
Random seed. |
Numeric vector of simulated scores.
Writes a StanTVA fit object to a file.
write_stantva_fit(fit, file, ...)write_stantva_fit(fit, file, ...)
fit |
The StanTVA fit object. |
file |
The file name. |
... |
Additional arguments passed to |
No return value, called for side effects.
## Not run: write_stantva_fit(fit, "fit.rds")## Not run: write_stantva_fit(fit, "fit.rds")
Writes a StanTVA model to a file.
write_stantva_model(model, file = stdout())write_stantva_model(model, file = stdout())
model |
The StanTVA model object. |
file |
The file name. |
No return value, called for side effects.
## Not run: write_stantva_model(model, "model.stan")## Not run: write_stantva_model(model, "model.stan")
Writes TVA data to a file.
write_tva_data(data, file, ...)write_tva_data(data, file, ...)
data |
The TVA data object. |
file |
The file name. |
... |
Additional arguments passed to |
No return value, called for side effects.
## Not run: data <- read_tva_data("data.dat") write_tva_data(data, "data.dat") ## End(Not run)## Not run: data <- read_tva_data("data.dat") write_tva_data(data, "data.dat") ## End(Not run)