/
VideoVignette.Rmd
222 lines (175 loc) · 4.58 KB
/
VideoVignette.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
---
title: "Code used in the video vignette"
author: "Martijn Schuemie"
date: "`r Sys.Date()`"
output:
pdf_document: default
html_document: default
subtitle: A short demonstration of the EvidenceSynthesis package
vignette: >
%\VignetteIndexEntry{Code used in the video vignette}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE}
library(EvidenceSynthesis)
```
This vignette contains the code used in a short video on the EvidenceSynthesis package: [https://youtu.be/dho7E97vpgQ](https://youtu.be/dho7E97vpgQ).
# Simulate data
Simulate 10 sites:
```{r}
simulationSettings <- createSimulationSettings(
nSites = 10,
n = 10000,
treatedFraction = 0.8,
nStrata = 5,
hazardRatio = 2,
randomEffectSd = 0.5
)
set.seed(1)
populations <- simulatePopulations(simulationSettings)
head(populations[[1]])
table(populations[[1]][, c("x", "y")])
```
# Fit a model locally
Assume we are at site 1:
```{r message = FALSE}
library(Cyclops)
population <- populations[[1]]
cyclopsData <- createCyclopsData(Surv(time, y) ~ x + strata(stratumId),
data = population,
modelType = "cox"
)
cyclopsFit <- fitCyclopsModel(cyclopsData)
# Hazard ratio:
exp(coef(cyclopsFit))
# 95% confidence interval:
exp(confint(cyclopsFit, parm = "x")[2:3])
```
# Approximate the likelihood function at one site
## Normal approximation
```{r}
normalApproximation <- approximateLikelihood(
cyclopsFit = cyclopsFit,
parameter = "x",
approximation = "normal"
)
normalApproximation
plotLikelihoodFit(
approximation = normalApproximation,
cyclopsFit = cyclopsFit,
parameter = "x"
)
```
## Adaptive approximation
```{r}
approximation <- approximateLikelihood(
cyclopsFit = cyclopsFit,
parameter = "x",
approximation = "adaptive grid",
bounds = c(log(0.1), log(10))
)
head(approximation)
plotLikelihoodFit(
approximation = approximation,
cyclopsFit = cyclopsFit,
parameter = "x"
)
```
# Approximate at all sites
```{r}
fitModelInDatabase <- function(population, approximation) {
cyclopsData <- createCyclopsData(Surv(time, y) ~ x + strata(stratumId),
data = population,
modelType = "cox"
)
cyclopsFit <- fitCyclopsModel(cyclopsData)
approximation <- approximateLikelihood(cyclopsFit,
parameter = "x",
approximation = approximation
)
return(approximation)
}
adaptiveGridApproximations <- lapply(
X = populations,
FUN = fitModelInDatabase,
approximation = "adaptive grid"
)
normalApproximations <- lapply(
X = populations,
FUN = fitModelInDatabase,
approximation = "normal"
)
normalApproximations <- do.call(rbind, (normalApproximations))
```
# Synthesize evidence
## Fixed-effects
Gold standard (pooling data):
```{r message = FALSE,cache = TRUE}
fixedFxPooled <- computeFixedEffectMetaAnalysis(populations)
fixedFxPooled
```
Normal approximation:
```{r message = FALSE}
fixedFxNormal <- computeFixedEffectMetaAnalysis(normalApproximations)
fixedFxNormal
```
Adaptive grid approximation:
```{r message = FALSE}
fixedFxAdaptiveGrid <- computeFixedEffectMetaAnalysis(adaptiveGridApproximations)
fixedFxAdaptiveGrid
```
### Visualization
Normal approximation:
```{r message = FALSE, warning = FALSE, fig.width = 9, fig.height = 5}
plotMetaAnalysisForest(
data = normalApproximations,
labels = paste("Site", 1:10),
estimate = fixedFxNormal,
xLabel = "Hazard Ratio"
)
```
Adaptive grid approximation:
```{r message = FALSE, warning = FALSE, fig.width = 9, fig.height = 5}
plotMetaAnalysisForest(
data = adaptiveGridApproximations,
labels = paste("Site", 1:10),
estimate = fixedFxAdaptiveGrid,
xLabel = "Hazard Ratio"
)
```
## Random-effects
Gold standard (pooling data):
```{r cache = TRUE, message = FALSE,cache=TRUE}
randomFxPooled <- computeBayesianMetaAnalysis(populations)
exp(randomFxPooled[, 1:3])
```
Normal approximation:
```{r message = FALSE}
randomFxNormal <- computeBayesianMetaAnalysis(normalApproximations)
exp(randomFxNormal[, 1:3])
```
Adaptive grid approximation:
```{r message = FALSE}
randomFxAdaptiveGrid <- computeBayesianMetaAnalysis(adaptiveGridApproximations)
exp(randomFxAdaptiveGrid[, 1:3])
```
### Visualization
Normal approximation:
```{r message = FALSE, warning = FALSE, fig.width = 8, fig.height = 5}
plotMetaAnalysisForest(
data = normalApproximations,
labels = paste("Site", 1:10),
estimate = randomFxNormal,
xLabel = "Hazard Ratio"
)
```
Adaptive grid approximation:
```{r message = FALSE, warning = FALSE, fig.width = 8, fig.height = 5}
plotMetaAnalysisForest(
data = adaptiveGridApproximations,
labels = paste("Site", 1:10),
estimate = randomFxAdaptiveGrid,
xLabel = "Hazard Ratio"
)
```