dirichlet | R Documentation |
Density, distribution function, quantile function and random generation for the dirichlet distribution.
ddirichlet(x, alpha, log = FALSE)
rdirichlet(n, alpha)
x |
vector of quantiles. |
alpha |
dirichlet alpha parameter (numeric vector with positive entries) |
log |
logical; if TRUE, probabilities p are given as log(p). |
n |
number of observations. If length(n) > 1, the length is taken to be the number required. |
The functions (d/r)dirichlet simply wrap those of the
standard (d/r)gamma R implementation, so look at, say,
dgamma
for details.
dgamma
; these functions just wrap the
(d/r)gamma functions.
ddirichlet(c(.5,.5), c(.5, .5))
dbeta(.5, .5, .5)
rdirichlet(3, c(1, 1)) # rows sum to 1
rdirichlet(3, c(1, 1, 1)) # rows sum to 1
rowSums(rdirichlet(3, c(1, 1, 1)))
library(dplyr)
library(ggplot2); theme_set(theme_bw())
samples <- rdirichlet(2e3, rep(1,3))
head(samples)
# project the points into two dimensions
bary_samples <- simp2bary(samples) %>% as.data.frame %>% tbl_df
names(bary_samples) <- c("dim1", "dim2")
qplot(dim1, dim2, data = bary_samples) +
coord_equal()
# visualize a density
f <- function(v) ddirichlet(v, c(20, 10, 5))
mesh <- simplex_mesh(.0025) %>% as.data.frame %>% tbl_df
mesh$f <- mesh %>% apply(1, function(v) f(bary2simp(v)))
p <- ggplot(mesh, aes(x, y)) +
geom_raster(aes(fill = f)) +
coord_equal(xlim = c(0,1), ylim = c(0, .85))
p
points <- rdirichlet(1e3, c(20, 10, 5)) %>% simp2bary %>%
as.data.frame %>% tbl_df %>% rename(x = V1, y = V2)
p + geom_point(data = points, color = "orange", alpha = .3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.