View source: R/box_cox_functions.R
| find_lambda_one_d | R Documentation |
Finds a value of the Box-Cox transformation parameter lambda (\lambda)
for which the (positive univariate) random variable with log-density
\log f has a density closer to that of a Gaussian random
variable. Works by estimating a set of quantiles of the distribution implied
by \log f and treating those quantiles as data in a standard
Box-Cox analysis. In the following we use theta (\theta) to
denote the argument of \log f on the original scale and
phi (\phi) on the Box-Cox transformed scale.
find_lambda_one_d(
logf,
...,
ep_bc = 1e-04,
min_phi = ep_bc,
max_phi = 10,
num = 1001,
xdiv = 100,
probs = seq(0.01, 0.99, by = 0.01),
lambda_range = c(-3, 3),
phi_to_theta = NULL,
log_j = NULL
)
logf |
A function returning the log of the target density |
... |
further arguments to be passed to |
ep_bc |
A (positive) numeric scalar. Smallest possible value of
|
min_phi, max_phi |
Numeric scalars. Smallest and largest values
of |
num |
A numeric scalar. Number of values at which to evaluate
|
xdiv |
A numeric scalar. Only values of |
probs |
A numeric scalar. Probabilities at which to estimate the quantiles of that will be used as data to find lambda. |
lambda_range |
A numeric vector of length 2. Range of lambda over which to optimise. |
phi_to_theta |
A function returning (inverse) of the transformation
from |
log_j |
A function returning the log of the Jacobian of the
transformation from |
The general idea is to estimate quantiles of f corresponding
to a set of equally-spaced probabilities in probs and to use these
estimated quantiles as data in a standard estimation of the Box-Cox
transformation parameter lambda.
The density f is first evaluated at num points equally
spaced over the interval (min_phi, max_phi). The continuous
density f is approximated by attaching trapezium-rule estimates of
probabilities to the midpoints of the intervals between the points. After
standardizing to account for the fact that f may not be normalized,
(min_phi, max_phi) is reset so that values with small
estimated probability (determined by xdiv) are excluded and the
procedure is repeated on this new range. Then the required quantiles are
estimated by inferring them from a weighted empirical distribution
function based on treating the midpoints as data and the estimated
probabilities at the midpoints as weights.
A list containing the following components
lambda |
A numeric scalar. The value of lambda. |
gm |
A numeric scalar. Box-Cox scaling parameter, estimated by the geometric mean of the quantiles used in the optimisation to find the value of lambda. |
init_psi |
A numeric scalar. An initial estimate of the mode of the Box-Cox transformed density |
sd_psi |
A numeric scalar. Estimates of the marginal standard deviations of the Box-Cox transformed variables. |
phi_to_theta |
as detailed above (only if |
log_j |
as detailed above (only if |
Box, G. and Cox, D. R. (1964) An Analysis of Transformations. Journal of the Royal Statistical Society. Series B (Methodological), 26(2), 211-252.
Andrews, D. F. and Gnanadesikan, R. and Warner, J. L. (1971) Transformations of Multivariate Data, Biometrics, 27(4).
ru and ru_rcpp to perform
ratio-of-uniforms sampling.
find_lambda and find_lambda_rcpp
to produce (somewhat) automatically
a list for the argument lambda of ru/ru_rcpp
for any value of d.
find_lambda_one_d_rcpp for a version of
find_lambda_one_d that uses the Rcpp package to improve
efficiency.
# Log-normal density ===================
# Note: the default value of max_phi = 10 is OK here but this will not
# always be the case.
lambda <- find_lambda_one_d(logf = dlnorm, log = TRUE)
lambda
x <- ru(logf = dlnorm, log = TRUE, d = 1, n = 1000, trans = "BC",
lambda = lambda)
# Gamma density ===================
alpha <- 1
# Choose a sensible value of max_phi
max_phi <- qgamma(0.999, shape = alpha)
# [I appreciate that typically the quantile function won't be available.
# In practice the value of lambda chosen is quite insensitive to the choice
# of max_phi, provided that max_phi is not far too large or far too small.]
lambda <- find_lambda_one_d(logf = dgamma, shape = alpha, log = TRUE,
max_phi = max_phi)
lambda
x <- ru(logf = dgamma, shape = alpha, log = TRUE, d = 1, n = 1000,
trans = "BC", lambda = lambda)
alpha <- 0.1
# NB. for alpha < 1 the gamma(alpha, beta) density is not bounded
# So the ratio-of-uniforms emthod can't be used but it may work after a
# Box-Cox transformation.
# find_lambda_one_d() works much better than find_lambda() here.
max_phi <- qgamma(0.999, shape = alpha)
lambda <- find_lambda_one_d(logf = dgamma, shape = alpha, log = TRUE,
max_phi = max_phi)
lambda
x <- ru(logf = dgamma, shape = alpha, log = TRUE, d = 1, n = 1000,
trans = "BC", lambda = lambda)
plot(x)
plot(x, ru_scale = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.