Reputation: 4949
I have two factors, day and the other group. The value is how many totals in each group.
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(1,2,3,4,5,6,7,8,9,10,11,12)
day <- as.character (c ( 1,2,3,4,5,6,7,8,9,10,11,12) )
df1 <- data.frame(x, y, day)
df2 <- reshape2::melt(df1, id.vars='day')
colnames (df2)[2] = "group"
> df2
day group value
1 1 x 5
2 2 x 17
3 3 x 31
4 4 x 9
5 5 x 17
6 6 x 10
7 7 x 30
8 8 x 28
9 9 x 16
10 10 x 29
11 11 x 14
12 12 x 34
13 1 y 1
14 2 y 2
15 3 y 3
16 4 y 4
17 5 y 5
18 6 y 6
19 7 y 7
20 8 y 8
21 9 y 9
22 10 y 10
23 11 y 11
24 12 y 12
so in the above example there are a total of 5 in group x and total of 1 for y in day 1. I would like to determine significance for each unique days between x and y, so perhaps a chisquare test?
I run the code as such but for some reason, it keeps implying that the group is not found?
df2 %>% group_by(day) %>%
rstatix::chisq_test( day, group ) %>%
add_significance("p.adj")
Can anyone help with this, thanks in advance.
Upvotes: 1
Views: 81
Reputation: 887231
Perhaps this helps
library(dplyr)
library(purrr)
library(rstatix)
df2 %>%
mutate(day = as.numeric(day)) %>%
split(.$day) %>%
map_dfr(~ with(.x, chisq_test(setNames(value, group))), .id = 'day')
-output
# A tibble: 12 × 7
day n statistic p df method p.signif
<chr> <int> <dbl> <dbl> <dbl> <chr> <chr>
1 1 2 2.67 0.102 1 Chi-square test ns
2 2 2 11.8 0.000579 1 Chi-square test ***
3 3 2 23.1 0.00000157 1 Chi-square test ****
4 4 2 1.92 0.166 1 Chi-square test ns
5 5 2 6.55 0.0105 1 Chi-square test *
6 6 2 1 0.317 1 Chi-square test ns
7 7 2 14.3 0.000156 1 Chi-square test ***
8 8 2 11.1 0.000858 1 Chi-square test ***
9 9 2 1.96 0.162 1 Chi-square test ns
10 10 2 9.26 0.00235 1 Chi-square test **
11 11 2 0.36 0.549 1 Chi-square test ns
12 12 2 10.5 0.00118 1 Chi-square test **
Or could use group_modify
df2 %>%
group_by(day = as.numeric(day)) %>%
group_modify(~ with(.x, chisq_test(setNames(value, group)))) %>%
ungroup
# A tibble: 12 × 7
day n statistic p df method p.signif
<dbl> <int> <dbl> <dbl> <dbl> <chr> <chr>
1 1 2 2.67 0.102 1 Chi-square test ns
2 2 2 11.8 0.000579 1 Chi-square test ***
3 3 2 23.1 0.00000157 1 Chi-square test ****
4 4 2 1.92 0.166 1 Chi-square test ns
5 5 2 6.55 0.0105 1 Chi-square test *
6 6 2 1 0.317 1 Chi-square test ns
7 7 2 14.3 0.000156 1 Chi-square test ***
8 8 2 11.1 0.000858 1 Chi-square test ***
9 9 2 1.96 0.162 1 Chi-square test ns
10 10 2 9.26 0.00235 1 Chi-square test **
11 11 2 0.36 0.549 1 Chi-square test ns
12 12 2 10.5 0.00118 1 Chi-square test **
Upvotes: 2