'R - Multiple chi square test on dataframe per row - get results same dataframe
I'm interested in performing chi-square test of group 1 x group2 (per gene/row) and group1 x group3 (per gene/row) and get the p-value and residuals in the same data frame. Creating columns for each comparison.
Genes<-c("GENE_A", "GENE_B","GENE_C")
Group1_Mut<-c(20,10,5)
Group1_WT<-c(40,50,55)
Group2_Mut<-c(10, 30, 10)
Group2_WT<-c(80, 60, 80)
Group3_Mut <- c(10,15,30)
Group3_WT <- c(30,40,45)
main<-data.frame(Genes,Group1_Mut,Group1_WT,Group2_Mut,Group2_WT, Group3_Mut,Group3_WT)
First I tried the example I found here at stackoverflow (but just for two groups comparations)
library(dplyr)
main %>%
rowwise() %>%
mutate(
chisq.statistic = chisq.test(matrix(c(Group1_Mut, Group1_WT, Group2_Mut, Group2_WT),
nrow = 2))$statistic
)
The chi-square value doesn´t match another statistics program
I tried again for just two groups:
main2 <- select(main,c(Group1_Mut, Group1_WT, Group2_Mut, Group2_WT))
main2 %>%
rowwise() %>%
mutate (statistics = chisq.test(main2))
but got this error:
Error: Problem with `mutate()` column `statistics`.
i `statistics = chisq.test(main2)`.
x `statistics` must be a vector, not a `htest` object.
i Did you mean: `statistics = list(chisq.test(main2))` ?
i The error occurred in row 1.
Then tried this:
main2 %>%
rowwise() %>%
mutate (statistics = list(chisq.test(main2)))
got:
Group1_Mut Group1_WT Group2_Mut Group2_WT statistics
<dbl> <dbl> <dbl> <dbl> <list>
1 20 40 10 80 <htest>
2 10 50 30 60 <htest>
3 5 55 10 80 <htest>
Any ideas on how can I do this test? Is there any functions that perform Chi-square on multiple comparisons?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
