在带有 `map2` 的 `mutate` 语句中使用 `metafor::robust`

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

我过去经常使用的代码现在导致错误。我不确定错误是源于

map2
还是
robust
函数,尽管我怀疑错误来自
map2
。关于解决
robust
错误的任何建议?

library(dplyr)
library(purrr)
library(tidyr)
library(metafor)

df <- mtcars %>%   
  group_by(cyl) %>%
  nest() %>% # make everything into a dataset
  ungroup() %>% 
  mutate(meta_main = map(data, ~rma.uni(yi = .x$mpg, vi = .x$hp, control=list(maxiter=1000))))


df %>% 
  mutate(robust_meta = purrr::map2(.x = meta_main,
                                   .y = data, 
                                   .f = ~robust(x = .x, cluster = .y$vs)))
#> Error in `mutate()`:
#> ℹ In argument: `robust_meta = purrr::map2(.x = meta_main, .y = data, .f
#>   = ~robust(x = .x, cluster = .y$vs))`.
#> Caused by error in `purrr::map2()`:
#> ℹ In index: 1.
#> Caused by error:
#> ! Cannot find the object/variable ('.y$vs') specified for the 'cluster' argument.
#> Backtrace:
#>      ▆
#>   1. ├─df %>% ...
#>   2. ├─dplyr::mutate(...)
#>   3. ├─dplyr:::mutate.data.frame(...)
#>   4. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   5. │   ├─base::withCallingHandlers(...)
#>   6. │   └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   7. │     └─mask$eval_all_mutate(quo)
#>   8. │       └─dplyr (local) eval()
#>   9. ├─purrr::map2(.x = meta_main, .y = data, .f = ~robust(x = .x, cluster = .y$vs))
#>  10. │ └─purrr:::map2_("list", .x, .y, .f, ..., .progress = .progress)
#>  11. │   ├─purrr:::with_indexed_errors(...)
#>  12. │   │ └─base::withCallingHandlers(...)
#>  13. │   ├─purrr:::call_with_cleanup(...)
#>  14. │   └─.f(.x[[i]], .y[[i]], ...)
#>  15. │     ├─metafor::robust(x = .x, cluster = .y$vs)
#>  16. │     └─metafor::robust.rma.uni(x = .x, cluster = .y$vs)
#>  17. │       └─metafor:::.getx("cluster", mf = mf, data = x$data)
#>  18. │         └─base::stop(...)
#>  19. └─base::.handleSimpleError(...)
#>  20.   └─purrr (local) h(simpleError(msg, call))
#>  21.     └─cli::cli_abort(...)
#>  22.       └─rlang::abort(...)

创建于 2023-04-04 与 reprex v2.0.2

r purrr metafor
© www.soinside.com 2019 - 2024. All rights reserved.