# R/MVNorm.R In DIRECT: Bayesian Clustering of Multivariate Data Under the Dirichlet-Process Prior

#### Documented in dMVNormrMVNorm

```rMVNorm <-
function (n, mean = rep(0, nrow(sigma)), sigma = diag(length(mean)), method = c("eigen", "svd", "chol"))
{
if (!isSymmetric(sigma, tol = sqrt(.Machine\$double.eps))) {
stop("sigma must be a symmetric matrix")
}
if (length(mean) != nrow(sigma)) {
stop("mean and sigma have non-conforming size")
}
sigma1 <- sigma
dimnames(sigma1) <- NULL
if (!isTRUE(all.equal(sigma1, t(sigma1)))) {
warning("sigma is numerically not symmetric")
}
method <- match.arg(method)
if (method == "eigen") {
ev <- eigen(sigma, symmetric = TRUE)
if (!all(ev\$values >= -sqrt(.Machine\$double.eps) * abs(ev\$values[1]))) {
warning("sigma is numerically not positive definite")
}
retval <- ev\$vectors %*% diag(sqrt(ev\$values), length(ev\$values)) %*%
t(ev\$vectors)
}
else if (method == "svd") {
sigsvd <- svd(sigma)
if (!all(sigsvd\$d >= -sqrt(.Machine\$double.eps) * abs(sigsvd\$d[1]))) {
warning("sigma is numerically not positive definite")
}
retval <- t(sigsvd\$v %*% (t(sigsvd\$u) * sqrt(sigsvd\$d)))
}
else if (method == "chol") {
retval <- chol(sigma, pivot = TRUE)
o <- order(attr(retval, "pivot"))
retval <- retval[, o]
}
retval <- matrix(rnorm(n * ncol(sigma)), nrow = n) %*% retval
retval <- sweep(retval, 2, mean, "+")
retval
}

dMVNorm <-
function (x, mean, sigma, log = FALSE)
{
if (is.vector(x)) {
x <- matrix(x, ncol = length(x))
}
if (missing(mean)) {
mean <- rep(0, length = ncol(x))
}
if (missing(sigma)) {
sigma <- diag(ncol(x))
}
if (NCOL(x) != NCOL(sigma)) {
stop("x and sigma have non-conforming size")
}
if (!isSymmetric(sigma, tol = sqrt(.Machine\$double.eps))) {
stop("sigma must be a symmetric matrix")
}
if (length(mean) != NROW(sigma)) {
stop("mean and sigma have non-conforming size")
}

logdet = determinant (sigma, logarithm=TRUE)\$modulus
distval <- mahalanobis(x, center = mean, cov = sigma)
logretval <- -(ncol(x) * log(2 * pi) + logdet + distval)/2

#    cat (logdet, ",", logretval, "\n")

if (log)
return(logretval)

exp(logretval)
}
```

## Try the DIRECT package in your browser

Any scripts or data that you put into this service are public.

DIRECT documentation built on May 29, 2017, 10:59 a.m.