User written Box-Cox type transformation function

Thank you all for your comments. Here is the final version of the function that works! This is based on the Yeo-Johnson transformation function which can handle both negative and positive values.

functions {
  
  real g(real y, real p) {
    
    real z;
    real eps;
    
    eps = 0.005;
    if (y >= 0) {
      if ( abs(p) <= eps) z = log1p(y);
      else z = (pow(1 + y, p) - 1)/p;
    }
    
    else {
      if (p >= 2-eps && p <= 2+eps) z = -log1m(y);
      else z = -(pow(1 - y, 2-p) - 1)/(2-p);
    }
    
    return z;
  }
}

Yeo, I.K. and Johnson, R.A., 2000. A new family of power transformations to improve normality or symmetry. Biometrika, 87(4), pp.954-959.

Regards,
Raphael

[edit: escaped code]

1 Like