Learn R Programming

psychonetrics (version 0.17.8)

loop_psychonetrics: Parallel loop for psychonetrics

Description

A convenience wrapper for running repeated psychonetrics analyses in parallel. Replaces the typical boilerplate of makeCluster / clusterExport / parLapply / stopCluster with a single function call. Particularly useful for bootstrapping, simulation studies, and cross-validation.

Variables referenced in the expression that exist in the calling environment are automatically exported to parallel workers, so explicit clusterExport calls are not needed in most cases.

Usage

loop_psychonetrics(expr, reps = 1000, nCores = 1,
                   export = character(0),
                   packages = c("psychonetrics", "dplyr"),
                   verbose = TRUE, envir = parent.frame(),
                   seed = NULL)

Value

A list of length reps, where each element is the result of evaluating expr once.

Arguments

expr

An expression (code block) to evaluate on each iteration. Typically enclosed in curly braces {...}. The expression should return a single object (e.g., a psychonetrics model).

reps

Number of replications (default: 1000).

nCores

Number of CPU cores to use. Set to 1 for sequential execution (default), or higher for parallel execution via makePSOCKcluster.

export

Character vector of additional variable names to export to parallel workers. Usually not needed because variables referenced in expr are auto-detected and exported.

packages

Character vector of packages to load on parallel workers (default: c("psychonetrics", "dplyr")).

verbose

Logical; if TRUE (default), shows a progress bar via pbapply.

envir

The environment in which to look up variables for auto-export and to evaluate the expression in sequential mode. Defaults to the calling environment.

seed

Optional integer seed for reproducible random number generation. In parallel mode this calls clusterSetRNGStream so that repeated runs with the same seed and number of cores give identical results; in sequential mode set.seed is used. The default NULL leaves the random number generator untouched (previous behavior).

Details

When nCores > 1, a PSOCK cluster is created and automatically cleaned up when the function exits (even on error). The specified packages are loaded on each worker, and variables (as well as functions defined in the calling environment) referenced in expr are automatically detected and exported.

The replication index i (running from 1 to reps) is visible inside expr, in both sequential and parallel mode.

For bootstrapping, use this function together with bootstrap = "nonparametric" in the model constructor and aggregate_bootstraps to summarize the results.

See Also

aggregate_bootstraps, bootstrap, CIplot

Examples

Run this code
# \donttest{
library(dplyr)
data(NA2020)

# Fit the original model:
model <- ggm(NA2020, estimator = "FIML") %>% runmodel

# Bootstrap with pruning (parallel, 100 reps):
bootstraps <- loop_psychonetrics({
  ggm(NA2020, bootstrap = "nonparametric",
      estimator = "FIML") %>%
    runmodel %>%
    prune(alpha = 0.05)
}, reps = 100, nCores = 2)

# Aggregate and inspect:
boot_agg <- aggregate_bootstraps(
  sample = model %>% prune(alpha = 0.05),
  bootstraps = bootstraps
)

# View results:
parameters(boot_agg)
CIplot(boot_agg, "omega", split0 = TRUE)
# }

Run the code above in your browser using DataLab