步骤将固定值变异为 tidymodels 中的变量列表
step mutate fixed value to a list of variables in tidymodels
我想知道是否可以在我的食谱中改变变量,获取变量列表并在找到 NA 时输入一个固定值 (-12345)。
到目前为止没有成功。
my_list <- c("impute1", "impute2", "impute3")
recipe <-
recipes::recipe(target ~ ., data = data_train) %>%
recipes::step_naomit(everything(), skip = TRUE) %>%
recipes::step_rm(c(v1, v2, id, id2 )) %>%
recipes::step_mutate_at(my_list, if_else(is.na(.), -12345, . ))
错误 step_mutate_at_new(terms = ellipse_check(...), fn = fn, trained = trained, :
缺少参数“fn”,没有默认值
你走在正确的轨道上。一些笔记。要使 recipes::step_mutate_at()
正常工作,您需要两件事。要转换的一组变量和一个或多个要应用于该选择的函数。这些函数应该作为函数(命名或匿名)或函数的命名列表传递给 fn
参数。
在 step_mutate_at()
中设置 fn = ~if_else(is.na(.), -12345, . )
应该可以解决您的问题,使用 ~fun(.)
lambda 样式。此外,我使用 all_of(my_list)
而不是 my_list
来避免使用外部向量 reference.
的模糊选择
最后使用 step_naomit()
删除烘焙过程中带有缺失值的观察结果,这可能是不可取的,因为您正在估算缺失值。
library(recipes)
mtcars1 <- mtcars
mtcars1[1, 1:3] <- NA
my_list <- c("mpg", "cyl", "disp")
recipe <-
recipe(drat ~ ., data = mtcars1) %>%
step_mutate_at(all_of(my_list), fn = ~if_else(is.na(.), -12345, . ))
recipe %>%
prep() %>%
bake(new_data = NULL)
#> # A tibble: 32 x 11
#> mpg cyl disp hp wt qsec vs am gear carb drat
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 -12345 -12345 -12345 110 2.62 16.5 0 1 4 4 3.9
#> 2 21 6 160 110 2.88 17.0 0 1 4 4 3.9
#> 3 22.8 4 108 93 2.32 18.6 1 1 4 1 3.85
#> 4 21.4 6 258 110 3.22 19.4 1 0 3 1 3.08
#> 5 18.7 8 360 175 3.44 17.0 0 0 3 2 3.15
#> 6 18.1 6 225 105 3.46 20.2 1 0 3 1 2.76
#> 7 14.3 8 360 245 3.57 15.8 0 0 3 4 3.21
#> 8 24.4 4 147. 62 3.19 20 1 0 4 2 3.69
#> 9 22.8 4 141. 95 3.15 22.9 1 0 4 2 3.92
#> 10 19.2 6 168. 123 3.44 18.3 1 0 4 4 3.92
#> # … with 22 more rows
由 reprex package (v2.0.0)
于 2021-06-21 创建
我想知道是否可以在我的食谱中改变变量,获取变量列表并在找到 NA 时输入一个固定值 (-12345)。
到目前为止没有成功。
my_list <- c("impute1", "impute2", "impute3")
recipe <-
recipes::recipe(target ~ ., data = data_train) %>%
recipes::step_naomit(everything(), skip = TRUE) %>%
recipes::step_rm(c(v1, v2, id, id2 )) %>%
recipes::step_mutate_at(my_list, if_else(is.na(.), -12345, . ))
错误 step_mutate_at_new(terms = ellipse_check(...), fn = fn, trained = trained, : 缺少参数“fn”,没有默认值
你走在正确的轨道上。一些笔记。要使 recipes::step_mutate_at()
正常工作,您需要两件事。要转换的一组变量和一个或多个要应用于该选择的函数。这些函数应该作为函数(命名或匿名)或函数的命名列表传递给 fn
参数。
在 step_mutate_at()
中设置 fn = ~if_else(is.na(.), -12345, . )
应该可以解决您的问题,使用 ~fun(.)
lambda 样式。此外,我使用 all_of(my_list)
而不是 my_list
来避免使用外部向量 reference.
最后使用 step_naomit()
删除烘焙过程中带有缺失值的观察结果,这可能是不可取的,因为您正在估算缺失值。
library(recipes)
mtcars1 <- mtcars
mtcars1[1, 1:3] <- NA
my_list <- c("mpg", "cyl", "disp")
recipe <-
recipe(drat ~ ., data = mtcars1) %>%
step_mutate_at(all_of(my_list), fn = ~if_else(is.na(.), -12345, . ))
recipe %>%
prep() %>%
bake(new_data = NULL)
#> # A tibble: 32 x 11
#> mpg cyl disp hp wt qsec vs am gear carb drat
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 -12345 -12345 -12345 110 2.62 16.5 0 1 4 4 3.9
#> 2 21 6 160 110 2.88 17.0 0 1 4 4 3.9
#> 3 22.8 4 108 93 2.32 18.6 1 1 4 1 3.85
#> 4 21.4 6 258 110 3.22 19.4 1 0 3 1 3.08
#> 5 18.7 8 360 175 3.44 17.0 0 0 3 2 3.15
#> 6 18.1 6 225 105 3.46 20.2 1 0 3 1 2.76
#> 7 14.3 8 360 245 3.57 15.8 0 0 3 4 3.21
#> 8 24.4 4 147. 62 3.19 20 1 0 4 2 3.69
#> 9 22.8 4 141. 95 3.15 22.9 1 0 4 2 3.92
#> 10 19.2 6 168. 123 3.44 18.3 1 0 4 4 3.92
#> # … with 22 more rows
由 reprex package (v2.0.0)
于 2021-06-21 创建