tulpa_joint_grid_batch: Fit multiple joint nested-Laplace models through one fused...

View source: R/nested_laplace_joint_batch.R

tulpa_joint_grid_batchR Documentation

Fit multiple joint nested-Laplace models through one fused grid solve

Description

Fits B responses that share one design (arms, latent blocks, outer grid layout, solver settings) and differ only in their responses, their arm dispersions and the nodes of any dispersion axis, through one fused grid solve (cpp_nested_laplace_joint_multi_batch): one design pass per outer- grid cell for all B responses, B block-diagonal Newton solves, instead of B independent fits each repeating the design pass.

Usage

tulpa_joint_grid_batch(fits)

Arguments

fits

A non-empty list of functions of no arguments, each performing one ordinary joint nested-Laplace fit.

Details

Each element of fits is a function of no arguments that performs one ordinary fit (a tulpa_nested_laplace_joint() call, or a consumer front door that makes one). The fits run twice: the first run stops each fit at its main outer-grid solve and keeps the kernel request; the fused solve answers every request at once; the second run replays each fit with its main grid solve served from the fused result. Every other kernel call a fit makes (placement, refinement, diagnostics) runs as it always does, so each returned fit is the object its ordinary fit would have returned, built by the same code.

The fits replay one after another from the random number state the batch was called at, so they draw the same stream the same fits called in sequence would draw; each capture starts from that state too, and a fit whose grid request depended on a draw taken before it is refused at its replay rather than served. Warnings and messages a capture run raises are muffled; the replay raises them again.

Raises a tulpa_grid_batch_ineligible condition (catchable with tryCatch(..., tulpa_grid_batch_ineligible = ...)) when the fits do not share a design or a request uses a setting the fused driver does not carry, and says so on the first fit's capture when the setting is the request's own. The cheap-pass screen is one: it prunes each response's grid to its own kept set, which one fused grid walk cannot, and it is on by default, so fits meant for the fused solve pass control$prune = FALSE. The random number state is restored to where the batch was called before that condition leaves, so a caller that falls back to fitting the responses one at a time draws the stream it would have drawn without the batch.

Value

A list of fit results, one per element of fits, in order.

See Also

tulpa_nested_laplace_joint()

Examples


# The fused solve serves coupled-arm families (a model package's joint
# occupancy-cover fit, say); for designs it does not carry it signals
# `tulpa_grid_batch_ineligible`, and the caller fits one at a time.
set.seed(1)
n_s <- 20L; N <- 100L
s <- sample.int(n_s, N, replace = TRUE); x <- rnorm(N)
nb <- lapply(seq_len(n_s), function(i) setdiff(c(i - 1L, i + 1L), c(0L, n_s + 1L)))
prior <- list(type = "icar", n_spatial_units = n_s,
              adj_row_ptr = as.integer(c(0L, cumsum(lengths(nb)))),
              adj_col_idx = as.integer(unlist(nb)) - 1L,
              n_neighbors = lengths(nb), sigma_grid = c(0.5, 1))
fit_one <- function(y) function() tulpa_nested_laplace_joint(
  responses = list(occ = list(y = y, n_trials = rep(1L, N), X = cbind(1, x),
                              spatial_idx = s, family = "binomial")),
  prior = prior, control = list(progress = FALSE))
fits <- lapply(1:2, function(b) fit_one(rbinom(N, 1, plogis(0.3 * x))))
res <- tryCatch(tulpa_joint_grid_batch(fits),
                tulpa_grid_batch_ineligible = function(e) lapply(fits, function(f) f()))
length(res)


tulpa documentation built on Oct. 19, 2026, 5:09 p.m.