我是 R 新手。所以这是我的问题,我想进行比例的成对比较。在我的数据中,我有三个会话和每个会话的时间段。我添加了每个时隙的计数,并知道 M1 会话的样本大小 = 181、M2 = 67 和 M3 = 106。 我想比较每个会话、每个时段的比例。就像时间段 18h-19h 一样,我有来自 M1vsM2 、M2 vs M3 和 M1 vs M3 的 p 值。
data <- 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:我编辑了数据。
有人可以帮忙吗?谢谢
我希望获得每个时间段内每种会话组合的 p 值。谢谢
您可以对每一行使用
pairwise.prop.test()
。
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')))
# 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