Home > database >  Looping Dunn's test with purr package
Looping Dunn's test with purr package

Time:10-31

I would like to run a Dunn test with the purrr package. However, the tidy function to extract the parameter/results seemed to do not work with this test. Any one has sugesstion for me?

My example.

df <- data.frame(Type = c("n1", "n1", "n2", "n2", "n3", "n3", "n4", "n4", "n1", "n1", "n2", "n2", "n3", "n3", "n4", "n4"),
                 group = c("gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr1", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2", "gr2"),
                 value = runif(16, min = 1, max = 30))

tbl <- df %>%
  mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>% 
  nest(data = -group) %>% 
  mutate(fit = map(data, ~dunnTest(value~Type, data= .x, method = "bh")),
         tidied = map(fit, tidy))

Error: Problem with `mutate()` column `tidied`.
i `tidied = map(fit, tidy)`.
x No tidy method for objects of class dunnTest
Run `rlang::last_error()` to see where the error occurred.          

CodePudding user response:

The dunnTest (assuming it is from FSA) returns a list as output. We may need to extract the 'res' element

library(dplyr)
library(tidyr)
library(FSA)
df %>%
    mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
    nest_by(group) %>%
    mutate(fit = list(dunnTest(value~Type, data= data, method = "bh")$res)) %>% 
    ungroup %>%
    unnest(c(fit))

-output

# A tibble: 12 × 6
   group               data Comparison      Z P.unadj P.adj
   <chr> <list<tibble[,2]>> <chr>       <dbl>   <dbl> <dbl>
 1 gr1              [8 × 2] n1 - n2     0.408   0.683 0.683
 2 gr1              [8 × 2] n1 - n3    -0.612   0.540 0.648
 3 gr1              [8 × 2] n2 - n3    -1.02    0.307 0.615
 4 gr1              [8 × 2] n1 - n4     1.02    0.307 0.922
 5 gr1              [8 × 2] n2 - n4     0.612   0.540 0.810
 6 gr1              [8 × 2] n3 - n4     1.63    0.102 0.615
 7 gr2              [8 × 2] n1 - n2     1.43    0.153 0.918
 8 gr2              [8 × 2] n1 - n3     0.816   0.414 0.828
 9 gr2              [8 × 2] n2 - n3    -0.612   0.540 0.810
10 gr2              [8 × 2] n1 - n4     1.02    0.307 0.922
11 gr2              [8 × 2] n2 - n4    -0.408   0.683 0.820
12 gr2              [8 × 2] n3 - n4     0.204   0.838 0.838

If we don't need the 'data' column either select out or use transmute

df %>%
    mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>%
    nest_by(group) %>%
    transmute(fit = list(dunnTest(value~Type, data= data, 
         method = "bh")$res)) %>% 
    ungroup %>%
    unnest(c(fit))

-output

# A tibble: 12 × 5
   group Comparison      Z P.unadj P.adj
   <chr> <chr>       <dbl>   <dbl> <dbl>
 1 gr1   n1 - n2     0.408   0.683 0.683
 2 gr1   n1 - n3    -0.612   0.540 0.648
 3 gr1   n2 - n3    -1.02    0.307 0.615
 4 gr1   n1 - n4     1.02    0.307 0.922
 5 gr1   n2 - n4     0.612   0.540 0.810
 6 gr1   n3 - n4     1.63    0.102 0.615
 7 gr2   n1 - n2     1.43    0.153 0.918
 8 gr2   n1 - n3     0.816   0.414 0.828
 9 gr2   n2 - n3    -0.612   0.540 0.810
10 gr2   n1 - n4     1.02    0.307 0.922
11 gr2   n2 - n4    -0.408   0.683 0.820
12 gr2   n3 - n4     0.204   0.838 0.838

The output is already a tidyied data.frame if we extract the list i.e. check the str of the output

> str(dunnTest(value ~ Type, data = subset(df, group == 'gr1')))
List of 3
 $ method: chr "Holm"
 $ res   :'data.frame': 6 obs. of  4 variables:
  ..$ Comparison: chr [1:6] "n1 - n2" "n1 - n3" "n2 - n3" "n1 - n4" ...
  ..$ Z         : num [1:6] 0.408 -0.612 -1.021 1.021 0.612 ...
  ..$ P.unadj   : num [1:6] 0.683 0.54 0.307 0.307 0.54 ...
  ..$ P.adj     : num [1:6] 0.683 1 1 1 1 ...
 $ dtres : chr [1:22] "  Kruskal-Wallis rank sum test" "" "data: x and g" "Kruskal-Wallis chi-squared = 2.8333, df = 3, p-value = 0.42" ...
 - attr(*, "class")= chr "dunnTest"

There is no need for the purrr to be used here if we are using nest_by which does rowwise attribute. In case, we are only nesting then

library(purrr)
df %>%
  mutate(Type = factor(Type, levels = c("n1", "n2", "n3", "n4"))) %>% 
  nest(data = -group) %>% 
  mutate(fit = map(data, ~dunnTest(value~Type, data= .x, method = "bh")$res)) %>%
  unnest(fit)

-output

# A tibble: 12 × 6
   group data             Comparison      Z P.unadj P.adj
   <chr> <list>           <chr>       <dbl>   <dbl> <dbl>
 1 gr1   <tibble [8 × 2]> n1 - n2     0.408   0.683 0.683
 2 gr1   <tibble [8 × 2]> n1 - n3    -0.612   0.540 0.648
 3 gr1   <tibble [8 × 2]> n2 - n3    -1.02    0.307 0.615
 4 gr1   <tibble [8 × 2]> n1 - n4     1.02    0.307 0.922
 5 gr1   <tibble [8 × 2]> n2 - n4     0.612   0.540 0.810
 6 gr1   <tibble [8 × 2]> n3 - n4     1.63    0.102 0.615
 7 gr2   <tibble [8 × 2]> n1 - n2     1.43    0.153 0.918
 8 gr2   <tibble [8 × 2]> n1 - n3     0.816   0.414 0.828
 9 gr2   <tibble [8 × 2]> n2 - n3    -0.612   0.540 0.810
10 gr2   <tibble [8 × 2]> n1 - n4     1.02    0.307 0.922
11 gr2   <tibble [8 × 2]> n2 - n4    -0.408   0.683 0.820
12 gr2   <tibble [8 × 2]> n3 - n4     0.204   0.838 0.838
  • Related