我有以下两种不同药物的胃灼热症状和反应表:
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要求不多,对吧?
发布于 2015-12-11 01:44:33
来自?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
发布于 2015-12-11 01:58:06
以下是一些替代方案:
1)外部使用outer生成期望值,然后将其与原始表和使用sprintf的父表放在一起。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 3682) gmodel::交叉表这个函数专门用于生成具有预期计数和其他统计数据的交叉稳定表:
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)下降::交叉表--这类似于gmodel::,但是输出略有不同。
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")))https://stackoverflow.com/questions/34214787
复制相似问题