#-----------------------------------------------------------------------------------------#
# Example 1: Two-component Univariate Normal Mixture
#-----------------------------------------------------------------------------------------#
# Simulate the data
set.seed(827)
n = 200
prop = 0.3
n1 = rbinom(1, n, prop)
mudif = 1.5
x1 = rnorm(n1, 0, 1)
x2 = rnorm(n - n1, mudif, 1)
x = c(x1, x2)
pm = c(2, 1, 3, 5, 4)
# Use the `mixnorm' function to get the MLE and the estimated classification probabilities
out = mixnorm(x, 2)
# Prepare latent component label
lat = rbind(rep(c(1, 0), times = c(n1, n - n1)),
rep(c(0, 1), times = c(n1, n - n1)))
# Fit the complh/distlat function
clhest = complh(out, lat)
clhest
# Result:
# mean of the first component: -0.1037359,
# mean of the second component: 1.6622397,
# sigma is 0.8137515 for both components, and
# the proportions for the two components are
# 0.3945660 and 0.6054340, respectively.
ditlatest = distlat(out, lat)
#-----------------------------------------------------------------------------------------#
# Example 2: Two-component Multivariate Normal Mixture
#-----------------------------------------------------------------------------------------#
# Simulate the data
n = 400
prop = 0.3
n1 = rbinom(1, n, prop)
pi = c(prop, 1 - prop)
mu1 = 0.5
mu2 = 0.5
mu = matrix(c(0, mu1, 0, mu2), ncol = 2)
pm = c(2, 1, 4, 3, 6, 5)
sigma = diag(c(1, 1))
ini = list(sigma = sigma, mu = mu, pi = pi)
x1 = mvtnorm::rmvnorm(n1, c(0, 0), ini$sigma)
x2 = mvtnorm::rmvnorm(n - n1, c(mu1, mu2), ini$sigma)
x = rbind(x1, x2)
# Use the `mixnorm' function to get the MLE and the estimated classification probabilities
out = mixnorm(x, 2)
# Prepare latent component label
lat = rbind(rep(c(1, 0), times = c(n1, n - n1)),
rep(c(0, 1), times = c(n1, n - n1)))
# Fit the complh/distlat function
clhest = complh(out, lat)
distlatest = distlat(out, lat)
#-----------------------------------------------------------------------------------------#
# Example 3: Three-component Multivariate Normal Mixture
#-----------------------------------------------------------------------------------------#
# Simulate the data
n = 100
pi = c(0.2, 0.3, 0.5)
ns = stats::rmultinom(1, n, pi)
n1 = ns[1]; n2 = ns[2]; n3 = ns[3]
mu1 = 1
mu2 = 1
mu = matrix(c(0, mu1, 2 * mu1, 0, mu2, 2 * mu2), ncol = 2)
sigma = diag(c(1, 1))
ini = list(sigma = sigma, mu = mu, pi = pi)
x1 = mvtnorm::rmvnorm(n1, c(0, 0), ini$sigma)
x2 = mvtnorm::rmvnorm(n2, c(mu1, mu2), ini$sigma)
x3 = mvtnorm::rmvnorm(n3, c(2 * mu1, 2 * mu2), ini$sigma)
x = rbind(x1, x2, x3)
# Use the `mixnorm' function to get the MLE and the estimated classification probabilities
out = mixnorm(x, 3)
# Prepare latent component label
lat = rbind(rep(c(1, 0), times = c(n1, n - n1)),
rep(c(0, 1, 0), times = c(n1, n2, n3)),
rep(c(0, 1), times = c(n - n3, n3)))
# Fit the complh/distlat function
clhest = complh(out, lat)
distlatest = distlat(out, lat)
Run the code above in your browser using DataLab