-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
452 lines (356 loc) · 20.8 KB
/
server.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# last edited 07-02-2019 by Laurent Smeets
# last edited 28-09-2020 by Ihnwhi Heo
library("shinydashboard")
library("shiny")
library("gridExtra")
library("shinyBS")
library("shinyWidgets")
library("cowplot")
library("shinyjs")
library("grid")
library("reshape")
library("rmarkdown")
library("knitr")
library("tidyverse")
server <- function(input, output, session) {
uu_color <- " #ffcd00"
observeEvent(input$disclaimer, {
# Define Pop-Up with Terms & Conditions
showModal(modalDialog(
title = "Disclaimer",
"This project is funded by the Netherlands organization for scientific research (NWO);
grant number VIDI-452-14-006. Purpose of the service 'utrecht-university.shinyapps.io'
is to provide a digital place for trying out, evaluating and/or comparing methods developed
by researchers of Utrecht University for the scientific community worldwide. Filing the app
and its contents for citing and referencing purposes is not permitted. The web application is
provided 'as is' and 'as available' and is without any warranty. Your use of this web application
is solely at your own risk. By using this app you agree to be bound by the above terms."))
})
observeEvent(input$answer1, {
showModal(modalDialog(
title = "Answer",
withMathJax("$$Delay_i = \\beta_{intercept}\ + \\beta_{age} * Age_i + \\beta_{age^2} * Age^2_i + e_i$$ ")))
#withMathJax(HTML("Delay = \\(\\beta_{intercept}\\) + \\(\\beta_{age}\\)* Age_i + \\(\\beta_{age^2}\\)* \\Age^2_i\\ + e_i"))))
})
observeEvent(input$answer2, {
showModal(modalDialog(
title = "Answer",
p("This effect would look like a negative parabola (n-shaped)."),
tags$img(src="NewPlot.png", align = "left",width=350)))
})
observeEvent(input$answer3, {
showModal(modalDialog(
title = "Answer",
"You can see this in the plot below. Note that the color of the variance of
the linear effect is coral and that of the quadratic effect is orange.
Let's focus on the widening of the variance of the quadratic effect. When you slightly increase
the standard deviation of the quadratic effect (please note that the scales of variance
sliders are different), it leads to a large widening of the quadratic effect ribbon over time. Any difference from the mean of
the quadratic effect is multiplied by age squared, so for example, for a 50-year-old, it is multiplied by 2500."))
})
output$distPlot_1 <- renderPlot({
P1<-data.frame() %>%
ggplot() +
geom_point() +
xlim(0, 120) +
ylim(-250, 1000)+
labs(xlab = "Age (in years)",
ylab = "Delay (in months)",
title = "Parameter Space")+
annotate(geom = "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
theme_light()
P2<-data.frame() %>%
ggplot()+
geom_point()+xlim(0, 120)+ylim(-250, 1000)+
labs(xlab= "Age (in years)",
ylab="Delay (in months)",
title= "Plausible Parameter Space")+
annotate(geom= "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
coord_cartesian(xlim = c(input$range1[1], input$range1[2]),
ylim = c(input$range2[1], input$range2[2]))+
theme_light()
grid.arrange(P1, P2,
left ="Delay (in months)",
bottom = "Age (in years)")
})
output$distPlot_2 <- renderPlot({
# based on the the input of the user we calculate the regression coefficients
age <- 0:120
delay<-input$p1*age + input$p2*(age^2)+input$p3
delay_intercept <- input$p3
delay_linear <- input$p1*age + input$p3
delay_quadratic <- input$p1*age + input$p2*(age^2) + input$p3
data=data.frame(age, delay, delay_intercept, delay_linear, delay_quadratic)
# this is the code to make the box
x1<-input$range1[1]
x2<-input$range1[2]
y1<-input$range2[1]
y2<-input$range2[2]
colourintercept <- "blue"
colourlinear <- "coral2"
colourquadratic <- "orange"
if(input$check_box_1==TRUE){
P<-ggplot(data = data, aes(x = age, y = delay))+
geom_line(data = filter(data, age>x1 & age<x2 & delay_intercept>y1 & delay_intercept<y2), aes(x = age, y = delay_intercept),colour = colourintercept, size = 1.3)+
geom_line(data = filter(data, age>x1 & age<x2 & delay_linear>y1 & delay_linear<y2), aes(x = age, y = delay_linear), colour = colourlinear, size = 1.3)+
geom_line(data = filter(data, age>x1 & age<x2 & delay_quadratic>y1 & delay_quadratic<y2), aes(x = age, y = delay_quadratic),colour = colourquadratic, size = 1.3)+
geom_line(aes(x = age, y = delay_intercept), colour = colourintercept, size = 1.3, linetype = "dotted")+
geom_line(aes(x = age, y = delay_linear), colour = colourlinear, size = 1.3, linetype = "dotted")+
geom_line(aes(x = age, y = delay_quadratic), colour = colourquadratic, size = 1.3, linetype = "dotted")+
coord_cartesian(xlim = c(0,120), ylim = c(-200, 1000))+
labs(title = "Parameter Space",
y = "Delay (in months)",
x = "Age (in years)")+
annotate(geom = "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
theme_light()
P_legend <- ggplot(data = filter(gather(data, key = "Parameter", value = "value", - age), Parameter %in% c("delay_intercept","delay_linear", "delay_quadratic" )))+
geom_line(aes(x = age, y = value, col = Parameter))+
scale_colour_manual(values = c(colourintercept, colourlinear, colourquadratic),
labels = c("intercept", "intercept + linear effect", "intercept + linear effect + quadratic effect "))+
theme(legend.key.width = unit(3,"cm"))
legend <- cowplot::get_legend(P_legend)
lay <- rbind(c(1,1,1,1),
c(1,1,1,1),
c(1,1,1,1),
c(2,2,2,2))
grid.arrange(P, ggdraw(legend), layout_matrix = lay)
}
else{
P <- ggplot(data = data,aes(x = age, y = delay))+
geom_line(aes(x = age, y = delay_intercept), colour = colourintercept, size = 1.3)+
geom_line(aes(x = age, y = delay_linear), colour = colourlinear, size = 1.3)+
geom_line(aes(x = age, y = delay_quadratic), colour = colourquadratic, size = 1.3)+
scale_x_continuous(name = "Age (in years)")+
coord_cartesian(xlim = c(input$range1[1], input$range1[2]),
ylim = c(input$range2[1], input$range2[2]))+
labs(title = "Plausible Values",
y = "Delay (in months)",
x = "Age (in years)")+
annotate(geom = "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
theme_light()
P_legend<-ggplot(data = filter(gather(data, key = "Parameter", value = "value", - age), Parameter %in% c("delay_intercept","delay_linear", "delay_quadratic" )))+
geom_line(aes(x = age, y = value, col = Parameter))+
scale_colour_manual(values = c(colourintercept, colourlinear, colourquadratic),
labels = c("intercept", "intercept + linear effect", "intercept + linear effect + quadratic effect "))+
theme(legend.key.width = unit(3,"cm"))
legend <- cowplot::get_legend(P_legend)
lay <- rbind(c(1,1,1,1),
c(1,1,1,1),
c(1,1,1,1),
c(2,2,2,2))
grid.arrange(P, ggdraw(legend), layout_matrix = lay)
}
})
output$distPlot_3 <- renderPlot({
# based on the the input of the user we calculate the regression coefficients
age <- 0:120
delay <- input$p1*age + input$p2*(age^2)+input$p3
delay_intercept <- input$p3
delay_linear <- input$p1*age + input$p3
delay_quadratic <- input$p1*age + input$p2*(age^2)+input$p3
if(input$p9=="66.7%"){
ribonrange <- .6827
}
if(input$p9=="95%"){
ribonrange <- .9545
}
if(input$p9== "99.7%"){
ribonrange <- .9973
}
variance.intercept<-input$p8
variance.age<-input$p6
variance.age2<-input$p7
#delay_low <- qnorm((1-ribonrange),input$p3, sd=sqrt(variance.intercept))+(qnorm(1-ribonrange,input$p1, sd=sqrt(variance.age))*age)+(qnorm(1-ribonrange,input$p2, sd=sqrt(variance.age2))*(age^2))
delay_low <- qnorm((1-ribonrange),input$p3, sd=variance.intercept)+(qnorm(1-ribonrange,input$p1, sd=variance.age)*age)+(qnorm(1-ribonrange,input$p2, sd=variance.age2)*(age^2))
#delay_high<- qnorm(ribonrange,input$p3, sd=sqrt(variance.intercept))+(qnorm(ribonrange,input$p1, sd=sqrt(variance.age))*age)+(qnorm(ribonrange,input$p2, sd=sqrt(variance.age2))*(age^2))
delay_high<- qnorm(ribonrange,input$p3, sd=variance.intercept)+(qnorm(ribonrange,input$p1, sd=variance.age)*age)+(qnorm(ribonrange,input$p2, sd=variance.age2)*(age^2))
delay_low_intercept <- qnorm((1-ribonrange),input$p3, sd=variance.intercept)
#delay_low_intercept <- qnorm((1-ribonrange),input$p3, sd=sqrt(variance.intercept))
delay_high_intercept <- qnorm(ribonrange,input$p3, sd=variance.intercept)
#delay_high_intercept <- qnorm(ribonrange,input$p3, sd=sqrt(variance.intercept))
delay_low_linear <- input$p3+(qnorm(1-ribonrange,input$p1, sd=variance.age)*age)
#delay_low_linear <- input$p3+(qnorm(1-ribonrange,input$p1, sd=sqrt(variance.age))*age)
#delay_high__linear <-input$p3+(qnorm(ribonrange,input$p1, sd=sqrt(variance.age))*age)
delay_high__linear <-input$p3+(qnorm(ribonrange,input$p1, sd=variance.age)*age)
delay_low_quadratic <- input$p3+input$p1*age+(qnorm(1-ribonrange,input$p2, sd=variance.age2)*(age^2))
#delay_low_quadratic <- input$p3+input$p1*age+(qnorm(1-ribonrange,input$p2, sd=sqrt(variance.age2))*(age^2))
delay_high_quadratic <- input$p3+input$p1*age+(qnorm(ribonrange,input$p2, sd=variance.age2)*(age^2))
#delay_high_quadratic <- input$p3+input$p1*age+(qnorm(ribonrange,input$p2, sd=sqrt(variance.age2))*(age^2))
data=data.frame(age, delay, delay_low, delay_high, delay_low_intercept, delay_high_intercept, delay_intercept, delay_linear, delay_low_linear, delay_high__linear, delay_quadratic, delay_low_quadratic, delay_high_quadratic)
# this is the code to make the box
x1 <- input$range1[1]
x2 <- input$range1[2]
y1 <- input$range2[1]
y2 <- input$range2[2]
colourintercept <- "blue"
colourlinear <- "coral2"
colourquadratic <- "orange"
if(input$check_box_2==FALSE){
if(input$check_box_3==TRUE){
P<-ggplot(data = data, aes(x = age, y = delay))+
geom_line(data = filter(data, age>x1 & age<x2 & delay_intercept>y1 &delay_intercept<y2), aes(x=age, y=delay_intercept),colour=colourintercept, size=1.3)+
geom_line(data = filter(data, age>x1 & age<x2 & delay_linear>y1 &delay_linear<y2), aes(x=age, y=delay_linear),colour=colourlinear, size=1.3)+
geom_line(data = filter(data, age>x1 & age<x2 & delay_quadratic>y1 &delay_quadratic<y2), aes(x=age, y=delay_quadratic),colour=colourquadratic, size=1.3)+
geom_line( aes(x = age, y = delay_intercept), colour = colourintercept, size=1.3, linetype= "dotted")+
geom_ribbon(aes(ymin = delay_low_intercept, ymax = delay_high_intercept), fill = colourintercept, alpha=0.3)+
geom_line( aes(x = age, y=delay_linear), colour = colourlinear, size = 1.3, linetype= "dotted")+
geom_ribbon(aes(ymin = delay_low_linear, ymax=delay_high__linear), fill = colourlinear, alpha=0.3)+
geom_line( aes(x = age, y=delay_quadratic),colour=colourquadratic, size = 1.3, linetype= "dotted")+
geom_ribbon(aes(ymin = delay_low_quadratic, ymax=delay_high_quadratic), fill = colourquadratic, alpha=0.3)+
coord_cartesian(xlim=c(0,120), ylim=c(-200, 1000))+
scale_x_continuous(name = "Age (in years)")+
scale_y_continuous(name = "Delay (in months)")+
labs(title= "Parameter Space")+
annotate(geom = "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
theme_light()
P_legend<-ggplot(data = filter(gather(data, key = "Parameter", value= "value", - age), Parameter %in% c("delay_intercept","delay_linear", "delay_quadratic" )))+
geom_line(aes(x = age, y = value, col = Parameter))+
scale_colour_manual(values = c(colourintercept, colourlinear, colourquadratic),
labels = c("intercept", "intercept + linear effect", "intercept + linear effect + quadratic effect "))+
theme(legend.key.width = unit(3,"cm"))
legend <- cowplot::get_legend(P_legend)
lay <- rbind(c(1,1,1,1),
c(1,1,1,1),
c(1,1,1,1),
c(2,2,2,2))
grid.arrange(P, ggdraw(legend), layout_matrix = lay)
}else{
P <- ggplot(data = data,aes(x = age, y = delay))+
geom_line(aes(x = age, y=delay_intercept),colour=colourintercept, size = 1.3)+
geom_ribbon(aes(ymin= delay_low_intercept, ymax = delay_high_intercept), fill = colourintercept, alpha = 0.3)+
geom_line(aes(x = age, y = delay_linear),colour = colourlinear, size=1.3)+
geom_ribbon(aes(ymin=delay_low_linear, ymax = delay_high__linear), fill = colourlinear, alpha = 0.3)+
geom_line(aes(x=age, y=delay_quadratic),colour = colourquadratic, size = 1.3)+
geom_ribbon(aes(ymin=delay_low_quadratic, ymax = delay_high_quadratic), fill = colourquadratic, alpha = 0.3)+
scale_y_continuous(name = "Delay (in months)")+
scale_x_continuous(name = "Age (in years)")+
coord_cartesian(xlim = c(input$range1[1], input$range1[2]),
ylim = c(input$range2[1], input$range2[2]))+
labs(title = "Plausible Values")+
annotate(geom = "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
theme_light()
P_legend<-ggplot(data = filter(gather(data, key="Parameter", value= "value", - age), Parameter %in% c("delay_intercept","delay_linear", "delay_quadratic" )))+
geom_line(aes(x = age, y = value, col = Parameter))+
scale_colour_manual(values = c(colourintercept, colourlinear, colourquadratic),
labels = c("intercept", "intercept + linear effect", "intercept + linear effect + quadratic effect "))+
theme(legend.key.width = unit(3,"cm"))
legend <- cowplot::get_legend(P_legend)
lay <- rbind(c(1,1,1,1),
c(1,1,1,1),
c(1,1,1,1),
c(2,2,2,2))
grid.arrange(P, ggdraw(legend), layout_matrix = lay)
}}
else{
if(input$check_box_3 == TRUE){
P <- ggplot(data = data, aes(x = age, y = delay))+
geom_line(data =filter(data, age>x1 & age<x2 & delay>y1 &delay<y2), aes(x = age, y = delay),colour=colourquadratic, size=1.3)+
geom_line(aes(x = age, y = delay), colour = colourquadratic, size=1.3, linetype = "dotted")+
geom_ribbon(aes(ymin = delay_low, ymax = delay_high), fill = colourquadratic, alpha = 0.3)+
coord_cartesian(xlim = c(0,120), ylim = c(-200, 1000))+
scale_x_continuous(name = "Age (in years)")+
scale_y_continuous(name = "Delay (in months)")+
labs(title = "Parameter Space")+
annotate(geom = "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
theme_light()
P_legend<-ggplot(data = data)+
geom_line(aes(x =age, y = delay, color = "Quadratic effect with combined variances"))+
scale_color_manual(name = "Parameter", values = c("Quadratic effect with combined variances" = colourquadratic))+
theme(legend.key.width = unit(3,"cm"))
legend <- cowplot::get_legend(P_legend)
lay <- rbind(c(1,1,1,1),
c(1,1,1,1),
c(1,1,1,1),
c(2,2,2,2))
grid.arrange(P, ggdraw(legend), layout_matrix = lay)
}else{
P <- ggplot(data = data, aes(x = age, y = delay))+
geom_line(colour = colourquadratic, size = 1.3)+
geom_ribbon(aes(ymin = delay_low, ymax = delay_high), fill = colourquadratic, alpha = 0.3)+
scale_y_continuous(name = "Delay (in months)")+
scale_x_continuous(name = "Age (in years)")+
coord_cartesian(xlim = c(input$range1[1], input$range1[2]),
ylim = c(input$range2[1], input$range2[2]))+
labs(title = "Plausible Values")+
annotate(geom = "rect",
xmin = input$range1[1],
xmax = input$range1[2],
ymin = input$range2[1],
ymax = input$range2[2],
alpha = .1,
col = substr(uu_color,2, 8))+
theme_light()
P_legend<-ggplot(data = data)+
geom_line(aes(x = age, y = delay, color = "Quadratic effect with combined variances"))+
scale_color_manual(name = "Parameter", values = c("Quadratic effect with combined variances" = colourquadratic))+
theme(legend.key.width = unit(3,"cm"))
legend <- cowplot::get_legend(P_legend)
lay <- rbind(c(1,1,1,1),
c(1,1,1,1),
c(1,1,1,1),
c(2,2,2,2))
grid.arrange(P, ggdraw(legend), layout_matrix = lay)
}}
})
output$Priors <- renderUI(
withMathJax(
HTML(paste0("Different software requires different specification of the hyperparameters. Look at the specification that is relevant for you. <br><br>",
"<b>Parametrized as N(mean, variance)</b><br>",
"The hyperparameters of the priors you have selected correspond to:",
"<ul><li>Intercept (\\(\\beta_{intercept}\\)) ~ N(", input$p3,", ", input$p8, ")",
"</li><li> regression coefficient age (\\(\\beta_{age}\\)) ~ N(", input$p1,", ", input$p6,")",
"</li><li> regression coefficient squared (\\(\\beta_{age^2}\\)) ~ N(", input$p2,", ", input$p7, ")","</li></ul>",
"<b>Parametrized as N(mean, sd) (ex. Stan)</b><br>",
"The hyperparameters of the priors you have selected correspond to:",
"<ul><li>Intercept (\\(\\beta_{intercept}\\)) ~ N(", input$p3,", ", signif(sqrt(input$p8),2), ")",
"</li><li> regression coefficient age (\\(\\beta_{age}\\)) ~ N(", input$p1,", ", signif(sqrt(input$p6),2),")",
"</li><li> regression coefficient squared (\\(\\beta_{age^2}\\)) ~ N(", input$p2,", ", signif(sqrt(input$p7),2), ")","</li></ul>",
"<b>Parametrized as N(mean, precision), precision is 1/variance (ex. JAGS)</b><br>",
"The hyperparameters of the priors you have selected correspond to:",
"<ul><li>Intercept (\\(\\beta_{intercept}\\)) ~ N(", input$p3,", ", signif(1/input$p8,2), ")",
"</li><li> regression coefficient age (\\(\\beta_{age}\\)) ~ N(", input$p1,", ", signif(1/(input$p6),2),")",
"</li><li> regression coefficient age squared(\\(\\beta_{age^2}\\)) ~ N(", input$p2,", ", signif(1/(input$p7),2), ")","</li></ul>")
)))
}