odds ratio expecting 2 x 2 in R. It appears that I have created a 2 x 2 table...hmm, even prop.table() display data as 2 x 2 table, so what odds ratio function will work with my table?
Data:
nausea_yes <- c(98, 161, (98-161))
nausea_no <- c(264, 280, (264-280))
Table formed 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
Problem Code:
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:
Error in orrr(x, conf.level = conf.level, verbose = verbose, digits = digits, : expecting something 2 x 2
You should be using cbind
to create the 2x2 table, not table
. Then you can use fisher.test
to get the odds ratio, with the null that it is equal to 1 (no difference).
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
Interpretation is complicated since we don't know the row names, although I'm sure you know. All we can say is that the first row has a lower odds of "nausea_yes" than the second row. If the first row represented a treatment group, then we could say that there was a significantly lower odds of nausea among the treatment group.
Note that the table
function is used to tabulate raw data in order to obtain frequency counts. But since you've already got the frequency counts, there's no need to use table
here. Just combine the counts with cbind
(or rbind
).