rpurrrt-testrstatix

how to set string argument in a function within map function


I have this nested dataset

dput(head(nest2, 2))
structure(list(grp = c("Avoiding", "Savoring"), data = list(structure(list(
    Gender = c("Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men"), Education = structure(c(1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 
    2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 
    2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 1L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("Basic", 
    "Medium", "Higher"), class = "factor"), Efficacy = structure(c(24, 
    24, 29, 28, 29, 22, 30, 26, 16, 22, 26, 30, 32, 20, 24, 31, 
    28, 26, 25, 35, 30, 25, 28, 33, 33, 22, 24, 33, 27, 35, 26, 
    22, 25, 31, 22, 26, 22, 19, 26, 29, 28, 21, 23, 28, 27, 25, 
    22, 25, 27, 21, 25, 26, 23, 25, 21, 27, 27, 24, 17, 28, 20, 
    25, 35, 26, 29, 18, 23, 24, 23, 37, 25, 26, 22, 26, 30, 26, 
    24, 30, 19, 23, 23, 27, 20, 27, 25, 21, 12, 32, 24, 21, 26, 
    26, 26, 26, 20, 10, 25, 23, 17, 20, 22, 25, 23, 21, 23, 21, 
    20, 27, 14, 29, 24, 25, 23, 10, 31, 38, 20, 28, 29, 26, 23, 
    28, 21, 22, 27, 27, 27, 27, 21, 22, 23, 27, 23, 27, 22, 25, 
    27, 25, 20, 25, 25, 24, 27, 25, 24), format.spss = "F16.2"), 
    points = c(9, 10, 12, 10, 13, 11, 10, 8, 5, 6, 7, 8, 12, 
    6, 9, 10, 9, 11, 9, 13, 11, 10, 10, 14, 13, 9, 8, 11, 7, 
    13, 6, 8, 10, 10, 9, 11, 8, 5, 8, 12, 9, 9, 11, 9, 10, 10, 
    8, 9, 9, 10, 8, 9, 8, 9, 10, 9, 14, 8, 5, 11, 5, 7, 14, 8, 
    11, 8, 9, 9, 8, 15, 9, 6, 8, 10, 9, 9, 10, 12, 8, 8, 8, 13, 
    8, 11, 9, 9, 5, 13, 8, 7, 10, 10, 12, 10, 5, 3, 9, 9, 5, 
    6, 7, 8, 7, 6, 8, 6, 7, 16, 7, 10, 7, 7, 5, 4, 11, 16, 6, 
    9, 10, 10, 5, 9, 9, 7, 9, 12, 11, 10, 8, 10, 10, 9, 10, 10, 
    7, 7, 10, 8, 5, 8, 8, 9, 9, 6, 8)), row.names = c(NA, -145L
), class = c("tbl_df", "tbl", "data.frame")), structure(list(
    Gender = c("Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Women", "Women", "Women", "Women", 
    "Women", "Women", "Women", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", "Men", 
    "Men", "Men", "Men", "Men", "Men", "Men"), Education = structure(c(1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 
    2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 
    2L, 3L, 2L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 1L, 2L, 2L, 2L, 2L, 
    2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L), .Label = c("Basic", 
    "Medium", "Higher"), class = "factor"), Efficacy = structure(c(24, 
    24, 29, 28, 29, 22, 30, 26, 16, 22, 26, 30, 32, 20, 24, 31, 
    28, 26, 25, 35, 30, 25, 28, 33, 33, 22, 24, 33, 27, 35, 26, 
    22, 25, 31, 22, 26, 22, 19, 26, 29, 28, 21, 23, 28, 27, 25, 
    22, 25, 27, 21, 25, 26, 23, 25, 21, 27, 27, 24, 17, 28, 20, 
    25, 35, 26, 29, 18, 23, 24, 23, 37, 25, 26, 22, 26, 30, 26, 
    24, 30, 19, 23, 23, 27, 20, 27, 25, 21, 12, 32, 24, 21, 26, 
    26, 26, 26, 20, 10, 25, 23, 17, 20, 22, 25, 23, 21, 23, 21, 
    20, 27, 14, 29, 24, 25, 23, 10, 31, 38, 20, 28, 29, 26, 23, 
    28, 21, 22, 27, 27, 27, 27, 21, 22, 23, 27, 23, 27, 22, 25, 
    27, 25, 20, 25, 25, 24, 27, 25, 24), format.spss = "F16.2"), 
    points = c(20, 22, 25, 21, 22, 11, 21, 19, 15, 18, 23, 24, 
    19, 20, 20, 22, 19, 24, 22, 22, 24, 19, 19, 25, 25, 22, 19, 
    16, 24, 24, 22, 17, 19, 23, 21, 19, 21, 23, 23, 24, 24, 16, 
    21, 21, 17, 19, 17, 22, 20, 15, 16, 21, 17, 18, 19, 21, 17, 
    18, 23, 21, 13, 17, 24, 14, 19, 21, 21, 19, 20, 24, 21, 20, 
    20, 20, 18, 22, 17, 16, 21, 18, 16, 18, 21, 16, 19, 19, 10, 
    23, 19, 16, 18, 14, 12, 18, 17, 7, 24, 15, 21, 16, 11, 17, 
    21, 17, 15, 21, 12, 14, 12, 14, 16, 16, 19, 19, 18, 19, 18, 
    14, 20, 15, 20, 19, 16, 18, 14, 18, 16, 20, 18, 16, 17, 14, 
    12, 14, 14, 19, 12, 18, 13, 18, 18, 13, 23, 18, 19)), row.names = c(NA, 
-145L), class = c("tbl_df", "tbl", "data.frame")))), row.names = c(NA, 
-2L), class = c("tbl_df", "tbl", "data.frame"))

I have performed the t_test with the following code:

library(rstatix)

  nest2 %>% 
  dplyr::mutate(hyp = c(rep('greater',2), 'less', 'greater'), 
                test = purrr::map(data, ~t_test(points ~ Gender, data = ., ref.group = 'Women', detailed = TRUE)), 
                sd = map(data, ~.x %>% group_by(Gender) %>% summarise(sd = sd(.data$points))))%>%
  unnest(test, sd) %>% 
  pivot_wider(names_from = "Gender", 
              values_from = 'sd') %>% rename(sd1 = Men, sd2 = Women)

The output is the following

A tibble: 4 x 20
  grp    data     hyp   estim~1 estim~2 estim~3 .y.   group1 group2    n1    n2 stati~4       p    df conf.~5 conf.~6 method alter~7   sd1   sd2
  <chr>  <list>   <chr>   <dbl>   <dbl>   <dbl> <chr> <chr>  <chr>  <int> <int>   <dbl>   <dbl> <dbl>   <dbl>   <dbl> <chr>  <chr>   <dbl> <dbl>
1 Avoid~ <tibble> grea~  0.310     9.05    8.74 poin~ Women  Men      107    38  0.658  5.13e-1  59.8  -0.632   1.25  T-test two.si~  2.55  2.31
2 Savor~ <tibble> grea~  2.64     19.2    16.6  poin~ Women  Men      107    38  4.75   8.16e-6  84.9   1.53    3.74  T-test two.si~  2.70  3.54
3 Coping <tibble> less  -1.45      9.42   10.9  poin~ Women  Men      107    38 -2.95   4.43e-3  66.0  -2.43   -0.467 T-test two.si~  2.59  2.63
4 Obtai~ <tibble> grea~  0.0305   15.9    15.9  poin~ Women  Men      107    38  0.0498 9.6 e-1  61.7  -1.19    1.25  T-test two.si~  3.29  3.10

But the alternative I would like to set are in the order (there's the alternative argument in t_test function for this that I do not know how to set)

greater
greater
less 
greater

Can anyone give me a small clue? I was thinking to run twice a nested map() but I do not know how, if I am correct. Thanks


Solution

  • To fix the error in your basic code you want to use an anonymous function:

    library(dplyr)
    library(purr)
    
    nest2 %>%
      mutate(test = map(.x = data, .f = \(.x) t_test(.x, points ~ Gender)))
    

    To set the reference group, just add that to the function call:

    nest2 %>%
      mutate(test = map(.x = data, .f = \(.x) t_test(.x, points ~ Gender, ref.group = "Women")))
    

    If you also want to vary the alternative for the tests you could add another column to your data frame and use map2. I only see two rows of data (not 4), but it would look something like this:

    nest2 %>%
      mutate(alternative = c("greater", "less"),
             test = map2(data, alternative, \(.x, .y) {
               t_test(.x, points ~ Gender, ref.group = "Women", alternative = .y)
             }))