Learn R Programming

DirichletReg (version 0.6-2)

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

Value

  • rdirichletreturns a matrix with random numbers according to the supplied alpha vector or matrix.
  • ddirichletreturns a vector of densities (if sum.up = FALSE) or the (log-)likelihood (if sum.up = TRUE) for the given data and alphas.
  • ddirichlet_Ras ddirichlet, only implemented purely in R.

encoding

UTF-8

Details

The Dirichlet distribution is a multidimensional generalization of the Beta distribution where each dimension is governed by an $\alpha$-parameter. Formally this is $$\mathcal{D}(\alpha_i)=\left[\left.\Gamma(\sum_{i}\alpha_i)\right/\prod_i\Gamma(\alpha_i)\right]\prod_{i}y_i^{\alpha_i-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.

Examples

Run this code
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)

Run the code above in your browser using DataLab