Learn R Programming

LCPA (version 1.0.1)

get.P.Z.Xn.LCA: Compute Posterior Latent Class Probabilities Based on Fixed Parameters

Description

Computes posterior probabilities of latent class membership while simultaneously re-estimating class prevalences via an EM algorithm. Unlike standard posterior computation, this function iteratively updates class prevalences (\(\pi_l\)) using fixed conditional response probabilities (par).

Usage

get.P.Z.Xn.LCA(response, par, 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 categorical responses. Categories are automatically remapped to 0-based integers via adjust.response.

par

3D array (\(L \times I \times K_{\max}\)) of fixed conditional response probabilities where:

  • \(L\) = number of latent classes

  • \(I\) = number of items

  • \(K_{\max}\) = maximum categories across items

par[l, i, k] = \(P(X_i = k-1 \mid Z = l)\) (0-based indexing).

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 class \(l\): $$ P(Z_n = l \mid \mathbf{X}_n) = \frac{\pi_l^{(t)} \prod_{i=1}^I P(X_{ni} = x_{ni} \mid Z_n = l)} {\sum_{k=1}^L \pi_k^{(t)} \prod_{i=1}^I P(X_{ni} = x_{ni} \mid Z_n = k)} $$

M-step

Update class 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. Response categories are standardized to 0-based integers using adjust.response.

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

  3. Numerical stability: Small constants (1e-50) prevent division by zero.

  4. 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.LCA(N = 200, I = 3, L = 2, IQ = 0.85)  # From LCPA
fit <- LCA(data.obj$response, L = 2, method = "EM", nrep = 5)  # From LCPA

P.Z.Xn <- get.P.Z.Xn.LCA(
  response = data.obj$response,
  par = fit$params$par  # Fixed conditional probabilities
)
head(P.Z.Xn)
# }

Run the code above in your browser using DataLab