Dirichlet: The Dirichlet Distribution

Description Usage Arguments Details Value Author(s) Examples

Description

Density function and random number generation for the Dirichlet distribution

Usage

1
2
3
4
5
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.

ddirichlet_R

as ddirichlet, only implemented purely in R.

Author(s)

Marco J. Maier

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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)

Example output

Loading required package: Formula
Loading required package: rgl
Warning messages:
1: In rgl.init(initValue, onlyNULL) : RGL: unable to open X11 display
2: 'rgl_init' failed, running with rgl.useNULL = TRUE 
3: .onUnload failed in unloadNamespace() for 'rgl', details:
  call: fun(...)
  error: object 'rgl_quit' not found 
      [,1] [,2] [,3]
 [1,]    1    5   10
 [2,]    2    5    9
 [3,]    3    5    8
 [4,]    4    5    7
 [5,]    5    5    6
 [6,]    6    5    5
 [7,]    7    5    4
 [8,]    8    5    3
 [9,]    9    5    2
[10,]   10    5    1
      [,1] [,2] [,3]
 [1,] 0.04 0.46 0.50
 [2,] 0.24 0.41 0.35
 [3,] 0.46 0.19 0.35
 [4,] 0.13 0.28 0.59
 [5,] 0.34 0.27 0.39
 [6,] 0.37 0.18 0.45
 [7,] 0.54 0.38 0.08
 [8,] 0.46 0.18 0.35
 [9,] 0.57 0.42 0.02
[10,] 0.69 0.28 0.02
  [1]  9.97134122  0.03127643 13.73662827 13.41730771  2.69165839  7.91949939
  [7]  9.15187862 17.26556115  9.94291600 13.86985520 16.64690968  7.96700150
 [13]  9.65038584 16.88651435 17.33405474  4.06807551  5.10161193  2.34509742
 [19]  8.24794670 14.32721003  1.01758671 16.86243686 14.14693173 11.34409464
 [25]  0.39980126  3.80295661  4.42997776 13.61756955  4.50438834 11.11964458
 [31]  8.34540229  1.54622649  9.07124093 10.02260702 15.65463718  8.75890686
 [37]  5.39633256 15.68449537  9.15529637  1.51943347 12.38933517 12.20819136
 [43] 12.63234509 13.53657298 14.52644943  7.54604129 13.46810597  9.17338667
 [49] 15.95930158  6.40742945 17.63511718  8.23257961  4.55449924  2.30127512
 [55]  9.64903747 14.13404858  4.40077234  6.30987806 13.82573310  4.94725107
 [61] 15.84602280  1.68531474  1.06739152 10.27262858  6.92256286  6.28642978
 [67] 16.06754175  7.37529875 11.26165356 13.38830494  3.75381220  7.26268201
 [73]  7.81685195 10.86282130 12.60684944  0.65535013 12.41455955  3.36690414
 [79] 11.92392515  2.39572425 17.84056764  1.88235283  3.36789654  5.49232364
 [85]  8.71774358 16.06551485 13.00921024  2.73293108  8.45500741  9.51998177
 [91]  1.08007344  4.15442201  8.13744097  2.08098889 15.79670483  7.11725432
 [97] 15.67432454 17.46324346 12.57189958  8.47888680
 [1] 14.1111339  2.1702280  0.9141699  7.1636466 12.1206676  5.9532478
 [7]  3.2378113  3.4792060  6.7685161 36.1492351

DirichletReg documentation built on May 29, 2017, 7:09 p.m.