Learn R Programming

LCPA (version 1.0.1)

get.P.Z.Xn.LPA: Compute Posterior Latent Profile Probabilities Based on Fixed Parameters

Description

Computes posterior probabilities of latent profile membership while simultaneously re-estimating profile prevalences via an EM algorithm. Unlike standard posterior computation, this function iteratively updates profile prevalences (\(\pi_l\)) using fixed profile characteristics (means and covariances).

Usage

get.P.Z.Xn.LPA(response, means, covs, tol = 1e-10, maxiter = 2000, vis = TRUE)

Value

Numeric matrix (\(N \times L\)) of posterior probabilities. Rows sum to 1. Columns named "Class.1", "Class.2", etc.

Arguments

response

Numeric matrix (\(N \times I\)) of continuous responses. Missing values are not allowed. Data should typically be standardized prior to analysis.

means

Numeric matrix (\(L \times I\)) of fixed profile means where:

  • \(L\) = number of latent profiles

  • \(I\) = number of observed variables

Row \(l\) contains profile-specific means \(\boldsymbol{\mu}_l\).

covs

3D array (\(I \times I \times L\)) of fixed profile covariance matrices where:

  • covs[, , l] = profile-specific covariance matrix \(\boldsymbol{\Sigma}_l\)

Each slice must be symmetric and positive definite (after jittering).

tol

Convergence tolerance for absolute change in log-likelihood. Default: 1e-10.

maxiter

Maximum EM iterations. Default: 2000.

vis

Logical: show iteration progress? Default: TRUE.

Algorithm

The function implements an EM algorithm with:

E-step

Compute posterior probabilities for observation \(n\) and profile \(l\): $$ P(Z_n = l \mid \mathbf{x}_n) = \frac{\pi_l^{(t)} \cdot \mathcal{N}(\mathbf{x}_n \mid \boldsymbol{\mu}_l, \boldsymbol{\Sigma}_l)} {\sum_{k=1}^L \pi_k^{(t)} \cdot \mathcal{N}(\mathbf{x}_n \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k)} $$

M-step

Update profile prevalences: $$ \pi_l^{(t+1)} = \frac{1}{N} \sum_{n=1}^N P(Z_n = l \mid \mathbf{x}_n) $$

Convergence is determined by the absolute change in log-likelihood between iterations.

Details

  1. Numerical stability:

    • Covariance matrices are jittered with tol for positive definiteness

    • Log-space computation with log-sum-exp trick

    • Uniform probabilities used as fallback for non-finite densities

  2. Profile prevalences are initialized uniformly (\(\pi_l^{(0)} = 1/L\)).

  3. Termination occurs when:

    • \(|\log L^{(t)} - \log L^{(t-1)}| < \code{tol}\) (log-likelihood change)

    • Maximum iterations reached

Examples

Run this code
# \donttest{
library(LCPA)
set.seed(123)
data.obj <- sim.LPA(N = 300, I = 2, L = 2, constraint = "VV")  # From LCPA
fit <- LPA(data.obj$response, L = 2, method = "EM", nrep = 5)  # From LCPA

P.Z.Xn <- get.P.Z.Xn.LPA(
  response = data.obj$response,
  means = fit$params$means,    # Fixed profile means
  covs = fit$params$covs       # Fixed profile covariances
)
head(P.Z.Xn)
# }

Run the code above in your browser using DataLab