grp <- rep(c("A", "B"), each = 50)
set.seed(1)
hematoma <- rbinom(n = 100, size = 1,
prob = rep(c(0.3, 0.5), each = 50))
Create a cross tabulation. Hematoma = 1 means the subject experienced hematoma.
table(grp, hematoma)
## hematoma
## grp 0 1
## A 34 16
## B 29 21
Get proportions. It appears group B has a higher chance of hematoma (0.42 vs 0.32).
prop.table(table(grp, hematoma), margin = 1)
## hematoma
## grp 0 1
## A 0.68 0.32
## B 0.58 0.42
Calculating odds ratio can be done “by hand”
# save table of proportions
tab <- prop.table(table(grp, hematoma), margin = 1)
grpA_odds <- tab[1,2]/(1 - tab[1,2])
grpB_odds <- tab[2,2]/(1 - tab[2,2])
# odds ratio
grpB_odds/grpA_odds
## [1] 1.538793
Appears group B odds for hematoma are about 54% higher than the odds of hematoma for group A.
We can use logistic regression to get odds ratio, confidence interval, and the p-value testing null if odds ratio is 1. To do this we use the glm
function with family = binomial
.
m <- glm(hematoma ~ grp, family = binomial)
Odds ratio:
exp(coef(m)["grpB"])
## grpB
## 1.538793
Confidence interval on odds ratio:
exp(confint(m))["grpB",]
## Waiting for profiling to be done...
## 2.5 % 97.5 %
## 0.6819391 3.5234097
The p-value:
coef(summary(m))["grpB","Pr(>|z|)"]
## [1] 0.3015106
It appears the supposition of no difference in the odds cannot be rejected.