geoR (version 1.3-0)

BoxCox: Box-Cox transformation related functions

Description

Estimate parameters and plot results , computes density and random generation for the Box-Cox normal transformed distribution.

Usage

boxcox.fit(data, xmat, lambda, lambda2 = FALSE, add.to.data = 0,...)

print.boxcox.fit(x, ...)

plot.boxcox.fit(x, hist = TRUE, data = eval(x$call$data), ...)

lines.boxcox.fit(x, data = eval(x$call$data), ...)

rboxcox(n, lambda, lambda2 = NULL, mean = 0, sd = 1)

dboxcox(x, lambda, lambda2 = NULL, mean = 0, sd = 1)

Arguments

data
a vector with data.
xmat
a matrix of covariates. Defaults to rep(1, length(y)).
lambda
numerical value(s) of the transformation parameter $\lambda$. Used as initial value for the parameter estimation function. If not provided default values are assumed. If multiple values are passed the one with highest likelihood is used a
lambda2
numerical value(s) of the additional transformation (see DETAILS below). Defaults to FALSE. If TRUE this parameter is also estimated and the initial value is set to the absolute value of the minimum data. A numeri
add.to.data
a constant to be added to the data.
n
number of observations to be generated.
x
a vector of quantiles (dboxcox) or an output of boxcox.fit (print, plot, lines).
mean
a vector of means at for the normal scale.
sd
a vector of standard deviations at the normal scale.
hist
logical indicating whether histograms are to be plotted.
...
extra parameters to be passed to the minimization function optim (boxcox.fit), hist (plot) or

Value

  • The functions returns the following results:
  • boxcox.fita list with estimated parameters and results on the numerical minimization.
  • print.boxcox.fitprint estimated parameters. No values returned.
  • plot.boxcox.fitplots histogram of the data (optional) and the model. No values returned. This function is only valid if covariates are not included in boxcox.fit.
  • lines.boxcox.fitadds a line with the fitted model to the current plot. No values returned. This function is only valid if covariates are not included in boxcox.fit.
  • rboxcoxa vector of generated random deviates.
  • dboxcoxa vector of densities.

Details

Denote $Y$ the variable at the original scale and $Y'$ the transformed variable. The Box-Cox transformation is defined by: $$Y' = \left{ \begin{array}{ll} log(Y) \mbox{ , if $\lambda = 0$} \cr \frac{Y^\lambda - 1}{\lambda} \mbox{ , otherwise} \end{array} \right.$$. An additional shifting parameter $\lambda_2$ can be included in which case the transformation is given by: $$Y' = \left{ \begin{array}{ll} log(Y + \lambda_2) \mbox{ , $\lambda = 0$ } \cr \frac{(Y + \lambda_2)^\lambda - 1}{\lambda} \mbox{ , otherwise} \end{array} \right.$$.

The function rboxcox samples $Y'$ from the normal distribution using rnorm and backtransform the values according to the equations above. If necessary the back-transformation truncates the values such that $Y' \geq \frac{1}{\lambda}$. This results in values of $Y = 0$ in the original scale. Increasing the mean and/or reducing the variance might help to avoid truncation.

References

Box, G.E.P. and Cox, D.R.(1964) An analysis of transformations. JRSS B 26:211--246.

See Also

The minimization function optim, boxcox in the package MASS and boxcox in the package car.

Examples

Run this code
## Simulating data
simul <- rboxcox(100, lambda=0.5, mean=10, sd=2)
## Finding the ML estimates
ml <- boxcox.fit(simul)
ml
## Ploting histogram and fitted model
plot(ml)
##
## Comparing models with different lambdas,
## zero  means and unit variances
curve(dboxcox(x, lambda=-1), 0, 8)
for(lambda in seq(-.5, 1.5, by=0.5))
  curve(dboxcox(x, lambda), 0, 8, add = TRUE)
##
## Another example, now estimating lambda2
##
simul <- rboxcox(100, lambda=0.5, mean=10, sd=2)
ml <- boxcox.fit(simul, lambda2 = TRUE)
ml
plot(ml)
##
## An example with a regression model
##
if(require(MASS)){
  data(trees)
  boxcox.fit(data = trees[,3], xmat = trees[,1:2])
  }

Run the code above in your browser using DataCamp Workspace