Skip to contents

This function implements the Expectation-Maximization (EM) algorithm, which iteratively optimizes model parameters by alternating between an E-step (Expectation step) and an M-step (Maximization step). The algorithm is used for parameter estimation in latent variable models, with different model types like "M0", "M1", "M2", and "M3".

Usage

EM(
  data,
  par0,
  types,
  tol1 = 0.001,
  max_iter,
  t_list,
  u_list,
  model,
  n_points = n_points,
  n_samples = 100,
  n_intervals = NULL,
  approx.method,
  period = c(0, 1),
  show_progress = TRUE
)

Arguments

data

Numeric matrix of size `m x n`, where `m` is the number of samples and `n` is the number of variables.

par0

Numeric vector of initial model parameters.

types

Character vector specifying the model types for each variable.

tol1

Numeric value, the tolerance for convergence. The algorithm stops if the parameter change is below this threshold.

max_iter

Integer, the maximum number of iterations for the EM algorithm.

t_list

List, contains the `t` values for each variable (used in Lambda calculations).

u_list

List, contains the `u` values for each variable (used in Lambda calculations).

model

Character, the model type ("M0", "M1", "M2", "M3", etc.) to be used in the estimation.

n_points

Integer, the number of points for Gaussian-Legendre integration.

n_samples

Integer, the number of random samples for Monte Carlo integration.

n_intervals

Integer, the number of intervals for Trapezoidal integration.

approx.method

Character, specifies the numerical method for approximating the expectation ("gl" for Gaussian-Legendre, "mc" for Monte Carlo, or "ti" for Trapezoidal).

period

Numeric vector of length 2, defining the integration limits (e.g., `c(0,1)`).

show_progress

Logical, if TRUE, shows a progress bar during iterations.

Value

A list containing: - `par_re`: A matrix of estimated parameters after the final iteration. - `iter`: The number of iterations performed. - `mse_all`: The mean squared error between parameter estimates in consecutive iterations, used as a convergence criterion.

Details

The E-step calculates the expected values of latent variables, and the M-step updates the model parameters based on these expectations. The function also supports different numerical methods for approximating the expectations (e.g., Monte Carlo, Trapezoidal, and Gaussian-Legendre methods).