在 R 中期望 2 x 2 的比值比
oddsratio expecting 2 x 2 in R
优势比在 R 中预期为 2 x 2。看来我创建了一个 2 x 2 table...嗯,甚至 prop.table() 显示数据为 2 x 2 table,那么什么优势比函数适用于我的 table?
数据:
nausea_yes <- c(98, 161, (98-161))
nausea_no <- c(264, 280, (264-280))
Table形成OK:
tbl <- table(nausea_yes, nausea_no)
tbl.prop <- prop.table(tbl, margin=1)
nausea_no
nausea_yes -16 264 280
-63 1 0 0
98 0 1 0
161 0 0 1
问题代码:
library(mosaicCore)
nausea_yes <- c(98, 161, (98-161))
nausea_no <- c(264, 280, (264-280))
tbl <- table(nausea_yes, nausea_no)
tbl.prop <- prop.table(tbl, margin=1)
odds_ratio <- mosaic::oddsRatio(tbl.prop, verbose = TRUE)
错误:
Error in orrr(x, conf.level = conf.level, verbose = verbose, digits = digits, : expecting something 2 x 2
根据?oddsRatio
This function calculates the odds ratio and relative risk for a 2 x 2 contingency table and a confidence interval (default conf.level is 95 percent) for the each estimate. x should be a matrix, data frame or table.
The odds ratio is calculated as (Odds row 2) / (Odds row 1)
在 OP 的示例中,它是 3 x 3 table/matrix 而不是 2 x 2
如果是 2 x 2 矩阵,应该可以
nausea_yes <- c(98, 161)
nausea_no <- c(264, 280)
mosaic::oddsRatio(cbind(nausea_yes, nausea_no))
#[1] 1.54898
您应该使用 cbind
创建 2x2 table,而不是 table
。然后你可以使用 fisher.test
来获得优势比,空值等于 1(没有区别)。
nausea_yes <- c(98, 161)
nausea_no <- c(264, 280)
tbl <- cbind(nausea_yes, nausea_no); tbl
# nausea_yes nausea_no
#[1,] 98 264
#[2,] 161 280
fisher.test(tbl)
#Fisher's Exact Test for Count Data
#data: tbl
#p-value = 0.004974
#alternative hypothesis: true odds ratio is not equal to 1
#95 percent confidence interval:
# 0.4712971 0.8827141
#sample estimates:
#odds ratio
# 0.645947
解释很复杂,因为我们不知道行名称,但我相信您知道。我们只能说第一行的赔率 "nausea_yes" 低于第二行。如果第一行代表一个治疗组,那么我们可以说治疗组中恶心的几率明显较低。
请注意,table
函数用于对原始数据进行制表以获得频率计数。但由于您已经获得了频率计数,因此无需在此处使用 table
。只需将计数与 cbind
(或 rbind
)相结合即可。
优势比在 R 中预期为 2 x 2。看来我创建了一个 2 x 2 table...嗯,甚至 prop.table() 显示数据为 2 x 2 table,那么什么优势比函数适用于我的 table?
数据:
nausea_yes <- c(98, 161, (98-161))
nausea_no <- c(264, 280, (264-280))
Table形成OK:
tbl <- table(nausea_yes, nausea_no)
tbl.prop <- prop.table(tbl, margin=1)
nausea_no
nausea_yes -16 264 280
-63 1 0 0
98 0 1 0
161 0 0 1
问题代码:
library(mosaicCore)
nausea_yes <- c(98, 161, (98-161))
nausea_no <- c(264, 280, (264-280))
tbl <- table(nausea_yes, nausea_no)
tbl.prop <- prop.table(tbl, margin=1)
odds_ratio <- mosaic::oddsRatio(tbl.prop, verbose = TRUE)
错误:
Error in orrr(x, conf.level = conf.level, verbose = verbose, digits = digits, : expecting something 2 x 2
根据?oddsRatio
This function calculates the odds ratio and relative risk for a 2 x 2 contingency table and a confidence interval (default conf.level is 95 percent) for the each estimate. x should be a matrix, data frame or table.
The odds ratio is calculated as (Odds row 2) / (Odds row 1)
在 OP 的示例中,它是 3 x 3 table/matrix 而不是 2 x 2
如果是 2 x 2 矩阵,应该可以
nausea_yes <- c(98, 161)
nausea_no <- c(264, 280)
mosaic::oddsRatio(cbind(nausea_yes, nausea_no))
#[1] 1.54898
您应该使用 cbind
创建 2x2 table,而不是 table
。然后你可以使用 fisher.test
来获得优势比,空值等于 1(没有区别)。
nausea_yes <- c(98, 161)
nausea_no <- c(264, 280)
tbl <- cbind(nausea_yes, nausea_no); tbl
# nausea_yes nausea_no
#[1,] 98 264
#[2,] 161 280
fisher.test(tbl)
#Fisher's Exact Test for Count Data
#data: tbl
#p-value = 0.004974
#alternative hypothesis: true odds ratio is not equal to 1
#95 percent confidence interval:
# 0.4712971 0.8827141
#sample estimates:
#odds ratio
# 0.645947
解释很复杂,因为我们不知道行名称,但我相信您知道。我们只能说第一行的赔率 "nausea_yes" 低于第二行。如果第一行代表一个治疗组,那么我们可以说治疗组中恶心的几率明显较低。
请注意,table
函数用于对原始数据进行制表以获得频率计数。但由于您已经获得了频率计数,因此无需在此处使用 table
。只需将计数与 cbind
(或 rbind
)相结合即可。