```
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)
```