-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.Rmd
237 lines (195 loc) · 5.12 KB
/
convert.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
---
title: "Conversion from OR to RR or RD"
author: "Ehsan Karim: ehsank.com"
date: "`r format(Sys.time(), '%d %B %Y')`"
always_allow_html: yes
output:
html_document:
fig_caption: yes
keep_md: yes
toc: yes
number_sections: true
toc_depth: 3
toc_float:
collapsed: true
smooth_scroll: true
theme: lumen
highlight: textmate
pdf_document:
fig_caption: yes
latex_engine: xelatex
number_sections: yes
toc: yes
toc_depth: 3
word_document:
toc: yes
slidy_presentation:
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(epiDisplay)
require(sjstats)
```
# Notations
- OR = Odds ratio
- RR = Risk ratio
- RD = Risk difference
- p0 = Background event rate (e.g., for unexposed group)
- p1 = Risk for experiencing event for exposed group
- O0 = Background odds of experiencing events
- O1 = Odds for experiencing event for exposed group
# Functions
## OR to RD
Following ref 1
```{r}
OR2RD <- function(OR,p0){
O0 = p0/(1-p0)
RD = O0*(OR-1)/( (1+OR*O0)*(1+O0) )
return(RD)
}
```
## OR to RR
Following ref 1
```{r}
OR2RR <- function(OR,p0){
O0 = p0/(1-p0)
RD = O0*(OR-1)/( (1+OR*O0)*(1+O0) )
RR = (RD+p0)/p0
return(RR)
}
```
Following ref 2
```{r}
OR2RRx <- function(OR,p0){
O0 = p0/(1-p0)
O1 = OR*O0
p1 = O1/(1+O1)
RR = p1/p0
return(RR)
}
```
Following ref 3
```{r}
OR2RRy <- function(OR,p0){
RR = OR/(1 - p0 + (OR * p0))
return(RR)
}
```
# Testing the Function
## OR to RD
### Option 1
Using derivation by a student: Liang Xu.
```{r}
OR = seq(0.1,2,0.1)
p0.list = seq(0.1,.9,0.1)
plot(OR,seq(-0.55,0.15,length=20), type = "n", xlab = "OR", ylab = "RD")
for (i in p0.list){
p0 = i
p1 = OR * (p0/(1 - p0))/(1 + OR * (p0/(1 - p0)))
RD = p1-p0
lines(OR,RD, col = i*10, lty = i*10)
}
legend("bottomright", legend=p0.list, title="p0",
col=p0.list*10, lty=p0.list*10, cex=0.8)
```
### Option 2
```{r}
OR = seq(0.1,2,0.1)
p0.list = seq(0.1,.9,0.1)
plot(OR,seq(-0.55,0.15,length=20), type = "n", xlab = "OR", ylab = "RD")
for (i in p0.list){
RD = OR2RD(OR = OR, p0 = i)
lines(OR,RD, col = i*10, lty = i*10)
}
legend("bottomright", legend=p0.list, title="p0",
col=p0.list*10, lty=p0.list*10, cex=0.8)
```
## OR to RR
### Option 1
```{r}
OR = seq(0.1,2,0.1)
p0.list = seq(0.1,.9,0.1)
plot(OR,seq(0,2,length=20), type = "n", xlab = "OR", ylab = "RR")
for (i in p0.list){
p0 = i
p1 = OR * (p0/(1 - p0))/(1 + OR * (p0/(1 - p0)))
RR = p1/p0
lines(OR,RR, col = i*10, lty = i*10)
}
legend("bottomright", legend=p0.list, title="p0",
col=p0.list*10, lty=p0.list*10, cex=0.8)
```
### Option 2
```{r}
OR = seq(0.1,2,0.1)
p0.list = seq(0.1,.9,0.1)
plot(OR,seq(0,2,length=20), type = "n", xlab = "OR", ylab = "RR")
for (i in p0.list){
RR = OR2RR(OR = OR, p0 = i)
lines(OR,RR, col = i*10, lty = i*10)
}
legend("bottomright", legend=p0.list, title="p0",
col=p0.list*10, lty=p0.list*10, cex=0.8)
```
### Option 3
```{r}
OR = seq(0.1,2,0.1)
p0.list = seq(0.1,.9,0.1)
plot(OR,seq(0,2,length=20), type = "n", xlab = "OR", ylab = "RR")
for (i in p0.list){
RR = OR2RRx(OR = OR, p0 = i)
lines(OR,RR, col = i*10, lty = i*10)
}
legend("bottomright", legend=p0.list, title="p0",
col=p0.list*10, lty=p0.list*10, cex=0.8)
```
# Working with a data
## Titanic data
### OR
```{r}
url <- "http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt"
titanic <- read.csv(file = url, stringsAsFactors = FALSE)
titanic$age[is.na(titanic$age)] <- median(titanic$age, na.rm = TRUE)
fit.OR <- glm(survived ~ sex,
family = binomial("logit"), data = titanic)
est.OR = exp(coef(fit.OR))
est.OR
require(epiDisplay)
logistic.display(fit.OR)
```
### RD
- The outcome variable is `survived`
- `sex` variable is considered as the exposure variable: `Female` categoty is the reference category, and hence considered here as the background population to calculate $p0$.
- Stating values were identified by grid search (e.g., using `for` loop within plausible values)
```{r}
tablex <- table(titanic$sex,titanic$survived)
tablex
survival.rate = tablex[,"1"]/sum(tablex[,"1"])
survival.rate
p0 <- survival.rate[1]
p0
OR2RD(OR=est.OR[2],p0=p0)
fit.RD <- glm(survived ~ sex, start = c(.1,-.01),
family = binomial("identity"), data = titanic)
fit.RD
```
### RR
```{r}
OR2RR(OR=est.OR[2],p0=p0)
OR2RRx(OR=est.OR[2],p0=p0)
OR2RRy(OR=est.OR[2],p0=p0)
require(sjstats)
or_to_rr(or=est.OR, p0=p0)
```
- Stating values were identified by grid search (e.g., using `for` loop within plausible values)
```{r}
fit.RR <- glm(survived ~ sex, start=c(-1,.5),
family = binomial("log"), data = titanic)
fit.RR
exp(coef(fit.RR))
```
# References
1. [Popham, F. (2016). Converting between marginal effect measures from binomial models. International journal of epidemiology, 45(2), 590-591.](https://academic.oup.com/ije/article/45/2/590/2572549)
2. [StatsToDo : Odd and Risk Interconversion Explained](https://www.statstodo.com/OddRiskConversion_Exp.php)
3. [Get relative risks estimates from logistic regressions or odds ratio values](https://strengejacke.github.io/sjstats/reference/odds_to_rr.html)