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, ZIP expectation for all entries: \(\xi \odot A\), with \(A = \exp( XB + M C^\top + 0.5\, S\, (C\odot C)^\top )\),Sbeing the variational variances).imputedn x pmatrix equal topredictedat 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
set.seed(1)
n <- 30; p <- 5; d <- 2; q <- 2
X <- cbind(1, rnorm(n * p)) # (n*p) x d design
theta <- list(B = matrix(c(1, 0.5)), D = matrix(c(1, -0.5)),
C = matrix(rnorm(p * q, sd = 0.5), p, q))
Y <- Simul(X, theta, list(n = n, p = p, d = d, q = q))$Y
Y[sample(n * p, 20)] <- NA # missing entries
fit <- Miss.ZIPLNPCA(Y, X, q)
str(fit$mStep)
#> List of 3
#> $ gamma: num [1:2, 1] 0.84 -0.615
#> $ beta : num [1:2, 1] 0.87 0.557
#> $ C : num [1:5, 1:2] 0.4939 -0.2381 0.0296 0.1108 -1.037 ...
fit$imputed[is.na(Y)][1:5] # imputed values
#> [1] 3.069936 2.878609 2.428064 1.297661 2.505136