Skip to contents

kernel_latent() fits a named random-effect tier with a user-supplied between-unit covariance matrix K. For one named tier, unique = TRUE adds a kernel-structured diagonal \(\boldsymbol\Psi\) companion; the default FALSE is loadings-only. Two or more named kernel_latent() tiers may share the same grouping levels, each with its own K, loading matrix, and latent field. On the TMB engine, two named Gaussian tiers can each use unique = TRUE with a known common observation-error SD supplied through known_sigma_eps to gllvmTMB(), positive weights, and observed same-trait kernel patterns that pass a separation check. Each declared trait needs at least two observed rows. Passing that check does not guarantee precise component estimates: recovery has been poor in some known-truth settings, and intervals remain unvalidated. Mixed-family and Julia-engine requests, as well as separate kernel_dep() tiers, are not supported. Similar kernels can be weakly separable, so use diagnose_kernel_separability() before assigning a distinct interpretation to each component.

A matrix returned by make_cross_kernel() is treated as a fixed supplied covariance. The model does not estimate its association structure or bridge strength. extract_Gamma() returns descriptive point summaries; it does not provide calibrated intervals or establish a causal evolutionary process.

Usage

kernel_latent(unit, K, d = 1, name = "kernel", unique = FALSE, rho = 1)

kernel_indep(unit, K, name = "kernel", common = FALSE, rho = 1)

kernel_dep(unit, K, name = "kernel", rho = 1)

Arguments

unit

Unquoted grouping column whose levels align with rownames(K). When cluster is omitted, gllvmTMB() uses this column, so K is checked against it. An explicit cluster is unchanged.

K

Numeric dense positive-semidefinite covariance or correlation matrix aligned to the grouping levels.

d

Integer latent rank for kernel_latent().

name

Character scalar used as the extractor level, e.g. extract_Sigma(fit, level = "cross").

unique

Logical; TRUE auto-includes the kernel-structured diagonal trait-specific \(\boldsymbol\Psi\) companion. Two named tiers may each use it under the Gaussian fixed-error conditions described above. The default FALSE preserves the loadings-only subset.

rho

Source strength: a number in [0,1] fixes rho * K + (1-rho) * diag(diag(K)) on the legacy-resolved source scale. Omitted or explicit 1 preserves the existing model. NULL estimates strength for one Gaussian structured trait-intercept block with complete replicated multivariate observations and no competing ordinary covariance. Estimated latent terms require rank one and at least four traits. The same strength applies to the entire latent-plus-Psi covariance. This parameter is not a variance-share summary. Fixed attenuation and the admitted Gaussian estimator have implementation checks; recovery is regime-specific.

common

FALSE (default) for a separate dense-kernel variance per trait (kernel_indep); TRUE ties all traits to one shared variance, the canonical spelling for the soft-deprecated kernel_scalar().

Value

A formula marker; never evaluated as a regular R function.

Examples

set.seed(1)
## A known similarity among 30 sites along an environmental gradient; any
## dense positive-semidefinite covariance or correlation matrix can be K.
x <- seq(0, 1, length.out = 30)
K <- exp(-abs(outer(x, x, "-")) / 0.1)
dimnames(K) <- list(paste0("s", 1:30), paste0("s", 1:30))
## Four traits load on one K-correlated latent field and carry their own
## K-correlated deviations, so the site-level trait covariance is
## lambda lambda^T + diag(0.2).
L <- t(chol(K))
lambda <- c(0.8, 0.6, -0.5, 0.4)
site_eff <- outer(drop(L %*% rnorm(30)), lambda) +
  L %*% matrix(rnorm(30 * 4, sd = sqrt(0.2)), 30, 4)
## Long format: four replicate visits per site, one row per (visit, trait).
dat <- expand.grid(trait = factor(paste0("y", 1:4)), visit = 1:4,
                   site = factor(rownames(K), levels = rownames(K)))
dat$obs <- factor(paste(dat$site, dat$visit, sep = "_"))
dat$value <- site_eff[cbind(as.integer(dat$site), as.integer(dat$trait))] +
  rnorm(nrow(dat), sd = 0.4)
fit <- gllvmTMB(
  value ~ 0 + trait +
    kernel_latent(site, K = K, d = 1, name = "gradient", unique = TRUE),
  data = dat, trait = "trait", unit = "obs", cluster = "site"
)
extract_Sigma(fit, level = "gradient")$Sigma
#> Kernel tier 'gradient' is fitted with its dense kernel matrix.
#>            y1         y2         y3         y4
#> y1  0.8311059  0.4569353 -0.4490348  0.3717949
#> y2  0.4569353  0.3571187 -0.2983636  0.2470411
#> y3 -0.4490348 -0.2983636  0.4422823 -0.2427697
#> y4  0.3717949  0.2470411 -0.2427697  0.3366605
## kernel_indep(): trait-specific variances only, on the same K:
fit_indep <- gllvmTMB(
  value ~ 0 + trait + kernel_indep(site, K = K, name = "gradient"),
  data = dat, trait = "trait", unit = "obs", cluster = "site"
)
extract_Sigma(fit_indep, level = "gradient")$Sigma
#> Kernel tier 'gradient' is fitted with its dense kernel matrix.
#>           y1        y2        y3        y4
#> y1 0.8054219 0.0000000 0.0000000 0.0000000
#> y2 0.0000000 0.3431975 0.0000000 0.0000000
#> y3 0.0000000 0.0000000 0.4253156 0.0000000
#> y4 0.0000000 0.0000000 0.0000000 0.3364306