EM Algorithm for Mixtures of Multivariate Normals
mvnormalmixEM.RdReturn EM algorithm output for mixtures of multivariate normal distributions.
Usage
mvnormalmixEM(x, lambda = NULL, mu = NULL, sigma = NULL, k = 2,
arbmean = TRUE, arbvar = TRUE, epsilon = 1e-08,
maxit = 10000, verb = FALSE)Arguments
- x
A matrix of size nxp consisting of the data.
- lambda
Initial value of mixing proportions. Entries should sum to 1. This determines number of components. If NULL, then
lambdais random from uniform Dirichlet and number of components is determined bymu.- mu
A list of size k consisting of initial values for the p-vector mean parameters. If NULL, then the vectors are generated from a normal distribution with mean and standard deviation according to a binning method done on the data. If both
lambdaandmuare NULL, then number of components is determined bysigma.- sigma
A list of size k consisting of initial values for the pxp variance-covariance matrices. If NULL, then
sigmais generated using the data. Iflambda,mu, andsigmaare NULL, then number of components is determined byk.- k
Number of components. Ignored unless
lambda,mu, andsigmaare all NULL.- arbmean
If TRUE, then the component densities are allowed to have different
mus. If FALSE, then a scale mixture will be fit.- arbvar
If TRUE, then the component densities are allowed to have different
sigmas. If FALSE, then a location mixture will be fit.- epsilon
The convergence criterion.
- maxit
The maximum number of iterations.
- verb
If TRUE, then various updates are printed during each iteration of the algorithm.
Value
normalmixEM returns a list of class mixEM with items:
- x
The raw data.
- lambda
The final mixing proportions.
- mu
A list of with the final mean vectors.
- sigma
A list with the final variance-covariance matrices.
- loglik
The final log-likelihood.
- posterior
An nxk matrix of posterior probabilities for observations.
- all.loglik
A vector of each iteration's log-likelihood.
- restarts
The number of times the algorithm restarted due to unacceptable choice of initial values.
- ft
A character vector giving the name of the function.
Examples
##Fitting randomly generated data with a 2-component location mixture of bivariate normals.
set.seed(100)
x.1 <- rmvnorm(40, c(0, 0))
x.2 <- rmvnorm(60, c(3, 4))
X.1 <- rbind(x.1, x.2)
mu <- list(c(0, 0), c(3, 4))
out.1 <- mvnormalmixEM(X.1, arbvar = FALSE, mu = mu,
epsilon = 1e-02)
#> number of iterations= 5
out.1[2:5]
#> $lambda
#> [1] 0.4017365 0.5982635
#>
#> $mu
#> $mu[[1]]
#> [1] -0.3355381 0.3907146
#>
#> $mu[[2]]
#> [1] 2.960001 4.046757
#>
#>
#> $sigma
#> [,1] [,2]
#> [1,] 0.622783783 -0.004242171
#> [2,] -0.004242171 0.931678982
#>
#> $loglik
#> [1] -323.3595
#>
##Fitting randomly generated data with a 2-component scale mixture of bivariate normals.
x.3 <- rmvnorm(40, c(0, 0), sigma =
matrix(c(200, 1, 1, 150), 2, 2))
x.4 <- rmvnorm(60, c(0, 0))
X.2 <- rbind(x.3, x.4)
lambda <- c(0.40, 0.60)
sigma <- list(diag(1, 2), matrix(c(200, 1, 1, 150), 2, 2))
out.2 <- mvnormalmixEM(X.2, arbmean = FALSE,
sigma = sigma, lambda = lambda,
epsilon = 1e-02)
#> number of iterations= 10
out.2[2:5]
#> $lambda
#> [1] 0.6023634 0.3976366
#>
#> $mu
#> [1] -0.6879008 0.1299603
#>
#> $sigma
#> $sigma[[1]]
#> [,1] [,2]
#> [1,] 1.2927836 -0.0824564
#> [2,] -0.0824564 1.1279411
#>
#> $sigma[[2]]
#> [,1] [,2]
#> [1,] 203.61983 -28.96627
#> [2,] -28.96627 199.55985
#>
#>
#> $loglik
#> [1] -566.9803
#>