Search code examples
rlsmeans

Using BCNPower Transform (car) and emmeans


The car package in R implements Box-Cox with non-positives (BCN). This is not a standard Box-Cox transformation, although it builds on it.

The emmeans package can handled transformed data. It can back transform if the proper functions are defined. There are built-in transformations under make.tran. This is the interface emmeans uses.

make.tran returns a list containing the transformation, its inverse, the jacobian, and a function to test the domain.

How can you add the BCN transformation to the built-in ones?

Is it a matter of simply creating a new list with the proper content (like specified by make.tran) and using this within the environment calling emmeans?

Thanks


Solution

  • Here is something that works. You need to set global variables lambda and gamma equal to the corresponding parameter values as documented for car::bcnPower().

    tran = list (
       linkfun = function(mu) {
           s = sqrt(mu^2 + gamma^2)
           if (abs(lambda) < 1e-10) log(.5*(mu + s))
           else ((0.5 * (mu + s))^lambda - 1) / lambda  },
       linkinv = function(eta) {
           q = if (abs(lambda) < 1e-10) 2 * exp(eta)
           else 2 * (lambda * eta + 1) ^ (1/lambda)
           (q^2 - gamma^2) / (2 * q) },
       mu.eta = function(eta) {
           if (abs(lambda) < 1e-10) { q = 2 * exp(eta); dq = q }
           else { q = 2 * (lambda * eta + 1) ^ (1/lambda)
           dq = 2 * (lambda * eta + 1)^(1/lambda - 1) }
           0.5 * (1 + (gamma/q)^2) * dq },
       valideta = function(eta) all(eta > 0),
       param = c(lambda, gamma),
       name = paste0("bcnPower(", signif(lambda,3), ", ", signif(gamma,3), ")")
    )
    

    Then use tran as if you had created it via make.tran().

    This will be available by way of make.tran("bcnPower, c(lambda, gamma)) after a month or more, when the next release is uploaded to CRAN.