ZI‑PLN-PCA with missing data (variational fit + imputation)
Source:R/Miss_ZIPLNPCA.R
Miss.ZIPLNPCA.RdFits a zero‑inflated Poisson log‑normal (ZI‑PLN-PCA) latent factor model to a count matrix with missing values. The routine optimizes a variational objective with NLOpt backends and returns parameter estimates, variational parameters and an imputed matrix.
Arguments
- Y
Numeric
n x pcount matrix. May containNA.- X
Numeric design matrix with
n*prows anddcolumns, aligned withvec(Y)(column‑wise vectorization).- q
Integer, latent rank (dimension of the latent space).
- params
Optional list of initial parameters. If
NULL, they are initialized internally viaInit_ZIP_q0(whenq = 0) orInit_ZIP(whenq > 0).- config
Optional list of optimizer controls. If
NULL, a default configuration is used (NLOpt backend, MMA algorithm, sensible tolerances).- tolS
List with numeric bounds for the variational scales
S: elementslowerandupper. Defaults tolist(lower = 0, upper = 1).- tolxi
Numeric tolerance for the Jaakkola‑type \(\xi\) updates in the logistic bound (default
1e-4).
Value
A list with components:
mStepList of model parameters. For
q = 0:gamma(d x 1) andbeta(d x 1). Forq > 0: same plus the loading matrixC(p x q).eStepList of variational parameters. For
q = 0: onlyxi(n x p). Forq > 0:M(n x q),S(n x q) andxi(n x p).predList with predictors and expected counts:
mu(n x p, abundance mean),nu(n x p, zero‑inflation mean),A(n x p, PLN expectation),predicted(n x p, predicted values). Forq = 0,predicted = exp(XB); forq > 0, $$ \mathrm{predicted} = \exp\!\big( XB + M C^\top + 0.5\,(S\odot S)\,(C\odot C)^\top \big). $$imputedn x pmatrix equal toxi * Aat missing entries ofY, andYelsewhere (ZIP expectation).iterInteger, number of iterations.
elboPathNumeric vector of objective (ELBO) values over iterations.
elboFinal ELBO value.
params.initParameters as recorded from the backend call.
monitoringOptimizer diagnostics/logs from the backend.
gradB, gradD, gradC, gradM, gradSGradients of the ELBO with respect to the corresponding parameters, obtained via
Elbo_grad.
Details
This function supports latent rank q >= 0. When q = 0, it
reduces to a ZIP regression (no latent factors). When q > 0, it fits
a ZIP‑PLN factor model.
Internally, a binary mask R = 1_{observed}(Y) is created; a copied
matrix Y.na sets missing entries to zero for objective evaluation.
The optimizer bounds for S are taken from tolS. Ensure that
the vectorization and parameter stacking used in the optimizer are
consistent with the shapes listed above.
Examples
if (FALSE) { # \dontrun{
set.seed(1)
n <- 40; p <- 12; d <- 3
q <- 2
Y <- matrix(rpois(n*p, 2), n, p)
Y[sample(length(Y), 25)] <- NA
# Vectorized design (n*p) x d:
X <- cbind(1, rnorm(n*p), rnorm(n*p))
fit <- Miss.ZIPLNPCA(Y = Y, X = X, q = q)
str(fit$mStep)
str(fit$eStep)
image(log1p(fit$imputed)) # quick look at imputed counts
# Rank-0 (ZIP regression without latent factors):
fit0 <- Miss.ZIPLNPCA(Y = Y, X = X, q = 0)
fit0$eStep$xi[1:3, 1:3]
} # }