Last chance! 50% off unlimited learning
Sale ends in
These functions provide the density function and a random number
generator for the multivariate normal
distribution with mean equal to mean
and covariance matrix
sigma
.
dmvnorm(x, mean = rep(0, p), sigma = diag(p), log = FALSE, checkSymmetry = TRUE)
rmvnorm(n, mean = rep(0, nrow(sigma)), sigma = diag(length(mean)),
method=c("eigen", "svd", "chol"), pre0.9_9994 = FALSE,
checkSymmetry = TRUE, rnorm = stats::rnorm)
vector or matrix of quantiles. When x
is a matrix, each
row is taken to be a quantile and columns correspond to the number of dimensions,
p
.
number of observations.
mean vector, default is rep(0, length = ncol(x))
.
In ldmvnorm
or
sldmvnorm
, mean
is a matrix with
observation-specific means arranged in columns.
covariance matrix, default is diag(ncol(x))
.
logical; if TRUE
, densities d are given as log(d).
string specifying the matrix decomposition used to
determine the matrix root of sigma
. Possible methods are
eigenvalue decomposition ("eigen"
, default),
singular value decomposition ("svd"
), and
Cholesky decomposition ("chol"
). The
Cholesky is typically fastest, not by much though.
logical; if FALSE
, the output produced in mvtnorm
versions up to 0.9-9993 is reproduced. In 0.9-9994, the
output is organized such that rmvnorm(10,...)
has the
same first ten rows as rmvnorm(100, ...)
when called
with the same seed.
logical; if FALSE
, skip checking whether the
covariance matrix is symmetric or not. This will speed up the
computation but may cause unexpected outputs when ill-behaved
sigma
is provided. The default value is TRUE
.
a function with the same interface as
rnorm
. This allows switching to other generators of
standard normal variables.
dmvnorm
computes the density function of the multivariate normal
specified by mean and the covariance matrix sigma
.
rmvnorm
generates multivariate normal variables.
pmvnorm
, rnorm
, qmvnorm
,
vignette("lmvnorm_src", package = "mvtnorm")
dmvnorm(x=c(0,0))
dmvnorm(x=c(0,0), mean=c(1,1))
sigma <- matrix(c(4,2,2,3), ncol=2)
x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma)
colMeans(x)
var(x)
dS <- dmvnorm(x, sigma = sigma)
### alternative interface
C <- t(chol(sigma))
(C <- ltMatrices(C[lower.tri(C, diag = TRUE)], diag = TRUE))
dC <- exp(ldmvnorm(obs = t(x), chol = C, logLik = FALSE))
all.equal(dS, dC)
x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma, method="chol")
colMeans(x)
var(x)
plot(x)
Run the code above in your browser using DataLab