##
## 1. A simple example to check the two algorithms
##
Days <- 0:9
bc1 <- BoxCox(Days, c(0.01, 1))
# Taylor expansion used for obs 1:7; expm1 for 8:10
# check
GM <- exp(mean(log(abs(Days+1))))
bc0 <- (((Days+1)^0.01)-1)/0.01
bc1. <- (bc0 / (GM^(0.01-1)))
# log(Days+1) ranges from 0 to 4.4
# lambda = 0.01 will invoke both the obvious
# algorithm and the alternative assumed to be
# more accurate for (lambda(log(y)) < 0.02).
attr(bc1., 'lambda') <- c(0.01, 1)
attr(bc1., 'rescale') <- TRUE
attr(bc1., 'GeometricMean') <- GM
class(bc1.) <- 'BoxCox'
stopifnot(
all.equal(bc1, bc1.)
)
##
## 2. another simple example with lambda=0
##
bc0.4 <- BoxCox(1:5, 0)
GM5 <- prod(1:5)^.2
bc0.4. <- log(1:5)*GM5
attr(bc0.4., 'lambda') <- 0
attr(bc0.4., 'rescale') <- TRUE
attr(bc0.4., 'GeometricMean') <- GM5
class(bc0.4.) <- 'BoxCox'
stopifnot(
all.equal(bc0.4, bc0.4.)
)
bc0.4e9 <- BoxCox(1:5, .Machine$double.eps)
bc0.4ex <- log(1:5)*exp(mean(log(1:5)))
stopifnot(
all.equal(bc0.4ex, as.numeric(bc0.4e9))
)
# now invert:
bc0.4i <- invBoxCox(bc0.4.)
stopifnot(
all.equal(1:5, bc0.4i)
)
stopifnot(
all.equal(1:5, invBoxCox(bc0.4e9))
)
##
## 3. The "boxcox" function in the MASS package
## computes a maximum likelihood estimate with
## BoxCox(Days+1, lambda=0.21)
## with a 95 percent confidence interval of
## approximately (0.08, 0.35)
##
bcDays1 <- BoxCox(MASS::quine$Days, c(0.21, 1))
# check
GeoMean <- exp(mean(log(abs(MASS::quine$Days+1))))
bcDays1. <- ((((MASS::quine$Days+1)^0.21)-1) /
(0.21*GeoMean^(0.21-1)))
# log(Days+1) ranges from 0 to 4.4
attr(bcDays1., 'lambda') <- c(0.21, 1)
attr(bcDays1., 'rescale') <- TRUE
attr(bcDays1., 'GeometricMean') <- GeoMean
class(bcDays1.) <- 'BoxCox'
stopifnot(
all.equal(bcDays1, bcDays1.)
)
iDays <- invBoxCox(bcDays1)
stopifnot(
all.equal(iDays, MASS::quine$Days)
)
##
## 4. Easily computed example
##
bc2 <- BoxCox(c(1, 4), 2)
# check
bc2. <- (c(1, 4)^2-1)/4
attr(bc2., 'lambda') <- 2
attr(bc2., 'rescale') <- TRUE
attr(bc2., 'GeometricMean') <- 2
class(bc2.) <- 'BoxCox'
stopifnot(
all.equal(bc2, bc2.)
)
stopifnot(
all.equal(invBoxCox(bc2), c(1, 4))
)
##
## 5. plot(BoxCox())
##
y0 <- seq(-2, 2, .1)
z2 <- BoxCox(y0, 2, rescale=FALSE)
plot(y0, z2)
# check
z2. <- (sign(y0)*y0^2-1)/2
attr(z2., 'lambda') <- 2
attr(z2., 'sign.y') <- sign(y0, 1)
attr(z2., 'rescale') <- FALSE
attr(z2., 'GeometricMean') <- 0
class(z2.) <- 'BoxCox'
stopifnot(
all.equal(z2, z2.)
)
stopifnot(
all.equal(invBoxCox(z2), y0)
)
Run the code above in your browser using DataLab