我有下表的胃灼热症状和对两种不同药物的反应:
Medication Symptoms Drug A Drug B Totals Heartburn 64 92 156 Normal 114 98 212 Totals 178 190 368
我正在寻找一个R函数,它通过以下操作来获得预期的计数:
Medication Symptoms Drug A Drug B Heartburn 156 * 178 / 368 = 75 156 * 190 / 368 = 81 Normal 212 * 178 / 368 = 103 212 * 190 / 368 = 109
理想情况下,显示器甚至可选择如下:
Medication Symptoms Drug A Drug B Totals Heartburn 64(75) 92(81) 156 Normal 114(103) 98(109) 212 Totals 178 190 368
这不是太多问,是吗?
示例来自?chisq.test
:
M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477))) dimnames(M) <- list(gender = c("F", "M"), party = c("Democrat","Independent", "Republican")) Xsq <- chisq.test(M) # Prints test summary
我认为$expected
结果的组成部分是你想要的:
Xsq$expected ## party ## gender Democrat Independent Republican ## F 703.6714 319.6453 533.6834 ## M 542.3286 246.3547 411.3166
您首选显示的开头:
M2 <- M; M2[] <- paste(M,paste0("(",round(Xsq$expected),")")) ## party ## gender Democrat Independent Republican ## F 762 (704) 327 (320) 468 (534) ## M 484 (542) 239 (246) 477 (411)
也可以看看 ?addmargins