Performing "zero skewness log transformation" in R

Hi all,

I have been asked to perform "zero skewness log transformation". It is a procedure in Stata, but as I use R I am trying to find the equivalent for it.

I am not able to find something equivalent. Have any of you tried to do the same ?

Thank you all!

rlnskew0.pdf (stata.com)

Description
lnskew0 creates newvar = ln(±exp − k), choosing k and the sign of exp so that the skewness
of newvar is zero.

My initial though is that I would try to use optim for this

Hi,

Yes, the challenge is to find the optimal constant. With sign of exp I ques it just depend if the distribution is left-skewed or right-skewed.

I have looked at this optim() you suggest, but I can´t figure out how to use it in this situation.

library(e1071)
set.seed(42)
a_skewed_dist <<- rnorm(1000)^2
hist(a_skewed_dist)
e1071::skewness(a_skewed_dist)

transform_plus <- function(exp,k){log(exp - k)}
transform_min <- function(exp,k){log(-exp - k)}

tplus_wrap <- function(k){
  tvals <- transform_plus(a_skewed_dist,k)
  abs(e1071::skewness(tvals))
}
tmin_wrap <- function(k){
  tvals <- transform_min(a_skewed_dist,k)
  abs(e1071::skewness(tvals))
}

(plus_res <- optim(par=c(0),fn=tplus_wrap))
(min_res <- optim(par=c(-100),fn=tmin_wrap))

choice_res <- if(plus_res$value <= min_res$value){
  list(optim_res = plus_res,
       sign="+",
       k=plus_res$par,
       func_to_use = function(x){
         transform_plus(exp=x,k=plus_res$par)
       })
} else {
  list(optim_res = min_res,
       sign="-",
       k=min_res$par,
       func_to_use = function(x){
         transform_min(exp=x,k=min_res$par)
       })
}

(my_transformed_var <- choice_res$func_to_use(a_skewed_dist))
e1071::skewness(my_transformed_var)
hist(my_transformed_var)

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.