-
Notifications
You must be signed in to change notification settings - Fork 1
/
1a) CE plane.R
333 lines (125 loc) · 5.04 KB
/
1a) CE plane.R
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# Simulate data and perform CEA
# clear workspace
rm(list = ls())
### Import packages --------
library(data.table)
library(dplyr)
library(readr)
library(tidyverse)
library(lubridate)
library(stringr)
library (lme4)
library (boot)
library(ggplot2)
library(mice)
### Create data ----------
# patient ID
id<-as.data.frame(1:100)
# create the cost variable
cost<- as.data.frame(rnorm(100, 1500, 500))
# create the cost variable
QALYs<- as.data.frame(rnorm(100, 0.5, 0.3))
# treatment code
trtcode1 <- matrix(1, nrow=50, ncol =1)
trtcode0 <- matrix(0, nrow=50, ncol =1)
trtcode <- rbind(trtcode0, trtcode1)
# merge all data sets
data <- as.data.frame(cbind(id, cost, QALYs, trtcode))
colnames(data)<-c("id", "cost", "QALYs", "trtcode")
rm(trtcode0, trtcode1, cost, QALYs, trtcode, id)
### Regression analysis ---------
# effects
model_effect <- lm (QALYs ~ trtcode, data = data)
summary(model_effect)
confint(model_effect) # get 95% CI
# costs
model_costs <- lm (cost ~ trtcode , data = data)
summary(model_costs)
confint(model_costs) # get 95% CI
# store results from analysis
# to calculate ICER
coef_effect <- as.data.frame(summary(model_effect )$coefficients[2,1])
coef_cost <- as.data.frame(summary(model_costs)$coefficients[2,1])
# merge
coef_icer <- cbind(coef_effect, coef_cost)
# add column names
colnames(coef_icer)<-c("coef_effect", "coef_cost")
# remove data sets not needed
rm(coef_cost, coef_effect)
### Bootstrapping ---------
# non paramteric bootstrap
B<-1000 # Number of resampling iterations
results<-as.data.frame(matrix(NA, nrow=B, ncol = 3)) # matrix with 2 columns and 'B' rows
colnames(results)<-c("cost_diff", "effect_dif", "boot_id")
# Bootstrapping
for(i in 1:B){
df_boot <- data[data$id %in% sample(data$id, replace = TRUE),]
model_costs <- lm (cost ~ trtcode, data = df_boot)
model_effect <- lm (QALYs ~ trtcode, data = df_boot)
results[i,1]<-model_costs$coefficients[2]
results[i,2]<-model_effect$coefficients[2]
results[i,3] <- i
}
### cost-effectiveness plane ----------------
ggplot () +
geom_point(data=results, aes(x=results$effect_dif, y=results$cost_dif, color = I("blue3")))+
geom_point(data=coef_icer, aes(x=coef_effect, y=coef_cost, color = I("red3"))) +
labs (x= "Difference in Effects", y= "Difference in Costs") +
geom_hline(yintercept=0, linetype = "dashed") +
geom_vline(xintercept=0, linetype = "dashed") +
ylim(min(results$cost_dif * 1.2),
max(results$cost_dif * 1.2)) + # scale range is proportional to range of values
xlim(min(results$effect_dif * 1.2),
max(results$effect_dif * 1.2)) + # scale range is proportional to range of values
theme_minimal()
# Get proportion in each of the
# four quadrants of teh CE plane
results <- results %>%
mutate(quadr= ifelse(results$cost_dif > 0 & results$effect_dif > 0, "NE_quad",
ifelse(results$cost_dif < 0 & results$effect_dif > 0, "SE_quad",
ifelse(results$cost_dif < 0 & results$effect_dif <0, "SW_quad",
"NW_quad"))))
(table(results$quadr)/(B)*100) # B number of bootstrapped data sets
# add assertion to
# ensure no mistakes in code
z <- sum((table(results$quadr)/(B)*100) )
if (z!=100) # unique patid's must be the same as initially imported dataset
stop("You made an error in your code. Cost and effect pairs are wrong")
rm(z)
### Cost-effectiveness acceptability curves --------------
wtp_seq <- seq(0, 50000, by= 5000) # define wtp range
lw=length(wtp_seq) # get length of wtp values
# create a wtp vector
# for different wtp thresholds
for (i in wtp_seq){
results[[paste('wtp_',i ,sep="")]] <- ifelse((i*results$effect_dif)-
results$cost_dif >0,1,0) # NMB:yes/no
}
# View(results)
# uncomment to see results
# create a data frame to store
# results in order to make ceac
ceac_res<-as.data.frame(matrix(NA, nrow=lw, ncol = 2)) # 2 columns: wtp length and p.ce
colnames(ceac_res)<-c("wtp_thr", "p.ce")
ceac_res$wtp_thr <- wtp_seq
for (i in 5:ncol(results)){ # from 5th column till end; amend as needed
a <- table(results[,i]) # extract n of NMB
a <- cbind(a,prop.table(a)) # calculate proportion of NMB:yes
a <- a[nrow(a),ncol(a)] # store proportion for each wtp, last row & column
ceac_res$p.ce[i-4]<-a # bind with ceac results
}
# if the probability of ce is 0, the results may be wrong!
# then force p.ce <- 0 !!!
# uncomemnt to test
# b <- table(results$wtp_45000)
# b <- cbind(b,prop.table(b))
# b <- b[2,2]
# plot ceac
ggplot () +
geom_line(data=ceac_res, aes(x=ceac_res$wtp_thr, y=ceac_res$p.ce, color = I("blue3")))+
labs (x= "WTP threshold", y= "Probability of cost-effectiveness") +
ylim(c(0, 1)) +
xlim(min(ceac_res$wtp_thr), max(ceac_res$wtp_thr)) + # uses range of wtp
theme_minimal()
# remove data sets we do not need
rm(a, i)