R/ma_rmvnorm.R

Defines functions ma_rmvnorm

Documented in ma_rmvnorm

## File Name: ma_rmvnorm.R
## File Version: 0.178

ma_rmvnorm <- function(n, mu=NULL, sigma, eps=1E-10 )
{
    # requireNamespace("MASS")
    p <- ncol(sigma)
    ind <- which( diag(sigma) > eps )
    p2 <- length(ind)
    if (is.null(mu)){
        mu <- rep(0,p)
    }
    mat <- matrix( mu, nrow=n, ncol=p )
    sigma2 <- sigma[ ind, ind, drop=FALSE ]
    V <- chol(sigma2)
    mat2 <- matrix( stats::rnorm( p2*n ), ncol=p2 ) %*% V
    # V <- svd(sigma2)
    # mat2 <- MASS::mvrnorm(n, mu=rep(0,p2), Sigma=sigma2)
    mat[, ind ] <- mat[,ind] + mat2
    return(mat)
}

Try the miceadds package in your browser

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

miceadds documentation built on May 29, 2024, 11:05 a.m.