r - 在嵌套 tibble 的两列上应用 purrr::map2
问题描述
这个问题涉及 . 语言中的操作tidyverse
。我正在尝试在 a 的两列上使用tidyr::nest
和purrr:map2
执行双变量函数tibble
,用其他两列替换它们,这是该双变量函数的结果。该操作是根据下的统计值计算 ROC 的操作,H0
它H1
产生两个新值(即列)FPR
和TPR
。这是一个工作示例:
library(tidyverse)
library(purrr)
# function to compute the rejection rates
get_reject_freq <- function(Tstat, th_vec, twosided=T) {
# Tstat is a vector, th could be a vector of thresholds threshold
if (twosided) Tstat <- abs(Tstat)
sapply(th_vec, function(th) mean(Tstat > th))
}
# function to compute the ROC
get_ROC <- function(T0, T1, twosided=T) {
T0_sorted <- sort(unique(T0), decreasing = T)
tibble(FPR = get_reject_freq(T0, T0_sorted, twosided = twosided),
TPR = get_reject_freq(T1, T0_sorted, twosided = twosided))
}
n = m = 15
run_sims_one_iter <- function(j) {
x = rt(n, df=5, ncp=0)
y = list(H0=rt(m, df=5, ncp=0), H1=rt(m, df=5, ncp=1))
result = NULL
for (h in c("H0","H1")) {
result[[h]] = tibble(method="t_test", H=h,
test_stat=t.test(x,y[[h]])$statistic) %>%
add_row(method="wilcoxon", H=h,
test_stat=wilcox.test(x,y[[h]], alternative = "two.sided")$statistic, )
}
return( bind_rows(result) )
}
result = bind_rows( lapply(1:100, run_sims_one_iter) )
#### The following can hopefully be improved ###
temp = result %>%
group_by(method,H) %>%
nest() %>%
pivot_wider(names_from = H, values_from = data) %>%
ungroup()
roc_results = bind_rows(
lapply(1:nrow(temp), function(i) {
get_ROC( temp[[i,"H0"]]$test_stat, temp[[i,"H1"]]$test_stat) %>%
add_column(method = temp[i,]$method)
}
))
线
temp = result %>%
group_by(method,H) %>%
nest() %>%
pivot_wider(names_from = H, values_from = data) %>%
ungroup()
产生以下形式的输出:
# A tibble: 2 x 3
method H0 H1
<chr> <list> <list>
1 t_test <tibble [100 × 1]> <tibble [100 × 1]>
2 wilcoxon <tibble [100 × 1]> <tibble [100 × 1]>
代码应该对每一行进行操作,将两个小标题放入H0
和H1
列,将它们传递给get_ROC
函数并用FPR
和TPR
列替换它们,然后是unnest
所有内容。roc_result
上述代码生成的期望是
roc_results
# A tibble: 157 x 3
FPR TPR method
<dbl> <dbl> <chr>
1 0.03 0.76 t_test
2 0.04 0.77 t_test
3 0.07 0.82 t_test
...
理想情况下,我想用单行形式替换temp
and的构造:roc_results
temp = result %>%
group_by(method,H) %>%
nest() %>%
pivot_wider(names_from = H, values_from = data) %>%
ungroup() %>%
mutate(res=map2(unlist(H0), unlist(H1), get_ROC)) %>% unnest(res)
但这不起作用。我想问题可能是get_ROC
每行的输出大小可能会发生变化(?)。知道如何使用该tidyverse
方法执行所有操作。
解决方案
你的方向是正确的,但你必须unlist
在函数中map2
而不是在参数中。
library(dplyr)
library(tidyr)
result %>%
group_by(method,H) %>%
nest() %>%
pivot_wider(names_from = H, values_from = data) %>%
mutate(res = purrr::map2(H0, H1, ~get_ROC(unlist(.x), unlist(.y)))) %>%
unnest(res) %>%
select(-c(H0, H1))
# method FPR TPR
# <chr> <dbl> <dbl>
# 1 t_test 0.01 0.49
# 2 t_test 0.06 0.59
# 3 t_test 0.08 0.65
# 4 t_test 0.1 0.74
# 5 t_test 0.11 0.77
# 6 t_test 0.13 0.82
# 7 t_test 0.19 0.84
# 8 t_test 0.21 0.84
# 9 t_test 0.22 0.85
#10 t_test 0.24 0.86
# … with 156 more rows
推荐阅读
- html - 每当从一个 HTML 转换到另一个 HTML 时,导航栏的大小都会发生变化
- regex - 为 fail2ban 创建一个 cakephp 过滤器
- javascript - 将 async/await 与 for 循环和 mongodb 一起使用
- node.js - 找不到模块 './version' - Azure DevOps 上的 npm 安装失败
- java - 初学者 Java 项目:我的数组有什么问题?
- javascript - 如何基于json创建动态数组?
- python - PySpark 计数 groupby 与 None 键
- python - 从显示为“小时:分钟:秒.毫秒”的列值中获取毫秒作为双精度
- javascript - 有什么方法可以在 React 或 ES6+ 中排序对象?
- php - 将基于 ACF 字段的自定义“添加到购物车”按钮添加到 WooCommerce 单品页面