Type: Package
Title: Density Deconvolution Using Bayesian Semiparametric Methods
Version: 0.1.3
Maintainer: Mainak Manna <mainakmanna29@utexas.edu>
Description: Estimates the density of a variable in a measurement error setup, potentially with an excess of zero values. For more details see Sarkar (2022) <doi:10.1080/10618600.2022.2060239>.
License: GPL-2 | GPL-3 [expanded from: GPL (≥ 2)]
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.3.3
LinkingTo: Rcpp, RcppArmadillo
Imports: Rcpp, msm, corpcor, ks, mvtnorm
Suggests: ggplot2, gridExtra, parallel, foreach, doParallel, RColorBrewer
Depends: R (≥ 4.1.0)
NeedsCompilation: yes
Packaged: 2026-01-07 16:26:49 UTC; mm227334
Author: Blake Moya [aut], Mainak Manna [cre, aut], Abhra Sarkar [aut], The University of Texas at Austin [cph, fnd]
Repository: CRAN
Date/Publication: 2026-01-09 15:00:12 UTC

Density Deconvolution Using Bayesian Semiparametric Methods

Description

Placeholder...

Author(s)

Blake Moya blakemoya@utexas.edu


The Quadratic B-Spline Distribution

Description

Density function for the normalized quadratic B-Spline distribution.

Usage

dbspline(x, theta, lwr, upr, log = FALSE)

pbspline(x, theta, lwr, upr, lower.tail = TRUE, log.p = FALSE)

Arguments

x

A vector of quantiles.

theta

A vector of unnormalized spline parameters.

lwr

A scalar lower bound.

upr

A scalar upper bound.

log

A logical value indication whether a probability p should be given as log(p)

Value

dbspline gives the density. The length of the result is determined by the length of x for dbspline.


Simulated dataset for BayesDecon

Description

A small example dataset used to demonstrate bayesdecon functions. There are 1000 observations each with 3 replicates for 3 variables V1, V2, V3. The first column represents the observations.

Usage

BayesDecon_data_simulated

Format

A data frame with 3000 rows and 4 variables.

Source

Generated for package examples.


Generate a Penalty Matrix

Description

Generate a Penalty Matrix

Usage

P1.mat(K)

Arguments

K

An integer dimensino value.

Value

A first-order difference penalty matrix.


Generate a Penalty Matrix

Description

Generate a Penalty Matrix

Usage

P2.mat(K)

Arguments

K

An integer dimensino value.

Value

A second-order difference penalty matrix.


The Restricted Two-Component Normal Distribution

Description

Density and distribution functions for the mean-restricted two-component normal distribution.

Usage

drtcnorm(x, p, mu, sig1, sig2, log = FALSE)

prtcnorm(q, p, mu, sig1, sig2, lower.tail = TRUE, log.p = FALSE)

Arguments

x

A vector of quantiles.

p

A scalar mixture weight.

mu

A scalar separation parameter.

sig1

A scalar standard deviation for the first component.

sig2

A scalar standard deviation for the second component.

log

A logical value indication whether a probability p.

lower.tail

A logical value indicated whether or not to take the cumulative distribution function with respect to the lower tail (alternatively upper).

Value

drtcnorm gives the density, prtcnorm gives the distribution. The length of the result is determined by the length of x for drtcnorm.


The Truncated Normal Distribution

Description

Density, distribution, quantile, and random variate generation functions for the Truncated Normal distribution, with mu and sig equal to the mean and standard deviation before truncation and truncated to ⁠[lwr, upr]⁠. Wrappers for functions from msm.

Usage

dtnorm(x, mu, sig, lwr, upr, log = FALSE)

ptnorm(q, mu, sig, lwr, upr, lower.tail = TRUE, log.p = FALSE)

qtnorm(p, mu, sig, lwr, upr, lower.tail = TRUE, log.p = FALSE)

rtnorm(n, mu, sig, lwr = -Inf, upr = Inf)

Arguments

x, q

A vector of quantiles.

mu

A real-valued mean.

sig

A strictly positive standard deviation.

lwr

A lower bound.

upr

An upper bound.

log, log.p

A logical value indication whether a probability p should be given as log(p)

lower.tail

A logical value indicating whether to take lower or upper tail probabilities.

p

A vector of probabilities.

n

A number of observations.

Value

dtnorm gives the density, ptnorm gives the distribution function, qtnorm gives the quantile function, and rtnorm generates random variates. The length of the result is determined by the length of x for dtnorm.


Density deconvolution using bayesian semiparametric methods

Description

Density deconvolution using bayesian semiparametric methods

Usage

## S3 method for class 'bdeconv_result'
as.list(x, ...)

Arguments

x

A bdeconv_result object.

...

Unused.


Visualization of bdeconv_result object using ggplot2

Description

Visualization of bdeconv_result object using ggplot2

Usage

autoplot.bdeconv_result(object)

Arguments

object

A bdeconv_result object.

Value

A collection of plots illustrating various densities and related functions made using ggplot2.


Obtaining MCMC samples from deconvoluted density

Description

Obtaining MCMC samples from deconvoluted density

Usage

bdeconv(
  obj,
  lwr = 0,
  upr = NULL,
  wgts = NULL,
  regular_var = NULL,
  res_type = c("final", "mean", "all"),
  err_type = c("flex", "norm", "lapl"),
  var_type = c("heteroscedastic", "homoscedastic"),
  na_rm = FALSE,
  control = list(),
  progress = FALSE,
  core_num = 1,
  parallel = FALSE
)

Arguments

obj

A data frame where the first column is subject labels.

lwr

Unused.

upr

The ratio of an upper bound for the deconvolved estimates to the upper bound of the surrogates. It can be either a scalar or a vector with a length that matches the number of numeric columns in obj.

wgts

Unused.

regular_var

Specifies which variables to consider as regular; all others are treated as zero inflated.

res_type

The type of result returned from the MCMC sampler.

err_type

The shape of the error distribution used for deconvolution.

var_type

whether to use homogeneous or heterogeneous error distribution.

na_rm

A logical value indicating whether to ignore rows which contain NA values in numeric columns.

control

A list of arguments specifying prior hyper-parameters and parameters of the MCMC sampler.

progress

Whether a progress bar for MCMC will be shown or not. Default is FALSE.

core_num

If parallelized, number of cores to be used.

parallel

For multivariate data. Whether the pre-processing of each variable will be parallelized or not. Default is FALSE.

Details

All hyper-parameters of the model and variables necessary for the MCMC sampler can be specified through a list via the control argument; some of the most important ones are listed.

Value

A bdeconv_result object, containing all the posterior samples, suitable for further analysis and visualization.

Examples

data(BayesDecon_data_simulated)
set.seed(123)
### Selecting first 300 rows for demonstration.
### For best results, using the whole data is recommended.
result_uni <- bdeconv(BayesDecon_data_simulated[1:300,c(1,2)],
 upr = 1, res_type = "all",
 control = list(nsamp = 500, nburn = 0, nthin = 1),
 progress = TRUE)
plot(result_uni)


set.seed(123)
result_mult <- bdeconv(BayesDecon_data_simulated, upr = 1, res_type = "all", progress = TRUE)
plot(result_mult)
plot(result_mult[,c(1,2)])
value = rbind(c(1,1,1),c(2,2,2),c(3,3,3),c(4,4,4))
colMeans(dist_x(result_mult, vals = value))
colMeans(dens_x(result_mult, vals = value))
print(result_mult)



Constructor for mv_res Objects

Description

Constructor for mv_res Objects

Usage

bdeconvmv_result(obj, res_type = c("final", "mean", "all"))

Arguments

obj

A list as returned by bdeconv.mvtzi().

res_type

The type of result returned from the Markov chain Monte Carlo sampler.

Value

An mv_res object.

See Also

bdeconv()


Constructor for nc_res Objects

Description

Constructor for nc_res Objects

Usage

bdeconvnc_result(obj, res_type = c("final", "mean", "all"))

Arguments

obj

A list as returned by bdeconv.uninc().

res_type

The type of result returned from the Markov chain Monte Carlo sampler.

Value

An nc_res object.

See Also

bdeconv()


Constructor for zi_res Objects

Description

Constructor for zi_res Objects

Usage

bdeconvzi_result(obj, res_type = c("final", "mean", "all"))

Arguments

obj

A list as returned by bdeconv.unizi().

res_type

The type of result returned from the Markov chain Monte Carlo sampler.

Value

A zi_res object.

See Also

bdeconv()


Generate a B-spline Basis Matrix

Description

Generate a B-spline Basis Matrix

Usage

bspline_basis(x, lwr, upr, K)

Arguments

x

A numeric vector of values on which to evaluate the basis.

lwr

A lower bound.

upr

An upper bound.

K

A number of knot points.

Value

A B-spline basis matrix.

See Also

bspline


Generate a B-spline Cumulative Basis Matrix

Description

Generate a B-spline Cumulative Basis Matrix

Usage

bspline_cumbasis(x, lwr, upr, K)

Arguments

x

A numeric vector of values on which to evaluate the basis.

lwr

A lower bound.

upr

An upper bound.

K

A number of knot points.

Value

A B-spline cumulative basis matrix.

See Also

bspline


Normalize B-Spline Parameters

Description

Normalize B-Spline Parameters

Usage

bspline_normcoef(theta, lwr, upr)

Arguments

theta

A B-spline parameter vector.

lwr

A lower bound.

upr

An upper bound.

Value

Normalize B-spline parameters.

See Also

bspline


Error Density Evaluation

Description

Error Density Evaluation

Usage

dens_u(obj, vals)

Arguments

obj

A bdeconv_result object.

vals

A numeric vector (or matrix) on which to evaluate the error density.

Value

A numeric matrix or array of density values.


Estimated density function for the true variable(s)

Description

Estimated density function for the true variable(s)

Usage

dens_x(obj, vals, expand = FALSE)

Arguments

obj

A bdeconv_result object.

vals

A numeric vector (for univariate) or matrix (for multivariate) on which to evaluate the density.

expand

Used only for mv_res objects. A logical value indicating whether or not to expand the matrix of values.

Value

A numeric matrix or array of density values for which each row represents the estimated density corresponding to each MCMC iteration.


Error Distribution Evaluation

Description

Error Distribution Evaluation

Usage

dist_u(obj, vals)

Arguments

obj

A bdeconv_result object.

vals

A numeric vector (or matrix) on which to evaluate the error density.

Value

A numeric matrix or array of cumulative distribution values.


Estimated cumulative distribution function for the true variable(s)

Description

Estimated cumulative distribution function for the true variable(s)

Usage

dist_x(obj, vals, expand = FALSE)

Arguments

obj

A bdeconv_result object.

vals

A numeric vector (for univariate) or matrix (for multivariate) on which to evaluate the CDF.

expand

Used only for mv_res objects. A logical value indicating whether or not to expand the matrix of values.

Value

A numeric matrix or array of cumulative distribution values for which each row represents the estimated CDF corresponding to each MCMC iteration.


Maximization function for variance function B-spline parameter initialization in bdeconv()

Description

Maximization function for variance function B-spline parameter initialization in bdeconv()

Usage

fr(thetas, xs, mis, knots.t, P.t, s2t, us)

Arguments

thetas

Spline parameters.

xs

Data values.

mis

Subject indicators.

knots.t

Knot points.

P.t

Penalty matrix.

s2t

Variance.

us

Residuals.

Value

A score to be maximized.


Maximization function for B-spline distribution parameter initialization in bdeconv()

Description

Maximization function for B-spline distribution parameter initialization in bdeconv()

Usage

fr.dens(thetas, xs, knots.t, P.t, s2t, density.est, K.t)

Arguments

thetas

Spline parameters.

xs

Data values.

knots.t

Knot points.

P.t

Penalty matrix.

s2t

Variance

density.est

Density estimate on grid.

K.t

Spline cardinality.

Value

A score to be maximized.


Maximization function for B-spline zero-inflation probability function parameter initialization in bdeconv()

Description

Maximization function for B-spline zero-inflation probability function parameter initialization in bdeconv()

Usage

fr.prob.nonzero(log_diff_thetas, xs, knots.t, P.t, s2t, probs.emp)

Arguments

log_diff_thetas

Spline parameters.

xs

Data values.

knots.t

Knot points.

P.t

Penalty matrix.

s2t

Variance

probs.emp

Empirical consumption probability.

Value

A score to be maximized.


Computing mean of MCMC samples

Description

Computing mean of MCMC samples

Usage

## S3 method for class 'bdeconv_result'
mean(x, ...)

Arguments

x

A bdeconv_result object.

...

Unused.

Details

Where a direct point-wise mean is not appropriate (i.e. for mixture density results), mixtures are expanded to included every component from every sampled density with weights divided by n, the number of samples from which the mean is being taken.

Value

A bdeconv_result object with one sample constituting the mean


Visualization of bdeconv_result object

Description

Visualization of bdeconv_result object

Usage

## S3 method for class 'bdeconv_result'
plot(x, ..., use_ggplot = TRUE)

Arguments

x

A bdeconv_result object.

...

Unused.

use_ggplot

A logical value indicating whether the plots will be generated using ggplot2 or using base plot functions.

Details

By default it uses ggplot2 package if it is available in the system library, otherwise it falls back to the base R plot function.

Value

A collection of plots illustrating various densities and related functions.


Printing summary of MCMC samples for bdeconv_result object

Description

Printing summary of MCMC samples for bdeconv_result object

Usage

## S3 method for class 'bdeconv_result'
print(x, ...)

Arguments

x

A bdeconv_result object.

...

Unused.

Details

For episodic components, this gives five number summary for the observed surrogates (y), the latent surrogates corresponding to the consumption days (w), the true intake patterns (x) and the errors (u). For regular components, this gives five number summary for y, x and u only.

Value

Prints the five number summary for the relevant variables.


Generate a Proposal Variance Matrix for B-Spline Parameters

Description

Generate a Proposal Variance Matrix for B-Spline Parameters

Usage

prop.sig.thetas.fn(thetas, xs, mis, us, s2t, K.t, P.t, knots.t, n)

Arguments

thetas

Spine parameters.

xs

Data values.

mis

Subject indicators.

us

Residuals.

s2t

Variance.

K.t

Spline cardinality.

P.t

Penalty matrix.

knots.t

Knot points.

n

Number of grid points.

Value

A proposal matrix.


Generate a Proposal Variance Matrix for B-Spline Parameters for episodic density

Description

Generate a Proposal Variance Matrix for B-Spline Parameters for episodic density

Usage

prop.sig.thetas.x.dens.fn(thetas, xs, s2t, K.t, P.t, knots.t, n)

Arguments

thetas

Spine parameters.

xs

Data values.

s2t

Variance.

K.t

Spline cardinality.

P.t

Penalty matrix.

knots.t

Knot points.

n

Number of grid points.

Value

A proposal matrix.


Register autoplot methods to ggplot2

Description

Register autoplot methods to ggplot2

Usage

register_autoplot_s3_methods()

Value

None


Register S3 Methods from External Packages

Description

https://github.com/tidyverse/hms/blob/master/R/zzz.R

Usage

register_s3_method(pkg, generic, class, fun = NULL)

Arguments

pkg

Package name.

generic

Generic function name.

class

Class name.

fun

Optional custom function name.

Value

None


Subset Methods for bdeconv_result object

Description

Subset Methods for bdeconv_result object

Usage

## S3 method for class 'zi_res'
obj[i]

## S3 method for class 'nc_res'
obj[i]

## S3 method for class 'mv_res'
obj[i, j]

Arguments

obj

A bdeconv_result object.

i

An integer row index (or indices) to select some specific MCMC iterations.

j

And integer column index (or indices) to select some specific set of variables (For multivariate case only).

Value

A bdeconv_result object containing the selected iterations of posterior samples for the specified variables (in the multivariate case), suitable for further analysis and visualization.


Variance Scaling For Mixtures of Restricted Two-Component Normals

Description

Variance Scaling For Mixtures of Restricted Two-Component Normals

Usage

var_e_fn(pi, p, mu, sig1, sig2)

Arguments

pi

A vector of mixing probabilities.

p

A vector of mixture weights as in drtcnorm.

mu

A vector of separation parameters.

sig1

A vector of standard deviations for the first component.

sig2

A vector of standard deviations for the second component.

Value

A variance scaling term.

See Also

rtcnorm


Error Variance as a function of X

Description

Error Variance as a function of X

Usage

varfun(obj, vals)

Arguments

obj

A bdeconv_result object.

vals

A numeric vector (or matrix) on which to evaluate the error variance.

Value

A numeric matrix or array of variance values.