我有下表的胃灼热症状和对两种不同药物的反应:
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
问题不是太多,不是吗?
答案 0 :(得分:6)
来自?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
答案 1 :(得分:3)
以下是一些替代方案:
1)外部使用outer
生成预期值,然后使用sprintf
将其与原始表和parens放在一起。 noquote
可用于显示它而不带引号:
mm <- m
mm[-3,-3] <- matrix(sprintf("%3.0f(%.0f)", m, outer(m[, 3], m[3, ]) / m[3,3]), 3)[-3,-3]
,并提供:
> noquote(mm)
Medication
Symptoms Drug A Drug B Total
Heartburn 64(75) 92(81) 156
Normal 114(103) 98(109) 212
Totals 178 190 368
2)gmodels :: CrossTable 此函数专门用于生成包含预期计数和其他统计信息的交叉表:
library(gmodels)
CrossTable(m[-3, -3], expected = TRUE, prop.r = FALSE, prop.c = FALSE,
prop.t = FALSE, prop.chisq = FALSE)
,并提供:
Cell Contents
|-------------------------|
| N |
| Expected N |
|-------------------------|
Total Observations in Table: 368
| Medication
Symptoms | Drug A | Drug B | Row Total |
-------------|-----------|-----------|-----------|
Heartburn | 64 | 92 | 156 |
| 75.457 | 80.543 | |
-------------|-----------|-----------|-----------|
Normal | 114 | 98 | 212 |
| 102.543 | 109.457 | |
-------------|-----------|-----------|-----------|
Column Total | 178 | 190 | 368 |
-------------|-----------|-----------|-----------|
Statistics for All Table Factors
Pearson's Chi-squared test
------------------------------------------------------------
Chi^2 = 5.85 d.f. = 1 p = 0.0156
Pearson's Chi-squared test with Yates' continuity correction
------------------------------------------------------------
Chi^2 = 5.35 d.f. = 1 p = 0.0207
3)descr :: CrossTable 这与gmodels :: CrossTable类似,但输出略有不同。
library(descr)
CrossTable(m[-3,-3], prop.r = FALSE, prop.c = FALSE, prop.t = FALSE,
prop.chisq = FALSE, expected = TRUE)
,并提供:
Cell Contents
|-------------------------|
| N |
| Expected N |
|-------------------------|
====================================
Medication
Symptoms Drug A Drug B Total
------------------------------------
Heartburn 64 92 156
75.5 80.5
------------------------------------
Normal 114 98 212
102.5 109.5
------------------------------------
Total 178 190 368
====================================
注意:我们将此作为输入m
:
m <- matrix(c(64, 114, 178, 92, 98, 190, 156, 212, 368), 3,
dimnames = list(Symptoms = c("Heartburn", "Normal", "Totals"),
Medication = c("Drug A", "Drug B", "Total")))