fastglm_streaming() fits a GLM while keeping only a block of design rows and coefficient-sized summary matrices in memory. Reading successive blocks this way is called streaming. It permits fitting data that cannot be loaded into memory together, at the cost of rereading the observations as the coefficient estimates are updated.

fastglm_streaming(
  chunk_callback,
  n_chunks = NULL,
  family = gaussian(),
  start = NULL,
  method = 2L,
  tol = 1e-08,
  maxit = 100L,
  firth = FALSE
)

Arguments

chunk_callback

a function. Called as chunk_callback(k) for k = 1, ..., n_chunks. Must return a list with elements

X

an n_k x p numeric matrix (chunk of the design matrix).

y

numeric vector of length n_k (response).

weights

optional; numeric vector of length n_k of prior weights.

offset

optional; numeric vector of length n_k of offsets.

Every chunk must have the same number of columns, in the same order. The supplied function, called a callback, is called repeatedly during fitting. It must return the same rows and values each time a block is requested.

n_chunks

integer; the number of chunks to iterate over.

family

a family object describing the error distribution and link. See stats::family().

start

optional length-p numeric vector of starting coefficients.

method

"llt" or 2 for LLT Cholesky (default), "ldlt" or 3 for LDLT Cholesky, or "qr" or 0 for incremental QR. All require full column rank across the complete weighted design. The "auto" choice retains the default Cholesky method for streaming fits.

tol

convergence tolerance on relative deviance change for ordinary fits. Cholesky bias-reduced fits use the largest coefficient update; Gamma-log fits also check the remaining adjusted-score update and relative dispersion change. QR bias-reduced fits require small remaining changes in fitted linear predictors, a small joint information-scaled adjusted score per positive-weight row, and dispersion stationarity where applicable.

maxit

maximum number of IRLS iterations.

firth

logical; if TRUE apply Firth bias reduction using lagged leverages from the previous IRLS iteration. At convergence, the fit solves the same adjusted-score equations as the dense path.

Value

An object with classes "fastglm_stream" and "fastglm" containing coefficient estimates and model summaries as in fastglm(), including coefficients, cov.unscaled, deviance, iter, and converged. The object retains the function used to read the observations. Prediction and fastglm_vcov() call that function again, so the data must remain accessible and unchanged. QR fits also retain a qr list containing a triangular matrix R, column permutation pivot, and column scales scale for the final weighted design. QR bias-reduced fits additionally save remaining.predictor.step, adjusted.score.rms, remaining.dispersion.step, and adjusted.dispersion.score.rms. The dispersion diagnostics are missing for fixed-dispersion families. refined.convergence.verification records whether verification required additional precision for the design.

Details

Iteratively reweighted least squares (IRLS) updates the coefficients by solving a sequence of weighted regressions. The updates and safeguards that shorten unsuccessful steps run in C++. For supported standard families, the distribution calculations also run in C++; the R function is called to supply each block of observations.

Cholesky methods accumulate the weighted crossproduct X' W X. The QR method instead updates a triangular factor as blocks arrive, avoiding crossproducts in initialization, coefficient updates, and covariance calculation. It can retain precision for nearly redundant predictors at the cost of more arithmetic. Rank is assessed after combining all blocks; individual blocks may contain fewer rows than columns or lack factor levels. Globally redundant predictors cause an error.

Standard errors and cov.unscaled use the final weighted design. For QR fits, prediction uncertainty, leverage, robust covariance, and default average-prediction inference use the triangular factor directly. Supplying a covariance matrix explicitly to fastglm_average() uses that matrix. Storage includes the current block and matrices with dimensions determined by the number of coefficients, including a quadratic storage requirement in that number. Neither method retains all design rows.

QR bias-reduced convergence checks evaluate the actual adjusted equations using current working weights and leverage values. Verification requires additional source passes. For poorly conditioned designs, the calculation forms and refactors a better-scaled basis before checking these equations. A small score alone cannot certify convergence: the remaining predictor change and dispersion checks must also pass. Failed step searches retain a failure flag.

Files and databases

fastglm_source_arrow() reads one Parquet file per block, and fastglm_source_dbi() reads the rows returned by one database query per block. fastglm_source() records the factor levels and transformations needed to give the design columns the same meaning in each block.

References

Zietkiewicz, P. and Kosmidis, I. (2024). Bounded-memory adjusted scores estimation in generalized linear models with large data sets. https://arxiv.org/abs/2307.07342.

Examples

# Simulate a "data source" that yields the design matrix in 4 row-blocks.
set.seed(1)
n <- 1000; p <- 5
X <- cbind(1, matrix(rnorm(n * (p - 1)), n, p - 1))
y <- rbinom(n, 1, plogis(X %*% c(0.2, 0.5, -0.3, 0.4, -0.2)))
chunk_size <- 250
chunks <- function(k) {
  idx <- ((k - 1) * chunk_size + 1):(k * chunk_size)
  list(X = X[idx, , drop = FALSE], y = y[idx])
}

fit_stream <- fastglm_streaming(chunks, n_chunks = 4, family = binomial())
fit_full   <- fastglm(X, y, family = binomial(), method = 2)
max(abs(coef(fit_stream) - coef(fit_full)))
#> [1] 2.220446e-16