The function dmvnorm()
computes the density of a multivariate normal
distribution.
The function rmvnorm()
samples from a multivariate normal distribution.
The functions with suffix _cpp
perform no input checks, hence are faster.
Usage
dmvnorm_cpp(x, mean, Sigma, log = FALSE)
rmvnorm_cpp(mean, Sigma, log = FALSE)
dmvnorm(x, mean, Sigma, log = FALSE)
rmvnorm(n = 1, mean, Sigma, log = FALSE)
Value
For dmvnorm()
: The density value.
For rmvnorm()
: If n = 1
a vector
of length p
, else
a matrix
of dimension n
times p
with samples as rows.
See also
Other simulation helpers:
correlated_regressors()
,
ddirichlet_cpp()
,
dtnorm_cpp()
,
dwishart_cpp()
,
simulate_markov_chain()
Examples
x <- c(0, 0)
mean <- c(0, 0)
Sigma <- diag(2)
# compute density
dmvnorm(x = x, mean = mean, Sigma = Sigma)
#> [1] 0.1591549
dmvnorm(x = x, mean = mean, Sigma = Sigma, log = TRUE)
#> [1] -1.837877
# sample
rmvnorm(n = 3, mean = mean, Sigma = Sigma)
#> [,1] [,2]
#> [1,] 0.3201427 1.0799257
#> [2,] 0.8303475 -1.2876009
#> [3,] 1.0528909 0.9508922
rmvnorm(mean = mean, Sigma = Sigma, log = TRUE)
#> [1] 5.531714 1.122541