简体   繁体   English

使用mutate_at对多个变量进行Boxcox转换

[英]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): 说,我想对以下数据(不是我正在使用的数据,只是为了解释我的问题)从插入符包进行boxcox转换:

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. 我可以重复相同的过程来变换b。 Is there a way I can do boxcox transformation on both variables a and b at the same time with dplyr? 有没有办法用dplyr同时对变量a和b进行boxcox转换? I tried the following but I am not able to figure out how to write the .funs 我尝试了以下操作,但无法弄清楚如何编写.funs

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

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(.)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM