data.frame
在尝试创建自己的问题并对其进行定量分析(例如 a )时,我无法想出解决方案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 对 RAW DATA.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) -
如果标题为“所有位置”,随后将被伤口、血液、尿液、导管等取代。