Title: | Build Dirichlet Process Objects for Bayesian Modelling |
---|---|
Description: | Perform nonparametric Bayesian analysis using Dirichlet processes without the need to program the inference algorithms. Utilise included pre-built models or specify custom models and allow the 'dirichletprocess' package to handle the Markov chain Monte Carlo sampling. Our Dirichlet process objects can act as building blocks for a variety of statistical models including and not limited to: density estimation, clustering and prior distributions in hierarchical models. See Teh, Y. W. (2011) <https://www.stats.ox.ac.uk/~teh/research/npbayes/Teh2010a.pdf>, among many other sources. |
Authors: | Gordon J. Ross [aut],
Dean Markwick [aut, cre],
Kees Mulder [ctb] |
Maintainer: | Dean Markwick <[email protected]> |
License: | GPL-3 |
Version: | 0.4.2 |
Built: | 2025-02-15 04:40:24 UTC |
Source: | https://github.com/dm13450/dirichletprocess |
Create a Beta mixture with zeros at the boundaries.
BetaMixture2Create(priorParameters = 2, mhStepSize = c(1, 1), maxT = 1)
BetaMixture2Create(priorParameters = 2, mhStepSize = c(1, 1), maxT = 1)
priorParameters |
The prior parameters for the base measure. |
mhStepSize |
The Metropolis Hastings step size. A numeric vector of length 2. |
maxT |
The upper bound of the Beta distribution. Defaults to 1 for the standard Beta distribution. |
A mixing distribution object.
See DirichletProcessBeta
for the default prior and hyper prior distributions.
BetaMixtureCreate( priorParameters = c(2, 8), mhStepSize = c(1, 1), maxT = 1, hyperPriorParameters = c(1, 0.125) )
BetaMixtureCreate( priorParameters = c(2, 8), mhStepSize = c(1, 1), maxT = 1, hyperPriorParameters = c(1, 0.125) )
priorParameters |
The prior parameters for the base measure. |
mhStepSize |
The Metropolis Hastings step size. A numeric vector of length 2. |
maxT |
The upper bound of the Beta distribution. Defaults to 1 for the standard Beta distribution. |
hyperPriorParameters |
The parameters for the hyper prior. |
A mixing distribution object.
Add burn-in to a dirichletprocess object
Burn(dpobj, niter)
Burn(dpobj, niter)
dpobj |
A dirichletprocess object. |
niter |
Number of iterations to burn. |
A dirichletprocess object where all chain objects have the first
niter
iterations are removed.
dp <- Fit(DirichletProcessGaussian(rnorm(10)), 100) DiagnosticPlots(dp) burned_dp <- Burn(dp, 50) DiagnosticPlots(burned_dp)
dp <- Fit(DirichletProcessGaussian(rnorm(10)), 100) DiagnosticPlots(dp) burned_dp <- Burn(dp, 50) DiagnosticPlots(burned_dp)
Using a fitted Dirichlet process object include new data. The new data will be assigned to the best fitting cluster for each point.
ChangeObservations(dpobj, newData)
ChangeObservations(dpobj, newData)
dpobj |
The Dirichlet process object. |
newData |
New data to be included |
Changed Dirichlet process object
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- ChangeObservations(dp, rnorm(10))
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- ChangeObservations(dp, rnorm(10))
Update the cluster assignment for each data point.
ClusterComponentUpdate(dpObj) ## S3 method for class 'conjugate' ClusterComponentUpdate(dpObj) ## S3 method for class 'hierarchical' ClusterComponentUpdate(dpObj)
ClusterComponentUpdate(dpObj) ## S3 method for class 'conjugate' ClusterComponentUpdate(dpObj) ## S3 method for class 'hierarchical' ClusterComponentUpdate(dpObj)
dpObj |
Dirichlet Process object |
Dirichlet process object with update components.
dp <- DirichletProcessGaussian(rnorm(10)) dp <- ClusterComponentUpdate(dp)
dp <- DirichletProcessGaussian(rnorm(10)) dp <- ClusterComponentUpdate(dp)
Given a fitted Dirichlet process object and some new data use this function to predict what clusters the new data belong to and associated cluster parameters.
ClusterLabelPredict(dpobj, newData)
ClusterLabelPredict(dpobj, newData)
dpobj |
Fitted Dirichlet Process |
newData |
New data to have cluster labels predicted. |
A list of the predicted cluster labels of some new unseen data.
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) newY <- rnorm(10, 1) pred <- ClusterLabelPredict(dp, newY)
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) newY <- rnorm(10, 1) pred <- ClusterLabelPredict(dp, newY)
Update the parameters of each individual cluster using all the data assigned to the particular cluster. A sample is taken from the posterior distribution using a direct sample if the mixing distribution is conjugate or the Metropolis Hastings algorithm for non-conjugate mixtures.
ClusterParameterUpdate(dpObj)
ClusterParameterUpdate(dpObj)
dpObj |
Dirichlet process object |
Dirichlet process object with update cluster parameters
dp <- DirichletProcessGaussian(rnorm(10)) dp <- ClusterParameterUpdate(dp)
dp <- DirichletProcessGaussian(rnorm(10)) dp <- ClusterParameterUpdate(dp)
Plot several diagnostic plots for dirichletprocess objects. Because the dimension of the dirichletprocess mixture is constantly changing, it is not simple to create meaningful plots of the sampled parameters. Therefore, the plots focus on the likelihood, alpha, and the number of clusters.
DiagnosticPlots(dpobj, gg = FALSE) AlphaTraceplot(dpobj, gg = TRUE) AlphaPriorPosteriorPlot( dpobj, prior_color = "#2c7fb8", post_color = "#d95f02", gg = TRUE ) ClusterTraceplot(dpobj, gg = TRUE) LikelihoodTraceplot(dpobj, gg = TRUE)
DiagnosticPlots(dpobj, gg = FALSE) AlphaTraceplot(dpobj, gg = TRUE) AlphaPriorPosteriorPlot( dpobj, prior_color = "#2c7fb8", post_color = "#d95f02", gg = TRUE ) ClusterTraceplot(dpobj, gg = TRUE) LikelihoodTraceplot(dpobj, gg = TRUE)
dpobj |
A dirichletprocess object that was fit. |
gg |
Logical; whether to create a ggplot or base R plot (if |
prior_color |
For |
post_color |
For |
If gg = TRUE
, a ggplot2 object. Otherwise, nothing is returned
and a base plot is plotted.
AlphaTraceplot()
: Trace plot of alpha.
AlphaPriorPosteriorPlot()
: Plot of the prior and posterior of alpha.
ClusterTraceplot()
: Trace plot of the number of clusters.
LikelihoodTraceplot()
: Trace plot of the likelihood of the data for
each iteration.
dp <- Fit(DirichletProcessGaussian(rnorm(10)), 100) DiagnosticPlots(dp)
dp <- Fit(DirichletProcessGaussian(rnorm(10)), 100) DiagnosticPlots(dp)
Create a hidden Markov model where the data is believed to be generated from the mixing object distribution.
DirichletHMMCreate(x, mdobj, alpha, beta)
DirichletHMMCreate(x, mdobj, alpha, beta)
x |
Data to be modelled |
mdobj |
Mixing disitribution object |
alpha |
Alpha parameter |
beta |
Beta parameter |
Create, fit and take posterior samples from a Dirichlet process.
Create a Dirichlet process object using the mean and scale parameterisation of the Beta distribution bounded on .
DirichletProcessBeta( y, maxY, g0Priors = c(2, 8), alphaPrior = c(2, 4), mhStep = c(1, 1), hyperPriorParameters = c(1, 0.125), verbose = TRUE, mhDraws = 250 )
DirichletProcessBeta( y, maxY, g0Priors = c(2, 8), alphaPrior = c(2, 4), mhStep = c(1, 1), hyperPriorParameters = c(1, 0.125), verbose = TRUE, mhDraws = 250 )
y |
Data for which to be modelled. |
maxY |
End point of the data |
g0Priors |
Prior parameters of the base measure |
alphaPrior |
Prior parameters for the concentration parameter. See also |
mhStep |
Step size for Metropolis Hastings sampling algorithm. |
hyperPriorParameters |
Hyper-prior parameters for the prior distributions of the base measure parameters |
verbose |
Logical, control the level of on screen output. |
mhDraws |
Number of Metropolis-Hastings samples to perform for each cluster update. |
.
The parameter also has a prior distribution
if the user selects
Fit(...,updatePrior=TRUE)
.
Dirichlet process object
Create a Dirichlet process object using the mean and scale parameterisation of the Beta distribution bounded on .
The Pareto distribution is used as a prior on the scale parameter to ensure that the likelihood is 0 at the boundaries.
DirichletProcessBeta2( y, maxY, g0Priors = 2, alphaPrior = c(2, 4), mhStep = c(1, 1), verbose = TRUE, mhDraws = 250 )
DirichletProcessBeta2( y, maxY, g0Priors = 2, alphaPrior = c(2, 4), mhStep = c(1, 1), verbose = TRUE, mhDraws = 250 )
y |
Data for which to be modelled. |
maxY |
End point of the data |
g0Priors |
Prior parameters of the base measure |
alphaPrior |
Prior parameters for the concentration parameter. See also |
mhStep |
Step size for Metropolis Hastings sampling algorithm. |
verbose |
Logical, control the level of on screen output. |
mhDraws |
Number of Metropolis-Hastings samples to perform for each cluster update. |
.
Dirichlet process object
Using a previously created Mixing Distribution Object (mdObject
) create a Dirichlet process object.
'alphaPriorParameters
sets the parameters for alpha using the shape-rate specification of the gamma distribution.
DirichletProcessCreate( x, mdObject, alphaPriorParameters = c(1, 1), mhDraws = 250 )
DirichletProcessCreate( x, mdObject, alphaPriorParameters = c(1, 1), mhDraws = 250 )
x |
Data |
mdObject |
Mixing Distribution Object |
alphaPriorParameters |
Prior parameters for the concentration parameter of the Dirichlet Process |
mhDraws |
Number of posterior samples to take in the nonconjugate case |
This is the constructor function to produce a dirichletprocess
object with a Exponential mixture kernel with unknown rate.
The base measure is a Gamma distribution that is conjugate to the posterior distribution.
DirichletProcessExponential(y, g0Priors = c(0.01, 0.01), alphaPriors = c(2, 4))
DirichletProcessExponential(y, g0Priors = c(0.01, 0.01), alphaPriors = c(2, 4))
y |
Data |
g0Priors |
Base Distribution Priors |
alphaPriors |
Alpha prior parameters. See |
Dirichlet process object
This is the constructor function to produce a dirichletprocess
object with a Gaussian mixture kernel with unknown mean and variance.
The base measure is a Normal Inverse Gamma distribution that is conjugate to the posterior distribution.
DirichletProcessGaussian(y, g0Priors = c(0, 1, 1, 1), alphaPriors = c(2, 4))
DirichletProcessGaussian(y, g0Priors = c(0, 1, 1, 1), alphaPriors = c(2, 4))
y |
Data |
g0Priors |
Base Distribution Priors |
alphaPriors |
Alpha prior parameters. See |
We recommend scaling your data to zero mean and unit variance for quicker convergence.
Dirichlet process object
Create a Dirichlet Mixture of the Gaussian Distribution with fixed variance.
DirichletProcessGaussianFixedVariance( y, sigma, g0Priors = c(0, 1), alphaPriors = c(2, 4) )
DirichletProcessGaussianFixedVariance( y, sigma, g0Priors = c(0, 1), alphaPriors = c(2, 4) )
y |
Data. |
sigma |
The fixed variance |
g0Priors |
Base Distribution Priors. |
alphaPriors |
Prior parameter distributions for the alpha concentration parameter. |
Dirichlet process object
Create a Hierarchical Dirichlet Mixture of Beta Distributions
DirichletProcessHierarchicalBeta( dataList, maxY, priorParameters = c(2, 8), hyperPriorParameters = c(1, 0.125), gammaPriors = c(2, 4), alphaPriors = c(2, 4), mhStepSize = c(0.1, 0.1), numSticks = 50, mhDraws = 250 )
DirichletProcessHierarchicalBeta( dataList, maxY, priorParameters = c(2, 8), hyperPriorParameters = c(1, 0.125), gammaPriors = c(2, 4), alphaPriors = c(2, 4), mhStepSize = c(0.1, 0.1), numSticks = 50, mhDraws = 250 )
dataList |
List of data for each separate Dirichlet mixture object |
maxY |
Maximum value for the Beta distribution. |
priorParameters |
Prior Parameters for the top level base distribution. |
hyperPriorParameters |
Hyper prior parameters for the top level base distribution. |
gammaPriors |
Prior parameters for the top level concentration parameter. |
alphaPriors |
Prior parameters for the individual parameters. |
mhStepSize |
Metropolis Hastings jump size. |
numSticks |
Truncation level for the Stick Breaking formulation. |
mhDraws |
Number of Metropolis-Hastings samples to perform for each cluster update. |
dpobjlist A Hierarchical Dirichlet Process object that can be fitted, plotted etc.
Create a Hierarchical Dirichlet Mixture of semi-conjugate Multivariate Normal Distributions
DirichletProcessHierarchicalMvnormal2( dataList, g0Priors, gammaPriors = c(2, 4), alphaPriors = c(2, 4), numSticks = 50, numInitialClusters = 1, mhDraws = 250 )
DirichletProcessHierarchicalMvnormal2( dataList, g0Priors, gammaPriors = c(2, 4), alphaPriors = c(2, 4), numSticks = 50, numInitialClusters = 1, mhDraws = 250 )
dataList |
List of data for each separate Dirichlet mixture object |
g0Priors |
Prior Parameters for the top level base distribution. |
gammaPriors |
Prior parameters for the top level concentration parameter. |
alphaPriors |
Prior parameters for the individual parameters. |
numSticks |
Truncation level for the Stick Breaking formulation. |
numInitialClusters |
Number of clusters to initialise with. |
mhDraws |
Number of Metropolis-Hastings samples to perform for each cluster update. |
dpobjlist A Hierarchical Dirichlet Process object that can be fitted, plotted etc.
DirichletProcessMvnormal( y, g0Priors, alphaPriors = c(2, 4), numInitialClusters = 1 )
DirichletProcessMvnormal( y, g0Priors, alphaPriors = c(2, 4), numInitialClusters = 1 )
y |
Data |
g0Priors |
Prior parameters for the base distribution. |
alphaPriors |
Alpha prior parameters. See |
numInitialClusters |
Number of clusters to initialise with. |
Create a Dirichlet mixture of multivariate normal distributions with semi-conjugate prior.
DirichletProcessMvnormal2(y, g0Priors, alphaPriors = c(2, 4))
DirichletProcessMvnormal2(y, g0Priors, alphaPriors = c(2, 4))
y |
Data |
g0Priors |
Prior parameters for the base distribution. |
alphaPriors |
Alpha prior parameters. See |
The likelihood is parameterised as .
The base measure is a Uniform Inverse Gamma Distribution.
This is a semi-conjugate distribution. The cluster parameter a is updated using the Metropolis Hastings algorithm an analytical posterior exists for b.
DirichletProcessWeibull( y, g0Priors, alphaPriors = c(2, 4), mhStepSize = c(1, 1), hyperPriorParameters = c(6, 2, 1, 0.5), verbose = FALSE, mhDraws = 250 )
DirichletProcessWeibull( y, g0Priors, alphaPriors = c(2, 4), mhStepSize = c(1, 1), hyperPriorParameters = c(6, 2, 1, 0.5), verbose = FALSE, mhDraws = 250 )
y |
Data. |
g0Priors |
Base Distribution Priors. |
alphaPriors |
Prior for the concentration parameter. |
mhStepSize |
Step size for the new parameter in the Metropolis Hastings algorithm. |
hyperPriorParameters |
Hyper prior parameters. |
verbose |
Set the level of screen output. |
mhDraws |
Number of Metropolis-Hastings samples to perform for each cluster update. |
Dirichlet process object
Kottas, A. (2006). Nonparametric Bayesian survival analysis using mixtures of Weibull distributions. Journal of Statistical Planning and Inference, 136(3), 578-596.
See DirichletProcessExponential
for details on the base measure.
ExponentialMixtureCreate(priorParameters = c(0.01, 0.01))
ExponentialMixtureCreate(priorParameters = c(0.01, 0.01))
priorParameters |
Prior parameters for the base measure. |
Mixing distribution object
Using Neal's algorithm 4 or 8 depending on conjugacy the sampling procedure for a Dirichlet process is carried out.
Lists of both cluster parameters, weights and the sampled concentration values are included in the fitted dpObj
.
When update_prior
is set to TRUE
the parameters of the base measure are also updated.
Fit(dpObj, its, updatePrior = FALSE, progressBar = TRUE)
Fit(dpObj, its, updatePrior = FALSE, progressBar = TRUE)
dpObj |
Initialised Dirichlet Process object |
its |
Number of iterations to use |
updatePrior |
Logical flag, defaults to |
progressBar |
Logical flag indicating whether to display a progress bar. |
A Dirichlet Process object with the fitted cluster parameters and labels.
Neal, R. M. (2000). Markov chain sampling methods for Dirichlet process mixture models. Journal of computational and graphical statistics, 9(2), 249-265.
Fit a Hidden Markov Dirichlet Process Model
## S3 method for class 'markov' Fit(dpObj, its, updatePrior = F, progressBar = F)
## S3 method for class 'markov' Fit(dpObj, its, updatePrior = F, progressBar = F)
dpObj |
Initialised Dirichlet Process object |
its |
Number of iterations to use |
updatePrior |
Logical flag, defaults to |
progressBar |
Logical flag indicating whether to display a progress bar. |
A Dirichlet Process object with the fitted cluster parameters and states.
Create a Gaussian Mixing Distribution with fixed variance.
GaussianFixedVarianceMixtureCreate(priorParameters = c(0, 1), sigma)
GaussianFixedVarianceMixtureCreate(priorParameters = c(0, 1), sigma)
priorParameters |
The prior parameters for the base measure. |
sigma |
The fixed variance of the model. |
A mixing distribution object.
See DirichletProcessGaussian
for details on the base measure.
GaussianMixtureCreate(priorParameters = c(0, 1, 1, 1))
GaussianMixtureCreate(priorParameters = c(0, 1, 1, 1))
priorParameters |
Prior parameters for the base measure. |
Mixing distribution object
Update the parameters of the hierarchical Dirichlet process object.
GlobalParameterUpdate(dpobjlist)
GlobalParameterUpdate(dpobjlist)
dpobjlist |
List of Dirichlet Process objects. |
Create a Mixing Object for a hierarchical Beta Dirichlet process object.
HierarchicalBetaCreate( n, priorParameters, hyperPriorParameters, alphaPrior, maxT, gammaPrior, mhStepSize, num_sticks )
HierarchicalBetaCreate( n, priorParameters, hyperPriorParameters, alphaPrior, maxT, gammaPrior, mhStepSize, num_sticks )
n |
Number of data sets |
priorParameters |
The prior parameters for the top level base distribution. |
hyperPriorParameters |
Hyper prior parameters for the top level base distribution. |
alphaPrior |
Individual level concentration parameter priors. |
maxT |
Bounding value of the data. |
gammaPrior |
Concentration parameter for the top level priors. |
mhStepSize |
Metropolis Hastings step size for the posterior drawing. |
num_sticks |
Number of stick breaking values to use. |
A mixing distribution object.
Create a Mixing Object for a hierarchical semi-conjugate Multivariate Normal Dirichlet process object.
HierarchicalMvnormal2Create( n, priorParameters, alphaPrior, gammaPrior, num_sticks )
HierarchicalMvnormal2Create( n, priorParameters, alphaPrior, gammaPrior, num_sticks )
n |
Number of data sets |
priorParameters |
The prior parameters for the top level base distribution. |
alphaPrior |
Individual level concentration parameter priors. |
gammaPrior |
Concentration parameter for the top level priors. |
num_sticks |
Number of stick breaking values to use. |
A mixing distribution object.
Initialise a Dirichlet process object by assigning all the data points to a single cluster with a posterior or prior draw for parameters.
Initialise( dpObj, posterior = TRUE, m = 3, verbose = TRUE, numInitialClusters = 1 )
Initialise( dpObj, posterior = TRUE, m = 3, verbose = TRUE, numInitialClusters = 1 )
dpObj |
A Dirichlet process object. |
posterior |
TRUE/FALSE value for whether the cluster parameters should be from the posterior. If false then the values are from the prior. |
m |
Number of auxiliary variables to use for a non-conjugate mixing distribution. Defaults to m=3. See |
verbose |
Logical flag indicating whether to output the acceptance ratio for non-conjugate mixtures. |
numInitialClusters |
Number of clusters to initialise with. |
A Dirichlet process object that has initial cluster allocations.
Evaluate the Likelihood of some data for some parameter
.
## S3 method for class 'beta' Likelihood(mdObj, x, theta) ## S3 method for class 'beta2' Likelihood(mdObj, x, theta) ## S3 method for class 'exponential' Likelihood(mdObj, x, theta) Likelihood(mdObj, x, theta) ## S3 method for class 'mvnormal' Likelihood(mdObj, x, theta) ## S3 method for class 'mvnormal2' Likelihood(mdObj, x, theta) ## S3 method for class 'normalFixedVariance' Likelihood(mdObj, x, theta) ## S3 method for class 'normal' Likelihood(mdObj, x, theta)
## S3 method for class 'beta' Likelihood(mdObj, x, theta) ## S3 method for class 'beta2' Likelihood(mdObj, x, theta) ## S3 method for class 'exponential' Likelihood(mdObj, x, theta) Likelihood(mdObj, x, theta) ## S3 method for class 'mvnormal' Likelihood(mdObj, x, theta) ## S3 method for class 'mvnormal2' Likelihood(mdObj, x, theta) ## S3 method for class 'normalFixedVariance' Likelihood(mdObj, x, theta) ## S3 method for class 'normal' Likelihood(mdObj, x, theta)
mdObj |
Mixing Distribution |
x |
Data |
theta |
Parameters of distribution |
Likelihood of the data
Calculate the likelihood of each data point with its parameter.
LikelihoodDP(dpobj)
LikelihoodDP(dpobj)
dpobj |
The dirichletprocess object on which to calculate the likelihood. |
Collecting the fitted cluster parameters and number of datapoints associated with each parameter a likelihood can be calculated. Each cluster is weighted by the number of datapoints assigned.
LikelihoodFunction(dpobj, ind)
LikelihoodFunction(dpobj, ind)
dpobj |
Dirichlet process object. |
ind |
The iteration number. Defaults to the last iteration. |
A function f(x) that represents the Likelihood of the dpobj.
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) f <- LikelihoodFunction(dp) plot(f(-2:2))
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) f <- LikelihoodFunction(dp) plot(f(-2:2))
The constructor function for a mixing distribution object. Use this function to prepare an object for use with the appropriate distribution functions.
MixingDistribution( distribution, priorParameters, conjugate, mhStepSize = NULL, hyperPriorParameters = NULL )
MixingDistribution( distribution, priorParameters, conjugate, mhStepSize = NULL, hyperPriorParameters = NULL )
distribution |
The name of the distribution mixture |
priorParameters |
The prior parameters |
conjugate |
Whether the prior is conjugate to the Likelihood. |
mhStepSize |
The scale of the proposal parameter for the Metropolis Hastings algorithm. Not needed for conjugate mixtures. |
hyperPriorParameters |
Vector of hyperPriorParameters for the distribution. |
Create a multivariate normal mixing distribution with semi conjugate prior
Mvnormal2Create(priorParameters)
Mvnormal2Create(priorParameters)
priorParameters |
The prior parameters for the Multivariate Normal. |
Create a multivariate normal mixing distribution
MvnormalCreate(priorParameters)
MvnormalCreate(priorParameters)
priorParameters |
The prior parameters for the Multivariate Normal. |
Used to find suitable starting parameters for nonconjugate mixtures. For some mixing distributions this hasn't been implemented yet.
## S3 method for class 'beta' PenalisedLikelihood(mdObj, x) PenalisedLikelihood(mdObj, x) ## Default S3 method: PenalisedLikelihood(mdObj, x)
## S3 method for class 'beta' PenalisedLikelihood(mdObj, x) PenalisedLikelihood(mdObj, x) ## Default S3 method: PenalisedLikelihood(mdObj, x)
mdObj |
Mixing distribution object |
x |
Data |
For a univariate Dirichlet process plot the density of the data with the posterior distribution and credible intervals overlayed. For multivariate data the first two columns of the data are plotted with the data points coloured by their cluster labels. The additional arguments are not used for multivariate data.
## S3 method for class 'dirichletprocess' plot(x, ...) plot_dirichletprocess_univariate( x, likelihood = FALSE, single = TRUE, data_fill = "black", data_method = "density", data_bw = NULL, ci_size = 0.05, xgrid_pts = 100, quant_pts = 100, xlim = NA ) plot_dirichletprocess_multivariate(x)
## S3 method for class 'dirichletprocess' plot(x, ...) plot_dirichletprocess_univariate( x, likelihood = FALSE, single = TRUE, data_fill = "black", data_method = "density", data_bw = NULL, ci_size = 0.05, xgrid_pts = 100, quant_pts = 100, xlim = NA ) plot_dirichletprocess_multivariate(x)
x |
Dirichlet Process Object to plot |
... |
Further arguments, currently ignored. |
likelihood |
Logical, indicating whether to plot the likelihood from the dpobj. |
single |
Logical, indicating whether to draw the posterior from the last iteration or use the full cluster sequence. |
data_fill |
Passed to 'fill' in the data geom, for example a color. Defaults to "black". |
data_method |
A string containing either "density" (default), "hist"/"histogram", or "none". Data is plotted according to this method. |
data_bw |
Bandwith to be passed either as the binwidth of
|
ci_size |
Numeric, the interval size to use. Defaults to .05. |
xgrid_pts |
Integer, the number of points on the x-axis to evaluate. |
quant_pts |
Integer, the number of posterior functions to use to obtain the posterior and its interval. |
xlim |
Default NA. If a vector of length two, the limits on the x-axis
of the plot. If |
A ggplot object.
dp <- DirichletProcessGaussian(c(rnorm(50, 2, .2), rnorm(60))) dp <- Fit(dp, 100) plot(dp) plot(dp, likelihood = TRUE, data_method = "hist", data_fill = rgb(.5, .5, .8, .6), data_bw = .3)
dp <- DirichletProcessGaussian(c(rnorm(50, 2, .2), rnorm(60))) dp <- Fit(dp, 100) plot(dp) plot(dp, likelihood = TRUE, data_method = "hist", data_fill = rgb(.5, .5, .8, .6), data_bw = .3)
Using the stick breaking representation the user can draw the posterior clusters and weights for a fitted Dirichlet Process.
See also PosteriorFunction
.
PosteriorClusters(dpobj, ind)
PosteriorClusters(dpobj, ind)
dpobj |
Fitted Dirichlet process |
ind |
Index for which the posterior will be drawn from. Defaults to the last iteration of the fit. |
A list with the weights and cluster parameters that form the posterior of the Dirichlet process.
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) postClusters <- PosteriorClusters(dp)
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) postClusters <- PosteriorClusters(dp)
Draw from the posterior distribution
## S3 method for class 'exponential' PosteriorDraw(mdObj, x, n = 1, ...) PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'mvnormal' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'mvnormal2' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'normalFixedVariance' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'normal' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'weibull' PosteriorDraw(mdObj, x, n = 100, ...)
## S3 method for class 'exponential' PosteriorDraw(mdObj, x, n = 1, ...) PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'mvnormal' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'mvnormal2' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'normalFixedVariance' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'normal' PosteriorDraw(mdObj, x, n = 1, ...) ## S3 method for class 'weibull' PosteriorDraw(mdObj, x, n = 100, ...)
mdObj |
Mixing Distribution |
x |
Data |
n |
Number of draws |
... |
For a non-conjugate distribution the starting parameters. Defaults to a draw from the prior distribution. |
A sample from the posterior distribution
Calculate the posterior mean and quantiles from a Dirichlet process object.
PosteriorFrame(dpobj, xgrid, ndraws = 1000, ci_size = 0.1)
PosteriorFrame(dpobj, xgrid, ndraws = 1000, ci_size = 0.1)
dpobj |
The Dirichlet process object to be drawn from. |
xgrid |
The x values the posterior is to be evaluated at. |
ndraws |
The number of posterior draws to take. |
ci_size |
The size of the credible interval draw in terms of percentage. |
A dataframe consisting of the posterior mean and credible intervals.
Generate the posterior function of the Dirichlet function
PosteriorFunction(dpobj, ind)
PosteriorFunction(dpobj, ind)
dpobj |
Fitted Dirichlet Process object |
ind |
What iteration to draw the posterior function from. Defaults to the last iteration. |
A posterior function f(x).
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) postFuncDraw <- PosteriorFunction(dp) plot(-3:3, postFuncDraw(-3:3))
y <- rnorm(10) dp <- DirichletProcessGaussian(y) dp <- Fit(dp, 5) postFuncDraw <- PosteriorFunction(dp) plot(-3:3, postFuncDraw(-3:3))
Calculate the posterior parameters for a conjugate prior.
PosteriorParameters(mdObj, x) ## S3 method for class 'mvnormal' PosteriorParameters(mdObj, x) ## S3 method for class 'normalFixedVariance' PosteriorParameters(mdObj, x) ## S3 method for class 'normal' PosteriorParameters(mdObj, x)
PosteriorParameters(mdObj, x) ## S3 method for class 'mvnormal' PosteriorParameters(mdObj, x) ## S3 method for class 'normalFixedVariance' PosteriorParameters(mdObj, x) ## S3 method for class 'normal' PosteriorParameters(mdObj, x)
mdObj |
Mixing distribution object |
x |
Data |
Parameters of the posterior distribution
Calculate how well the prior predicts the data.
## S3 method for class 'exponential' Predictive(mdObj, x) Predictive(mdObj, x) ## S3 method for class 'mvnormal' Predictive(mdObj, x) ## S3 method for class 'normalFixedVariance' Predictive(mdObj, x) ## S3 method for class 'normal' Predictive(mdObj, x)
## S3 method for class 'exponential' Predictive(mdObj, x) Predictive(mdObj, x) ## S3 method for class 'mvnormal' Predictive(mdObj, x) ## S3 method for class 'normalFixedVariance' Predictive(mdObj, x) ## S3 method for class 'normal' Predictive(mdObj, x)
mdObj |
The distribution |
x |
The data |
The probability of the data being from the prior.
Print a Dirichlet process object. This will print some basic information
about the dirichletprocess
object.
## S3 method for class 'dirichletprocess' print(x, param_summary = FALSE, digits = 2, ...)
## S3 method for class 'dirichletprocess' print(x, param_summary = FALSE, digits = 2, ...)
x |
Dirichlet Process Object to print. |
param_summary |
If |
digits |
Integer; Number of digits to display. |
... |
Further arguments passed to or from other methods. |
dp <- Fit(DirichletProcessGaussian(rnorm(10)), 100) dp
dp <- Fit(DirichletProcessGaussian(rnorm(10)), 100) dp
Draw prior clusters and weights from the Dirichlet process
PriorClusters(dpobj)
PriorClusters(dpobj)
dpobj |
A Dirichlet process object |
A list of weights and parameters of the prior distribution of the Dirichcet process
Calculate the prior density of a mixing distribution
## S3 method for class 'beta' PriorDensity(mdObj, theta) ## S3 method for class 'beta2' PriorDensity(mdObj, theta) PriorDensity(mdObj, theta) ## S3 method for class 'weibull' PriorDensity(mdObj, theta)
## S3 method for class 'beta' PriorDensity(mdObj, theta) ## S3 method for class 'beta2' PriorDensity(mdObj, theta) PriorDensity(mdObj, theta) ## S3 method for class 'weibull' PriorDensity(mdObj, theta)
mdObj |
Mixing distribution |
theta |
Prior parameters |
Draw from the prior distribution
## S3 method for class 'beta' PriorDraw(mdObj, n = 1) ## S3 method for class 'beta2' PriorDraw(mdObj, n = 1) ## S3 method for class 'exponential' PriorDraw(mdObj, n) PriorDraw(mdObj, n) ## S3 method for class 'mvnormal' PriorDraw(mdObj, n = 1) ## S3 method for class 'mvnormal2' PriorDraw(mdObj, n = 1) ## S3 method for class 'normalFixedVariance' PriorDraw(mdObj, n = 1) ## S3 method for class 'normal' PriorDraw(mdObj, n = 1) ## S3 method for class 'weibull' PriorDraw(mdObj, n = 1)
## S3 method for class 'beta' PriorDraw(mdObj, n = 1) ## S3 method for class 'beta2' PriorDraw(mdObj, n = 1) ## S3 method for class 'exponential' PriorDraw(mdObj, n) PriorDraw(mdObj, n) ## S3 method for class 'mvnormal' PriorDraw(mdObj, n = 1) ## S3 method for class 'mvnormal2' PriorDraw(mdObj, n = 1) ## S3 method for class 'normalFixedVariance' PriorDraw(mdObj, n = 1) ## S3 method for class 'normal' PriorDraw(mdObj, n = 1) ## S3 method for class 'weibull' PriorDraw(mdObj, n = 1)
mdObj |
Mixing Distribution |
n |
Number of draws. |
A sample from the prior distribution
Generate the prior function of the Dirichlet process
PriorFunction(dpobj)
PriorFunction(dpobj)
dpobj |
A Dirichlet process object |
A function f(x) that represents a draw from the prior distrubtion of the Dirichlet process.
@export
Update the prior parameters of a mixing distribution
## S3 method for class 'beta' PriorParametersUpdate(mdObj, clusterParameters, n = 1) PriorParametersUpdate(mdObj, clusterParameters, n = 1) ## S3 method for class 'weibull' PriorParametersUpdate(mdObj, clusterParameters, n = 1)
## S3 method for class 'beta' PriorParametersUpdate(mdObj, clusterParameters, n = 1) PriorParametersUpdate(mdObj, clusterParameters, n = 1) ## S3 method for class 'weibull' PriorParametersUpdate(mdObj, clusterParameters, n = 1)
mdObj |
Mixing Distribution Object |
clusterParameters |
Current cluster parameters |
n |
Number of samples |
mdobj New Mixing Distribution object with updated cluster parameters
Rat tumour data from Tarone (1982). Data from Table 5.1 of Bayesian Data Analysis
rats
rats
number of rats with a tumour
total number of rats in the experiment
http://www.stat.columbia.edu/~gelman/book/data/rats.asc
A Dirichlet process can be represented using a stick breaking construction
,
where are the stick breaking weights.
The atoms
are drawn from
the base measure of the Dirichlet Process.
The
. In theory
should be infinite, but we chose some value of
to truncate
the series. For more details see reference.
StickBreaking(alpha, N) piDirichlet(betas)
StickBreaking(alpha, N) piDirichlet(betas)
alpha |
Concentration parameter of the Dirichlet Process. |
N |
Truncation value. |
betas |
Draws from the Beta distribution. |
Vector of stick breaking probabilities.
piDirichlet()
: Function for calculating stick lengths.
Ishwaran, H., & James, L. F. (2001). Gibbs sampling methods for stick-breaking priors. Journal of the American Statistical Association, 96(453), 161-173.
Identifies the correct clusters labels, in any dimension, when cluster parameters and global parameters are matched.
true_cluster_labels(array, dpObj)
true_cluster_labels(array, dpObj)
array |
The array containing matching indexes. |
dpObj |
A hierarchical dirichletprocess object. |
The array containing the correct matching indexes
Using the procedure outlined in West (1992) we sample the concentration parameter of the Dirichlet process. See reference for further details.
UpdateAlpha(dpobj) ## Default S3 method: UpdateAlpha(dpobj) ## S3 method for class 'hierarchical' UpdateAlpha(dpobj)
UpdateAlpha(dpobj) ## Default S3 method: UpdateAlpha(dpobj) ## S3 method for class 'hierarchical' UpdateAlpha(dpobj)
dpobj |
Dirichlet process object. |
A Dirichlet process object with updated concentration parameter.
West, M. (1992). Hyperparameter estimation in Dirichlet process mixture models. ISDS Discussion Paper# 92-A03: Duke University.
and
parameter of a hidden Markov Dirichlet process model.Update the and
parameter of a hidden Markov Dirichlet process model.
UpdateAlphaBeta(dp)
UpdateAlphaBeta(dp)
dp |
Dirichlet process object |
See DirichletProcessWeibull
for the default prior and hyper prior distributions.
WeibullMixtureCreate( priorParameters, mhStepSize, hyperPriorParameters = c(6, 2, 1, 0.5) )
WeibullMixtureCreate( priorParameters, mhStepSize, hyperPriorParameters = c(6, 2, 1, 0.5) )
priorParameters |
Prior parameters for the Weibull parameters |
mhStepSize |
Metropolis Hastings Step Size |
hyperPriorParameters |
Parameters for the hyper-priors |
A mixing distribution object.
Generate a weighted function.
weighted_function_generator(func, weights, params)
weighted_function_generator(func, weights, params)
func |
Function that is used of the form func(x, params). |
weights |
Weighting of each cluster. |
params |
Cluster parameter list |
weighted function