dmix: Evaluate a mixture density

Description Usage Arguments Examples

View source: R/dmix.R

Description

Evaluates mixture densities of the form

f(x) = ∑_{j=1}^k f(x|θ^{(k)}) w_k

where the w_k are (possibly negative) weights that sum to 1 and f(x|θ^{(k)}) are densities that are specified via parameters θ^{(k)}, which are passed in the function argument params. A unique feature of this function is that it is able to evaluate mixture densities in which some of the mixture weights w_k are negative.

Usage

1
dmix(x, f, params, wts, log = FALSE, errorNodesWts = NULL, ...)

Arguments

x

Points at which the mixture should be evaluated. If the density is multivariate, then each row of x should contain one set of points at which the mixture should be evaluated.

f

Density used in the mixture. The function should be defined so it is can be called via f(x, params, log, ...). The density f is evaluated at the points in x using one set of parameters params, i.e., for some specific θ^{(k)}. if log==TRUE, then ln(f) is returned. Additional parameters may be passed to f via ....

params

Matrix in which each row contains parameters that define f. The number of rows in params should match the number of mixture components k.

wts

vector of weights for each mixture component

log

TRUE to return the log of the mixture density

errorNodesWts

list with elements inds and weights that point out which params get used to compute an approximation of the quadrature error.

...

additional arguments to be passed to f

Examples

 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
# evaluate mixture density at these locations
x = seq(0, 1, length.out = 100)

# density will be a mixture of beta distributions
f = function(x, theta, log = FALSE) {
  dbeta(x, shape1 = theta[1], shape2 = theta[2], log = log)
}

# beta parameters are randomly assigned
params = matrix(exp(2*runif(10)), ncol=2)

# mixture components are equally weighted
wts = rep(1/nrow(params), nrow(params))

# evaluate mixture density
fmix = dmix(x = x, f = f, params = params, wts = wts)

# plot mixture density
plot(x, fmix, type='l', ylab = expression(f(x)), 
     ylim = c(0, 4))

# plot component densities
for(i in 1:length(wts)){
  curve(f(x, params[i,]), col = 2, add = TRUE)
}

jmhewitt/bisque documentation built on Feb. 9, 2020, 2:36 a.m.