The Dirichlet distribution
rdiric.RdGenerates Dirichlet random variates.
Arguments
- n
number of observations. Note it has two meanings, see
is.matrix.shapebelow.- shape
the shape parameters. These must be positive. If
dimensionis specifed, values are recycled if necessary to lengthdimension.- dimension
the dimension of the distribution. If
dimensionis not numeric then it is taken to belength(shape)(orncol(shape)ifis.matrix.shape == TRUE).- is.matrix.shape
Logical. If
TRUEthenshapemust be a matrix, and thennis no longer the number of rows of the answer but the answer hasn * nrow(shape)rows. IfFALSE(the default) thenshapeis a vector and each of thenrows of the answer haveshapeas its shape parameters.
Details
This function is based on a relationship between the gamma and Dirichlet distribution. Random gamma variates are generated, and then Dirichlet random variates are formed from these.
Value
A n by dimension matrix of Dirichlet random variates.
Each element is positive, and each row will sum to unity.
If shape has names then these will become the column names
of the answer.
References
Lange, K. (2002). Mathematical and Statistical Methods for Genetic Analysis, 2nd ed. New York: Springer-Verlag.
See also
dirichlet is a VGAM family function for
fitting a Dirichlet distribution to data.
Examples
ddata <- data.frame(rdiric(n = 1000, shape = c(y1 = 3, y2 = 1, y3 = 4)))
fit <- vglm(cbind(y1, y2, y3) ~ 1, dirichlet, data = ddata, trace = TRUE)
#> Iteration 1: loglikelihood = 931.05677
#> Iteration 2: loglikelihood = 5343.4329
#> Iteration 3: loglikelihood = 13112.883
#> Iteration 4: loglikelihood = 17923.845
#> Iteration 5: loglikelihood = 18570.779
#> Iteration 6: loglikelihood = 18578.673
#> Iteration 7: loglikelihood = 18578.675
#> Iteration 8: loglikelihood = 18578.675
Coef(fit)
#> shape1 shape2 shape3
#> 3.0075279 0.9897924 3.9894108
coef(fit, matrix = TRUE)
#> loglink(shape1) loglink(shape2) loglink(shape3)
#> (Intercept) 1.101118 -0.0102601 1.383644