Hi, I have data as below. How can I do pairwise comparisons following a significant exact multinomial test?
I found that rstatix package support similar function. The input value shoul be the counts, However, my input is the value.
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.2.3
#> Warning: package 'tibble' was built under R version 4.2.3
#> Warning: package 'dplyr' was built under R version 4.2.3
#> Warning: package 'forcats' was built under R version 4.2.3
#> Warning: package 'lubridate' was built under R version 4.2.3
data <- tibble(
id = rep(c("treatment", "control", "test"), 12),
A = rep(c(rep("aa", 6), rep("bb", 6), rep("cc",6)), 2),
B = runif(36)
)
groups <- unique(data$id)
combinations <- data.frame(t(combn(unique(data$A), 2)), stringsAsFactors = FALSE)
names(combinations) <- c("col1", "col2")
combinations$Comparison <- paste0(combinations$col1, "_vs_", combinations$col2)
result_list <- set_names(map(groups, ~{
group <- .x
combinations %>%
group_by(Comparison) %>%
summarise(
Success_Count = sum(data$B[data$A == col2 & data$id == group] > data$B[data$A == col1 & data$id == group]),
n_trials = sum(data$id == group & data$A %in% c(col1, col2)),
p_value = binom.test(Success_Count, n_trials, p = 0.5, alternative = "greater")$p.value
)
}), groups)
result <- bind_rows(result_list, .id = "Group")
Created on 2023-05-24 with reprex v2.0.2