This package performs estimation and inference for Gaussian Mixture
Models (GMMs) where the input data may contain missing values. Rather
than imputing missing values before fitting the GMM, this package uses
an extended EM algorithm to obtain the true maximum likelihood estimates
of all model parameters given the observed data. In particular
MGMM performs the following tasks:
Compact Example
set.seed(101)
library(MGMM)
# Parameter settings.
mean_list <- list(
c(1, 1),
c(-1, -1)
)
cov_list <- list(
matrix(c(1, -0.5, -0.5, 1), nrow = 2),
matrix(c(1, 0.5, 0.5, 1), nrow = 2)
)
# Generate data.
data <- rGMM(
n = 1e3,
d = 2,
k = 2,
miss = 0.1,
means = mean_list,
covs = cov_list
)
# Original data.
head(data)
## y1 y2
## 1 1.6512855 2.60621938
## 2 -0.5721069 NA
## 2 -2.0045376 -2.31888263
## 2 -0.6229388 -1.51543968
## 1 2.0258413 0.06921658
## 2 -1.3476380 -1.51915826
# Choose cluster number.
choose_k <- ChooseK(
data,
k0 = 2,
k1 = 4,
boot = 10,
maxit = 10,
eps = 1e-4,
report = TRUE
)
## Cluster size 2 complete. 11 fit(s) succeeded.
## Cluster size 3 complete. 11 fit(s) succeeded.
## Cluster size 4 complete. 11 fit(s) succeeded.
# Cluster number recommendations.
show(choose_k$Choices)
## Metric k_opt Metric_opt k_1se Metric_1se
## 1 BIC 2 3020.6475534 2 3020.6475534
## 2 CHI 4 4.6296476 4 4.6296476
## 3 DBI 2 0.7827537 2 0.7827537
## 4 SIL 2 0.4785446 2 0.4785446
# Estimation.
fit <- FitGMM(
data,
k = 2,
maxit = 10
)
## Objective increment: 11.6
## Objective increment: 2.72
## Objective increment: 2.43
## Objective increment: 2.02
## Objective increment: 1.73
## Objective increment: 1.49
## Objective increment: 1.29
## Objective increment: 1.12
## Objective increment: 0.979
## Objective increment: 0.86
## 10 update(s) performed without reaching tolerance limit.
# Estimated means.
show(fit@Means)
## [[1]]
## y1 y2
## -1.036999 -1.052884
##
## [[2]]
## y1 y2
## 0.9515979 0.9609898
# Estimated covariances.
show(fit@Covariances)
## [[1]]
## y1 y2
## y1 0.9647105 0.5370824
## y2 0.5370824 0.9778174
##
## [[2]]
## y1 y2
## y1 0.9986800 -0.4598704
## y2 -0.4598704 0.9707609
# Cluster assignments.
head(fit@Assignments)
## Assignments Entropy
## 1 2 9.921032e-02
## 2 1 8.258841e-01
## 2 1 5.318370e-07
## 2 1 6.179668e-03
## 1 2 9.640208e-02
## 2 1 3.354176e-04
# Deterministic imputation.
head(fit@Completed)
## y1 y2
## 1 1.6512855 2.60621938
## 2 -0.5721069 -0.15672789
## 2 -2.0045376 -2.31888263
## 2 -0.6229388 -1.51543968
## 1 2.0258413 0.06921658
## 2 -1.3476380 -1.51915826
# Stochastic imputation.
imp <- GenImputation(fit)
head(imp)
## y1 y2
## 1 1.6512855 2.60621938
## 2 -0.5721069 0.87181156
## 2 -2.0045376 -2.31888263
## 2 -0.6229388 -1.51543968
## 1 2.0258413 0.06921658
## 2 -1.3476380 -1.51915826