Search code examples
rtransformationr-caretdplyr

Boxcox transformation on multiple variables with mutate_at


Say, I want to do boxcox transformation from caret package on the following data (not the data I am working with but just to explain my problem):

library(caret); library(tidyverse)
set.seed(001)
d <- tibble(a = rpois(20, 10), b = rnorm(20, 40, 10))
    head(d)
# A tibble: 6 x 2
      a     b
  <int> <dbl>
1     8  20.1
2    10  46.2
3     7  39.4
4    11  38.4
5    14  25.3
6    12  35.2

I can achieve this by running

d1 <- BoxCoxTrans(d$a) %>% predict(d$a)

I can repeat the same process to transform b. Is there a way I can do boxcox transformation on both variables a and b at the same time with dplyr? I tried the following but I am not able to figure out how to write the .funs

d %>% mutate_at(c("a", "b"), BoxCoxTrans %>% predict(d))

Solution

  • I have never used caret, but is there any reason these solutions would not work in your particular case? (They run fine for me.)

    library(tidyverse)
    library(caret)
    library(e1071)
    set.seed(001)
    d <- tibble(a = rpois(20, 10), b = rnorm(20, 40, 10))
    head(d)
    
    #On selected columns
    d %>%
      mutate_at(vars(a,b), funs( BoxCoxTrans(.) %>% predict(.)))
    
    #Or on all columns
    d %>%
      mutate_all(funs( BoxCoxTrans(.) %>% predict(.)))