Skip to contents

Mirrors the C# GaussianMixtureModel class: fits a mixture of k multivariate normals by expectation-maximization, initialized from a k-means fit. It generalizes ml_kmeans() by carrying a full covariance per component rather than only a centre.

Usage

ml_gaussian_mixture(
  x,
  k,
  seed = NULL,
  kmeans_plus_plus = TRUE,
  max_iterations = 1000,
  tolerance = 1e-08
)

Arguments

x

a numeric matrix or data frame with one row per observation, or a numeric vector.

k

the number of mixture components.

seed

integer PRNG seed; NULL (the default) uses the computer clock.

kmeans_plus_plus

use k-means++ initialization for the starting fit. Default TRUE.

max_iterations

the EM iteration cap. Default 1000.

tolerance

the relative convergence tolerance on the log-likelihood. Default 1e-8.

Value

a list with means (k by ncol(x)), sigmas (a length-k list of ncol(x) by ncol(x) covariance matrices), weights, labels (0-based), log_likelihood, and iterations.

Details

labels are 0-BASED, as in ml_kmeans(). log_likelihood is the full normalized mixture log likelihood from the latest expectation step, including when the fit reaches max_iterations.

Examples

x <- c(1, 1.2, 0.9, 1.1, 1.05, 8, 8.3, 7.9, 8.1, 8.2, 1.0, 8.0)
fit <- ml_gaussian_mixture(x, k = 2, seed = 12345)
round(fit$weights, 3)
#> [1] 0.5 0.5