No tidy method for objects of class formula using friedman.test in R

Viewed 40

here the datasets with repeated measure

alt=structure(list(groupter = c(1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 
1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L), screen = c(12.2, 
24.4, 13.5, 18.5, 13.9, 16.6, 12, 16.6, 13.5, 15.9, 11.5, 9.6, 
44, 22.2, 17.1, 31.2, 13.7, 39.9, 11.5, 20, 27.5, 18.5, 22.2, 
21.9, 18.3, 42.1, 16.4, 16.6, 12, 28.7, 10.3, 33.6, 10.1, 22.7, 
7.2, 28, 16.4, 13.2), vizit.1.day.2 = c(16.1, 9.8, 9.3, 21, 11.3, 
9.8, 11.3, 16.6, 15.4, 14.9, 11, 10.3, 22.7, 15.4, 33.3, 15.2, 
9.3, 32.1, 10.3, 13.9, 32.1, 14.4, 23.2, 17.1, 17.8, 27, 15.4, 
29.9, 12.2, 16.8, 9.6, 18.1, 10.5, 15.4, 13.2, 11.5, 20, 9.6), 
    vizit.2.day.9 = c(10.1, 16.4, 11.5, 21.9, 20, 12.5, 12.5, 
    13.5, 14.9, 17.1, 10.8, 11, 21.7, 14.4, 16.4, 34.5, 9.3, 
    23.6, 12, 12.5, 32.6, 11.3, 19.3, 16.4, 12.2, 30.7, 12, 28, 
    14.4, 17.1, 9.8, 22.7, 11.5, 13.2, 11.5, 10.5, 13.9, 14.9
    ), vizit.3.day.16 = c(22.7, 12.7, 22.4, 16.4, 12.2, 11, 10.8, 
    13, 13, 12.5, 9.6, 8.6, 17.8, 12.2, 13.5, 22.4, 8.4, 26.8, 
    14.4, 11.8, 72.9, 8.6, 19.5, 16.4, 14.2, 32.8, 12, 27.5, 
    9.1, 13, 9.3, 18.1, 11, 10.8, 12.7, 24.6, 13, 13.5), vizit.4.day.23 = c(23.9, 
    14, 11.2, 13.7, 21.1, 10.5, 15.6, 18.6, 13.7, 14.2, 12.4, 
    7.5, 20.9, 15.6, 13.7, 20.7, 8.2, 44, 10.7, 10.3, 32.2, 7, 
    20.2, 11.7, 29, 23.2, 10.7, 23.9, 9.8, 11.4, 9.1, 19.5, 8.7, 
    11.9, 11.7, 11.4, 20, 10.7), vizit.5.day.29 = c(13.5, 16.7, 
    15.4, 14.9, 44, 11, 14.4, 15.6, 11, 12.6, 11.4, 9.4, 26.2, 
    14, 17.4, 18.8, 10.3, 41.2, 12.6, 11.9, 28.5, 8.4, 20.7, 
    12.8, 24.1, 30.6, 13.7, 26.9, 13.5, 11.9, 10, 8.4, 10, 13, 
    12.4, 11.7, 16.3, 11.2)), class = "data.frame", row.names = c(NA, 
-38L))

i want do test for dependent samples, in this case the Friedman test, but for each study group separately(groupter). i can simple perform friedman.test for example so friedman.test(alt) and will be glad the result. But the main problem that in my datasets there is grouping variable groupter, and I need to do it not for the entire dataset, but for each group separately for groupter = 1 separately and groupter = 2 separately.

 ?friedman.test

does not allow to be divided into groups .

How for each category of groupter perform friedman.test by all metric vars.

from screen to vizit 5 day 29

to get needed output

alt                         
groupter    screen  vizit 1 day 2   vizit 2 day 9   vizit 3 day 16  vizit 4 day 23  vizit 5 day 29  
1                           0,05
2                           0,05

I use group_map but i get error

> if (requireNamespace("broom", quietly = TRUE)) {
+   # a list of tibbles
+   alt %>%
+     group_by(groupter) %>%
+     group_map(~ broom::tidy(friedman.test~., data = .x))}
 Error: No tidy method for objects of class formula 

Please help. As usual any help is greatly appreciated.

1 Answers
 alt %>% 
  group_by(groupter) %>% 
  mutate(id_row = row_number()) %>% 
  pivot_longer(-c(id_row, groupter)) %>% 
  nest() %>% 
  mutate(result = map(data, ~friedman.test(value ~ name | id_row, data = .x))) %>% 
  mutate(out = map(result, broom::tidy)) %>% 
  select(-c(data, result)) %>% 
  ungroup() %>% 
  unnest(out)

  groupter statistic p.value parameter method                
     <int>     <dbl>   <dbl>     <dbl> <chr>                 
1        1      14.5  0.0129         5 Friedman rank sum test
2        2      11.7  0.0389         5 Friedman rank sum test
Related