/
dirichlet_process_create.R
33 lines (23 loc) · 1.06 KB
/
dirichlet_process_create.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#' Create a Dirichlet Process object
#'
#' Using a previously created Mixing Distribution Object (\code{mdObject}) create a Dirichlet process object.
#' `\code{alphaPriorParameters} sets the parameters for alpha using the shape-rate specification of the gamma distribution.
#'
#' @param x Data
#' @param mdObject Mixing Distribution Object
#' @param alphaPriorParameters Prior parameters for the concentration parameter of the Dirichlet Process
#' @param mhDraws Number of posterior samples to take in the nonconjugate case
#' @export
DirichletProcessCreate <- function(x, mdObject, alphaPriorParameters = c(1, 1), mhDraws=250) {
if (!is.matrix(x)) {
x <- matrix(x, ncol = 1)
}
dpObj <- list(data = x,
mixingDistribution = mdObject,
n = dim(x)[1],
alphaPriorParameters = alphaPriorParameters,
alpha = rgamma(1, alphaPriorParameters[1], alphaPriorParameters[2]),
mhDraws=mhDraws)
class(dpObj) <- append(class(dpObj), c("dirichletprocess", class(mdObject)[-1]))
return(dpObj)
}