I am new to R. So here is my problem, I would like to perform a pairwise comparison of proportion. In my data, I have three sessions and time slots per session. I added the count per time slot knowing that sample size for M1 session = 181
, M2 = 67
and M3 = 106
.
I want to compare the proportion of each session, per time slots. Like for time slot 18h-19h I have p-value from M1 vs M2 , M2 vs M3 and M1 vs M3.
data <- tibble::tribble(
~Horaire, ~SessionM1, ~NbM1, ~SessionM2, ~NbM2, ~SessionM3, ~NbM3,
"18h-19h","M1", 6, "M2", 7, "M3", 6,
"19h-20h","M1", 6, "M2", 10, "M3", 17,
"20h-21h","M1", 14, "M2", 10, "M3", 16,
"21h-22h","M1", 23, "M2", 5, "M3", 9,
"22h-23h","M1", 15, "M2", 9, "M3", 4,
"23h-00h","M1", 19, "M2", 4, "M3", 6,
"00h-01h","M1", 19, "M2", 5, "M3", 10,
"01h-02h","M1", 21, "M2", 3, "M3", 7,
"02h-03h","M1", 8, "M2", 5, "M3", 7,
"03h-04h","M1", 15, "M2", 4, "M3", 10,
"04h-05h","M1", 15, "M2", 4, "M3", 9,
"05h-06h","M1", 20, "M2", 1, "M3", 5)
PS: I edited the data.
Can someone help, please? Thanks
I expect to obtain p-values for each combination of sessions within each time slot. Thanks
You can use pairwise.prop.test()
for each row. Different methods for adjusting p-values can be used by p.adjust.method
argument. The default is p.adjust.method = "holm"
. For more choices, see ?p.adjust
.
t(apply(data[grep("NbM", names(data))], 1, \(m) {
p <- pairwise.prop.test(x = m, n = c(181, 67, 106))$p.value
p[!is.na(p)]
})) |>
as.data.frame() |>
setNames(combn(1:3, 2, \(x) paste0('M', x, collapse = 'vs')))
Its tidyverse
version:
library(tidyverse)
data %>%
pivot_longer(starts_with("Nb"), names_prefix = "Nb") %>%
reframe(as.data.frame.table(
pairwise.prop.test(setNames(value, name), c(181, 67, 106))$p.value
), .by = Horaire) %>%
drop_na(Freq) %>%
pivot_wider(names_from = c(Var2, Var1), names_sep = "vs", values_from = Freq)
# M1vsM2 M1vsM3 M2vsM3
# 1 0.166 0.771 0.771
# 2 0.005 0.001 1.000
# 3 0.289 0.232 1.000
# 4 1.000 1.000 1.000
# 5 0.431 0.431 0.121
# 6 0.796 0.707 1.000
# 7 1.000 1.000 1.000
# 8 0.447 0.483 0.803
# 9 1.000 1.000 1.000
# 10 1.000 1.000 1.000
# 11 1.000 1.000 1.000
# 12 0.096 0.211 0.482