Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

Dirichlet

The Dirichlet Distribution


Description

Density function and random number generation for the Dirichlet distribution

Usage

rdirichlet(n, alpha)

ddirichlet(x, alpha, log = FALSE, sum.up = FALSE)

ddirichlet_R(x, alpha, log = FALSE, sum.up = FALSE)

Arguments

n

number of random observations to draw

x

a matrix containing observations

alpha

the Dirichlet distribution's parameters. Can be a vector (one set of parameters for all observations) or a matrix (a different set of parameters for each observation), see “Details”

log

if TRUE, logarithmic densities are returned

sum.up

if TRUE, the (log-)likelihood is returned

Details

The Dirichlet distribution is a multidimensional generalization of the Beta distribution where each dimension is governed by an alpha-parameter. Formally this is

D(α)=[Γ(∑α)/∏Γ(α)]∏ y^(α-1)

Usually, alpha is a vector thus the same parameters will be used for all observations. If alpha is a matrix, a complete set of alpha-parameters must be supplied for each observation.

log returns the logarithm of the densities (therefore the log-likelihood) and sum.up returns the product or sum and thereby the likelihood or log-likelihood.

Dirichlet (log-)densities are by default computed using C-routines (ddirichlet_log_vector and ddirichlet_log_matrix), a version only using R is provided by ddirichlet_R. Caution: Although .C() can be used to call the C routines directly, R will crash or produce wrong values, if, e.g., data types are not set properly.

Value

rdirichlet

returns a matrix with random numbers according to the supplied alpha vector or matrix.

ddirichlet

returns a vector of densities (if sum.up = FALSE) or the (log-)likelihood (if sum.up = TRUE) for the given data and alphas. Returns NaN if any element of alpha is <= 0.

ddirichlet_R

as ddirichlet, only implemented purely in R.

Author(s)

Marco J. Maier

Examples

X1 <- rdirichlet(100, c(5, 5, 10))

a.mat <- cbind(1:10, 5, 10:1)
a.mat
X2 <- rdirichlet(10, a.mat)
# note how the probabilities in the first an last column relate to a.mat
round(X2, 2)

ddirichlet(X1, c(5, 5, 10))
ddirichlet(X2, a.mat)

ddirichlet(X2[1:3,], c(1, 2, -1))
ddirichlet(X2[1:3,], c(1, 2, -1), sum.up = TRUE)

DirichletReg

Dirichlet Regression in R

v0.7-0
GPL (>= 2)
Authors
Marco Johannes Maier [cre, aut] (<https://orcid.org/0000-0002-1715-7456>)
Initial release
2020-05-28

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.