我无法想出一个解决方案来解决我在尝试创建自己的问题时遇到的问题 data.frame
并对其进行定量分析(例如 chisq.test
)。
背景如下: 我总结了我收到的关于两家医院的数据。两者都测量了相同的分类变量 n 次。在这种情况下,它是在特定观察期内发现与医疗保健相关的细菌的频率。
在表格中,汇总数据如下所示,其中 % 表示在该时间段内进行的所有测量的百分比。
n Hospital 1 (%) n Hospital 2 (%)
Healthcare associated bacteria 829 (59.4) 578 (57.6)
Community associated bacteria 473 (33.9) 372 (37.1)
Contaminants 94 (6.7) 53 (5.3)
Total 1396 (100.0) 1003 (100.0)
现在看看百分比,很明显这些比例非常相似,你可能想知道我到底为什么要对两家医院进行统计比较。但我有其他数据,其中的比例不同,所以这个问题的目的是:
如何在测量的类别方面比较医院 1 和医院 2。
由于数据以汇总方式和数组格式提供,我决定制作一个
data.frame
对于每个单个变量/类别。hosp1 <- rep(c("Yes", "No"), times=c(829,567))
hosp2 <- rep(c("Yes", "No"), times=c(578,425))
all <- cbind(hosp1, c(hosp2,rep(NA, length(hosp1)-length(hosp2))))
all <- data.frame(all)
names(all)[2]<-"hosp2"
summary(all)
到目前为止一切顺利,因为摘要似乎现在可以运行
chisq.test()
.但是现在,事情变得奇怪了。with(all, chisq.test(hosp1, hosp2, correct=F))
Pearson's Chi-squared test
data: hosp1 and hosp2
X-squared = 286.3087, df = 1, p-value < 2.2e-16
结果似乎表明存在显着差异。如果您对数据进行交叉制表,您会发现 R 正在以一种非常奇怪的方式对其进行汇总:
with(all, table(hosp1, hosp2))
No Yes
No 174 0
Yes 251 578
因此,当然,如果以这种方式总结数据,就会有一个具有统计意义的发现——因为一个类别被总结为根本没有测量。为什么会发生这种情况,我可以做些什么来纠正它?最后,不必单独制作
data.frame
对于每个类别,是否有明显的循环功能?我想不出一个。谢谢你的帮助!
根据 THELATEMAIL 对原始数据的请求进行更新。FRAME
dput(SO_Example_v1)
structure(list(Type = structure(c(3L, 1L, 2L), .Label = c("Community",
"Contaminant", "Healthcare"), class = "factor"), hosp1_WoundAssocType = c(464L,
285L, 24L), hosp1_BloodAssocType = c(73L, 40L, 26L), hosp1_UrineAssocType = c(75L,
37L, 18L), hosp1_RespAssocType = c(137L, 77L, 2L), hosp1_CathAssocType = c(80L,
34L, 24L), hosp2_WoundAssocType = c(171L, 115L, 17L), hosp2_BloodAssocType = c(127L,
62L, 12L), hosp2_UrineAssocType = c(50L, 29L, 6L), hosp2_RespAssocType = c(135L,
142L, 6L), hosp2_CathAssocType = c(95L, 24L, 12L)), .Names = c("Type",
"hosp1_WoundAssocType", "hosp1_BloodAssocType", "hosp1_UrineAssocType",
"hosp1_RespAssocType", "hosp1_CathAssocType", "hosp2_WoundAssocType",
"hosp2_BloodAssocType", "hosp2_UrineAssocType", "hosp2_RespAssocType",
"hosp2_CathAssocType"), class = "data.frame", row.names = c(NA,
-3L))
解释:这个
data.frame
实际上比上表中总结的更复杂,因为它还包含培养特定类型细菌的位置(即在伤口、血培养、导管等中)。所以我制作的表格实际上如下所示: All locations
n Hospital 1 (%) n Hospital 2 (%) p-val
Healthcare associated bacteria 829 (59.4) 578 (57.6) 0.39
Community associated bacteria 473 (33.9) 372 (37.1) ...
Contaminants 94 (6.7) 53 (5.3) ...
Total 1396 (100.0) 1003 (100.0) -
标题“所有位置”随后将被伤口、血液、尿液、导管等取代。
最佳答案
关于如何使 p 值起作用的问题的答案有些简单。您可以使用与 @thelatemail 相同的语法获取您正在寻找的其他两个 p 值,如下所示:
#community (p = 0.1049)
chisq.test(cbind(c(473,923),c(372,631)),correct=FALSE)
#contaminants (p = 0.1443)
chisq.test(cbind(c(94,1302),c(53,950)),correct=FALSE)
您可以通过以下方式以更多方式获得这些答案:
out <- cbind(rowSums(SO_Example_v1[,2:6]),rowSums(SO_Example_v1[,7:11]))
chisq.test(rbind(out[1,],colSums(out[2:3,])),correct=FALSE)
chisq.test(rbind(out[2,],colSums(out[c(1,3),])),correct=FALSE)
chisq.test(rbind(out[3,],colSums(out[1:2,])),correct=FALSE)
当然,此时我们已经超出了 SO 的范围,但鉴于数据的性质,也许一个更相关的问题是医院之间是否存在整体差异,您可以回答(从频率论者的角度)使用基于所有三种类型的卡方检验:
chisq.test(out,correct=FALSE)
关于r - 有人能告诉我为什么 R 没有将整个 data.frame 用于这个 chisq.test 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28979696/