I have no problem running a t.test on nested data (df). But when I try to use t_test from the rstatix package I get an error.
library(tidyverse)
library(rstatix)
#>
#> Attaching package: 'rstatix'
#> The following object is masked from 'package:stats':
#>
#> filter
df <- ToothGrowth
df$dose <- as.factor(df$dose)
nested_data <- df %>%
group_by(dose) %>%
nest()
# This works
nested_models1 <- nested_data %>%
mutate(t_test1 = map(data, ~t.test(.x$len ~ .x$supp, paired = TRUE,
detailed = TRUE)))
nested_models1
#> # A tibble: 3 × 3
#> # Groups: dose [3]
#> dose data t_test1
#> <fct> <list> <list>
#> 1 0.5 <tibble [20 × 2]> <htest>
#> 2 1 <tibble [20 × 2]> <htest>
#> 3 2 <tibble [20 × 2]> <htest>
# This does not work
nested_models2 <- nested_data %>%
mutate(t_test2 = map(data, ~rstatix::t_test(.x$len ~ .x$supp, paired = TRUE,
detailed = TRUE)))
#> Error in `mutate()`:
#> ℹ In argument: `t_test2 = map(data, ~rstatix::t_test(.x$len ~ .x$supp,
#> paired = TRUE, detailed = TRUE))`.
#> ℹ In group 1: `dose = 0.5`.
#> Caused by error in `map()`:
#> ℹ In index: 1.
#> Caused by error in `rstatix::t_test()`:
#> ! argument "formula" is missing, with no default
#> Backtrace:
#> ▆
#> 1. ├─nested_data %>% ...
#> 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(data, ~rstatix::t_test(.x$len ~ .x$supp, paired = TRUE, detailed = TRUE))
#> 10. │ └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
#> 11. │ ├─purrr:::with_indexed_errors(...)
#> 12. │ │ └─base::withCallingHandlers(...)
#> 13. │ ├─purrr:::call_with_cleanup(...)
#> 14. │ └─.f(.x[[i]], ...)
#> 15. │ └─rstatix::t_test(.x$len ~ .x$supp, paired = TRUE, detailed = TRUE)
#> 16. │ └─rstatix:::get_formula_left_hand_side(formula)
#> 17. │ └─base::deparse(formula[[2]])
#> 18. └─base::.handleSimpleError(...)
#> 19. └─purrr (local) h(simpleError(msg, call))
#> 20. └─cli::cli_abort(...)
#> 21. └─rlang::abort(...)
Created on 2023-09-15 with reprex v2.0.2
A nice thing about
rstatixis the way it handles grouped frames, so instead of a nesting you might want to try with just grouping. You can then joint_testresult to your nested tibble. Alternatively, you can switch to rowwise operation and applyt_teston nested tibbles through mutate.Created on 2023-10-02 with reprex v2.0.2