如何在 R 中使用 tidyeval 创建一个包含所有级别因子的小型预测数据集?

How do I use tidyeval in R to create a small prediction dataset that has all levels of factors?

我正在尝试使用 modelr::data_grid() 函数获得一个预测网格,该函数具有模型的一个因子的所有水平,而模型中的另一个因子只有“典型”水平。问题在于,对预测网格中保持不变的因子的调用 levels() 应该 return 原始数据中存在的所有级别。否则,一些预测函数可能会失败,return 臭名昭著的

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

这里展示了我可以从 modelr::data_grid() 中得到什么,以及我想要什么。

library(tidyverse)

co2_mod <- slice(CO2, -1, -43)
fit <- lm(uptake ~ Type + Treatment, data = co2_mod)

grid_df <- modelr::data_grid(co2_mod, Type, .model = fit) %>%
  mutate(Treatment = factor(Treatment))
grid_df
#> # A tibble: 2 x 2
#>   Type        Treatment
#>   <fct>       <fct>    
#> 1 Quebec      chilled  
#> 2 Mississippi chilled
levels(grid_df$Treatment)
#> [1] "chilled"

我想要

levels(grid_df$Treatment)
[1] "nonchilled" "chilled"

数据集中只有 1 个水平的因子变量应该“知道”原始数据集中所有可能的因子。例如,请参阅下面 rstanarm 小插图的屏幕截图,该小插图显示需要将 MaleFemale 作为级别,即使预测是保持 gender 等于Female.


我已经尝试编写几个函数来实现我的目标。有一个重复数据集 1000 次的额外步骤,因为我将此网格用作 newdata 来自 rstanarm 模型拟合的后验预测分布。

辅助功能(有效):

get_all_levels <- function(v, dataset){
  v <- enquo(v)
  
  a <- select(dataset, !!v) %>% as_vector()
  l <- levels(a)
  
  return(l)
}

主函数,调用辅助函数(这个不行):

grid_all_levels <- function(model, variable, df){
  variable <- enquo(variable)
  grid <- expand_grid(
    rep = seq(1, 1000, 1), 
    modelr::data_grid(df, !!variable, .model = model)
  ) %>%
    mutate(across(where(is.character), ~factor(., levels = get_all_levels(., df))))
  
  return(grid)
}

这是一个小的 reprex,您可以 运行 这些函数。

library(tidyverse)
  
get_all_levels <- function(v, dataset){
  v <- enquo(v)
  d <- as_tibble(dataset)
  a <- select(d, !!v) %>% as_vector()
  l <- levels(a)
  
  return(l)
}

grid_all_levels <- function(model, variable, df){
  variable <- enquo(variable)
  grid <- expand_grid(
    rep = seq(1, 1000, 1), 
    modelr::data_grid(df, !!variable, .model = model)
  ) %>%
    mutate(across(where(is.character), ~factor(., levels = get_all_levels(., df))))
  
  return(grid)
}

co2_mod <- slice(CO2, -1, -43)
fit <- lm(conc ~ Type + Treatment, data = co2_mod)
get_all_levels(Type, co2_mod)
#> [1] "Quebec"      "Mississippi"

grid_all_levels(fit, Type, co2_mod)
#> Note: Using an external vector in selections is ambiguous.
#> ℹ Use `all_of(.)` instead of `.` to silence this message.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
#> This message is displayed once per session.
#> Error: Problem with `mutate()` input `..1`.
#> x Can't subset columns that don't exist.
#> x Columns `chilled`, `chilled`, `chilled`, `chilled`, `chilled`, etc. don't exist.
#> ℹ Input `..1` is `across(...)`.

我很确定问题出在我如何在 grid_all_levels 函数的 get_all_levels 调用中使用 .

如何让 grid_all_levels() 工作?


更新

我根据以下答案修改了 grid_all_levels() 函数,使其看起来像这样:

grid_all_levels <- function(model, variable, df){
  variable <- enquo(variable)
  grid <- expand_grid(
    rep = seq(1, 1000, 1), 
    modelr::data_grid(df, !!variable, .model = model)
  ) %>%
    mutate(across(where(is.character), ~factor(., levels = get_all_levels(cur_column(), df))))
  
  return(grid)
}

哪个return

#> # A tibble: 2,000 x 3
#>      rep Type        Treatment
#>    <dbl> <fct>       <fct>    
#>  1     1 Quebec      chilled  
#>  2     1 Mississippi chilled  
#>  3     2 Quebec      chilled  
#>  4     2 Mississippi chilled  
#>  5     3 Quebec      chilled  
#>  6     3 Mississippi chilled  
#>  7     4 Quebec      chilled  
#>  8     4 Mississippi chilled  
#>  9     5 Quebec      chilled  
#> 10     5 Mississippi chilled  
#> # … with 1,990 more rows
levels(a$Treatment)
#> [1] "nonchilled" "chilled"

这正是我想要的!

问题是交叉内的 . 是实际的列而不是列的名称。遗憾的是,无法在 across 调用中获取名称,但是使用 purrr::reduce 我们可以解决此问题:

library(tidyverse)
levelize <- function(.x, .y){
 mutate(.x, !!paste0(.y) := factor(!!sym(.y), levels =  get_all_levels(!!sym(.y), df) ) )
}

estimate_prevalence_differences_polr <- function(model, variable, df){
  variable <- enquo(variable)
  # putting the names of the factor variables inside a vector
  vars <- sapply(df, function(x) is.factor(x))
  vars <- names(vars)[vars]
  expand_grid(
    rep = 1:1000, 
    modelr::data_grid(df, !!variable, .model = model)
  ) %>% 
### looping through the vars that are factor
## the .x argument is the one set by .init i.e the grid
## the .y argument is the element of the vars vector in the current iter
## the first iter we have .y="Type" and .x=.
## inside the levelize function
## basically mutate .x and change the variable named .y (Type) to become a factor
## with the levels returned from the original df
    reduce(intersect(vars, colnames(.)), levelize, .init=.)
}

现在调用函数后级别设置正确,我们可以通过打印每个 factor 列的唯一值来检查这一点:

estimate_prevalence_differences_polr(fit, Type, df) %>% select(where(is.factor)) %>% map(unique)
$Type
[1] Quebec      Mississippi
Levels: Quebec Mississippi

$Plant
[1] Qc2
Levels: Qn1 Qn2 Qn3 Qc1 Qc3 Qc2 Mn3 Mn2 Mn1 Mc2 Mc3 Mc1

$Treatment
[1] chilled    nonchilled
Levels: nonchilled chilled

编辑:

一种更简单的解决方案是使用 cur_column 我第一次写答案时并没有考虑这个选项。所以基本上不是将 . 传递给 get_all_levels 而是传递 cur_column()

expand_grid(
    rep = seq(1, 1000, 1), 
    modelr::data_grid(df, !!variable, .model = model)
  ) %>%
    mutate(across(where(is.character), ~factor(., levels = get_all_levels(cur_column(), df))))

我认为你让它变得比它需要的更复杂。如果我删除所有 tidyeval 的东西,我们会得到更简单的东西:

mutate(across(where(is.character), ~ factor(., levels = levels(.))))

您可以将 ~ lambda 提取到可重复使用的命名函数中。

不过,此实现会立即让我想到一些问题。由于 levels() returns NULL 具有字符向量,因此这仅适用于因子。加上因素,这是 no-op。我不确定你想要达到什么目的。

另外,get-all-levels 实现有一个问题:它需要一个 selection,它可以 return 多个向量,例如starts_with()。在这种情况下,你会得到一个令人困惑的错误。