我试图使用R包report
的report
来获得2个Wilcoxon测试的结果报告,每个测试对应一个Session
。
我可以获得测试结果,但不能获得文本报告。
我找不到任何使用purr
和report
的例子。
library(tidyverse)
library(report)
df <- tibble(Session = c(rep("1", 10), rep("2", 10)),
scores = sample(20:70, 20),
group = c(rep("before", 5), rep("after",5),
rep("before", 5), rep("after",5)))
# function to compute Wilcoxon test
w_test <- function(df) {
wilcox.test(scores ~ group, df, paired = TRUE)
}
# testing w_test function for Session 1
dfS1 <- df %>% filter(Session == "1")
w_test(dfS1)
#>
#> Wilcoxon signed rank exact test
#>
#> data: scores by group
#> V = 4, p-value = 0.4375
#> alternative hypothesis: true location shift is not equal to 0
# this does not work because I can't map wtest to report::report
w <- df %>%
group_by(Session) %>%
nest() %>%
mutate(wtest = map(data, w_test),
result = map(wtest, report))
#> Could not compute effectsize NA.
#> Possible reason: Unable to retrieve data from htest object.
#> Try using
#> 'rb()' directly.
#> Error in `mutate()`:
#> ℹ In argument: `result = map(wtest, report)`.
#> ℹ In group 1: `Session = "1"`.
#> Caused by error in `map()`:
#> ℹ In index: 1.
#> Caused by error in `abs()`:
#> ! non-numeric argument to mathematical function
#> Backtrace:
#> ▆
#> 1. ├─df %>% group_by(Session) %>% nest() %>% ...
#> 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::map(wtest, report)
#> 10. │ └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#> 11. │ ├─purrr:::with_indexed_errors(...)
#> 12. │ │ └─base::withCallingHandlers(...)
#> 13. │ ├─purrr:::call_with_cleanup(...)
#> 14. │ ├─report (local) .f(.x[[i]], ...)
#> 15. │ └─report:::report.htest(.x[[i]], ...)
#> 16. │ ├─report::report_table(x, model_info = model_info, ...)
#> 17. │ └─report:::report_table.htest(x, model_info = model_info, ...)
#> 18. │ ├─base::do.call(report_effectsize, args)
#> 19. │ ├─report (local) `<fn>`(`<htest>`, model_info = `<named list>`)
#> 20. │ └─report:::report_effectsize.htest(`<htest>`, model_info = `<named list>`)
#> 21. │ └─report:::.report_effectsize_wilcox(x, table, dot_args)
#> 22. │ ├─base::do.call(effectsize::interpret_r, args)
#> 23. │ └─effectsize (local) `<fn>`(NULL)
#> 24. │ └─effectsize::interpret(abs(r), rules)
#> 25. └─base::.handleSimpleError(...)
#> 26. └─purrr (local) h(simpleError(msg, call))
#> 27. └─cli::cli_abort(...)
#> 28. └─rlang::abort(...)
w$report
#> Error in eval(expr, envir, enclos): object 'w' not found
Created on 2023-10-01 with reprex v2.0.2
1条答案
按热度按时间enxuqcxy1#
要使
report()
与htest
对象一起工作,您可能需要从wilcox.test()
中删除data
参数并相应地更改公式(relevant github issue):改变功能:
根据您需要报告的内容,您还可以直接从
htest
对象中提取值,或者通过broom::tidy()
而不是report()
传递值。另一个替代方案是
{rstatix}
,它被设计为“管道友好”,并与分组帧一起工作:创建于2023-10-01使用reprex v2.0.2