使用map2将参数传递给workflow_variables

问题描述 投票:0回答:1

我正在尝试设置许多可用的组合来使用 tidymodels::workflow_set 进行建模 我已经在 c() 中设置了一个嵌套的 tibble,其中包含每个变量的组合,但我无法使用 map2 将该变量传递到工作流程中。

我认为我正在尝试做的事情可能是不可能的。

variables = map2(outc,pred, \(y,x) workflow_variables(outcomes = y, predictors = x))
这行就是问题所在。

在所有 200 个排列中,结果列为 y,预测变量列为 x。不是 cutc 和 pred 中保存的值

combinations <- expand_grid(  
  outc = c("measurement","Ln_measurement"),
  truth = c("callibrated_true","Ln_true","true_sqr","true_cub"),
  moisture = c("","Moisture_percent","Ln_Moisture","Moisture_sqr","Moisture_cub"),
  organic = c("","SOM_percent","Ln_SOM","SOM_sqr","SOM_cub")
  )%>%
  tibble()%>%
  mutate(
    name = paste(outc,truth, moisture, organic, sep = "_"),
    pred = pmap(list(truth, moisture, organic), c),
    variables = map2(outc,pred, \(y,x) workflow_variables(outcomes = y, predictors = x))
    )
r purrr tidymodels
1个回答
0
投票

workflow_variables()
的文档注意参数使用 tidyselect(与
dplyr
用于
select()
相同)。您的代码使用字符串,因此它不起作用。

您可以将它们转换为符号,或者更好的是,使用

dplyr
选择器函数,例如
all_of()
:

library(workflows)
library(dplyr)
library(purrr)
library(tidyr)
library(rlang)

combinations <- 
  crossing(  
    outc = c("measurement", "Ln_measurement"),
    truth = c("callibrated_true", "Ln_true", "true_sqr", "true_cub"),
    moisture = c("Moisture_percent", "Ln_Moisture", "Moisture_sqr", "Moisture_cub"),
    organic = c("SOM_percent", "Ln_SOM", "SOM_sqr", "SOM_cub")
  ) %>%
  mutate(
    name = paste(outc, truth, moisture, organic, sep = "_"),
    pred = pmap(list(truth, moisture, organic), c),
    variables = map2(
      outc,
      pred,
      \(y, x) workflow_variables(
        outcomes = dplyr::all_of(y),
        predictors = dplyr::all_of(x)
      )
    )
  )

combinations$variables[[1]]
#> $outcomes
#> <quosure>
#> expr: ^dplyr::all_of(y)
#> env:  0x107df3b58
#> 
#> $predictors
#> <quosure>
#> expr: ^dplyr::all_of(x)
#> env:  0x107df3b58
#> 
#> attr(,"class")
#> [1] "workflow_variables"

# test it: 

example <-
  tibble(
    callibrated_true = 1,
    Ln_Moisture = 2,
    Ln_SOM = 3,
    bogus_column = 4
  )

combinations$pred[[1]]
#> [1] "callibrated_true" "Ln_Moisture"      "Ln_SOM"
dplyr::select(example, !!combinations$variables[[1]]$predictors)
#> # A tibble: 1 × 3
#>   callibrated_true Ln_Moisture Ln_SOM
#>              <dbl>       <dbl>  <dbl>
#> 1                1           2      3

创建于 2024-04-18,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.