-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.R
449 lines (409 loc) · 18.2 KB
/
app.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
#######################################################
# Shiny App - An interactive visualisation of the DDM #
# --------------------------------------------------- #
# Written by Ladislas Nalborczyk #
# Last updated on September 3, 2020 #
#######################################################
library(shinythemes)
library(shinyhelper)
library(hrbrthemes)
library(tidyverse)
library(RWiener)
library(shiny)
################################################################################
############################# USER INTERFACE ###################################
################################################################################
ui <- shinyUI(
navbarPage(
title = "An interactive visualisation of the drift diffusion model",
# choose a theme
# themeSelector(),
# setting a theme
theme = shinytheme("sandstone"),
#####################################################################
# -------------------------- UI: 4DM -------------------------------#
#####################################################################
tabPanel(
title = "4DDM",
fluidPage(
withMathJax(),
fluidRow(h3("Simulating response time distributions") ),
fluidRow(h4(HTML(
"The code underlying this application can be found at
<a href='https://github.com/lnalborczyk/shiny_DDM'>
https://github.com/lnalborczyk/shiny_DDM</a>.
NB: the non-decision time is represented by the shaded area."
) ) ),
fluidRow(
sidebarLayout(
sidebarPanel(
id = "sidebar",
width = 4,
# height = "600px",
sliderInput(
inputId = "alpha",
label = "Select a value for the boundary separation (alpha)",
value = 2,
min = 1,
max = 3,
step = 0.1,
width = "500px"
) %>%
helper(
size = "m",
colour = "black",
type = "inline",
title = "Boundary separation",
content = "The boundary separation is the distance
between the two decision bounds and can be interpreted
as a measure of response caution, with high value
meaning high caution (or speed-accuracy trade-off
with high value meaning high accuracy).",
fade = TRUE
),
sliderInput(
inputId = "beta",
label = "Select a value for the starting point (beta)",
value = 0.5,
min = 0,
max = 1,
step = 0.1,
width = "500px"
) %>%
helper(
size = "m",
colour = "black",
type = "inline",
title = "Starting point",
content = "The starting point (or bias) of the
accumulation process is a measure of response
bias towards one of the two response boundaries,
reflecting the a priori expectation.",
fade = TRUE
),
sliderInput(
inputId = "delta",
label = "Select a value for the drift rate (delta)",
value = 0,
min = -2,
max = 2,
step = 0.1,
width = "500px"
) %>%
helper(
size = "m",
colour = "black",
type = "inline",
title = "Drift rate",
content = "The drift rate is the average slope
of the accumulation process towards the boundaries
(i.e., it represents the average amount of evidence
accumulated per unit of time). The larger the absolute
value of the drift rate, the stronger the evidence for
the corresponding response option (thus quantifying
the 'ease of processing').",
fade = TRUE
),
sliderInput(
inputId = "tau",
label = "Select a value for the non-decision time (tau)",
value = 1,
min = 0,
max = 2,
step = 0.1,
width = "500px"
) %>%
helper(
size = "m",
colour = "black",
type = "inline",
title = "Non-decision time",
content = "The non-decision time captures all
non-decisional processes such as stimulus encoding
and (motor) response processes.",
fade = TRUE
),
actionButton(
inputId = "refresh",
label = "Generate data",
width = "100%"
)
),
mainPanel(
id = "MainPanel",
width = 8,
plotOutput("BFdist.plot", width = "100%", height = "600px")
)
)
)
),
# footer
hr(),
HTML(
paste(
"Written by <a href='https://www.barelysignificant.com'>
Ladislas Nalborczyk</a>. Last update: September 3, 2020"
)
)
) # ends panel 4DDM
)
) # end UI
################################################################################
################################# SERVER #######################################
################################################################################
server <- function (input, output) {
##############################################################
# ---------------------- Server: 4DDM ---------------------- #
##############################################################
# displays helper text
observe_helpers(withMathJax = TRUE)
output$BFdist.plot <-
renderPlot({
# refresh the input
input$refresh
# extracts the user input as numeric values
alpha <- as.numeric(input$alpha)
beta <- as.numeric(input$beta)
delta <- as.numeric(input$delta)
tau <- as.numeric(input$tau)
# defines the grid of possible RT values
# rt_grid <- seq.int(from = 0, to = 15, length.out = 1e3)
# computes the lower density
# lower_dens <- dwiener(
# q = rt_grid,
# alpha = alpha, tau = tau, beta = beta, delta = delta,
# resp = "lower", give_log = FALSE
# )
# computes the upper density
# upper_dens <- dwiener(
# q = rt_grid,
# alpha = alpha, tau = tau, beta = beta, delta = delta,
# resp = "upper", give_log = FALSE
# )
# generates some data using the RWiener package
df <- rwiener(
n = 1e3,
alpha = alpha,
beta = beta,
delta = delta,
tau = tau
)
# computes the number of upper and lower responses
response_table <- table(df$resp) %>% data.frame
n_upper <- response_table %>%
filter(Var1 == "upper") %>%
pull(Freq) %>%
as.numeric
n_lower <- response_table %>%
filter(Var1 == "lower") %>%
pull(Freq) %>%
as.numeric
# computes densities
ud <- density(df$q[df$resp == "upper"], cut = 0)
ld <- density(df$q[df$resp == "lower"], cut = 0)
# rescales alpha between 0.5 and 1 to define the
# vertical blank space between densities
s <- (alpha / 10) * (1 - 0.5) + 0.5
# horizontal position of the densities
x <- c(
ud$x[1], ud$x, ud$x[length(ud$x)],
ld$x[1], ld$x, ld$x[length(ld$x)]
)
# vertical position of the densities
y <- c(s, ud$y + s, s, -s, -ld$y - s, -s)
# standardised starting point (relative to the standardised alpha)
lower_limit <- 0 - s / 2
upper_limit <- 0 + s / 2
beta_s <- lower_limit + beta * s
# computes the implied angle of the drift rate
drift_angle <- atan(delta) * 180 / pi
# new data frame
df2 <- data.frame(
x = x, y = y,
resp = rep(c("upper", "lower"), each = length(ud$x) + 2)
)
# gets maximum x-axis value
max_x <- max(df2$x)
# defines colors for lower and upper densities
lower_color <- "#c72e29"
upper_color <- "#016392"
# generates some evidence trajectories
# traj_length <- 100
# traj_y <- cumsum(c(beta_s, rnorm(traj_length-1, delta, 0.1) ) )
# traj_x <- seq.int(from = tau, to = max(df2$x), length.out = 1e2)
# when did the evidence crossed the boundary first?
# boundary_first_hit <- min(which(
# traj_y >= min(df2$y[df2$resp=="upper"]) |
# traj_y <= max(df2$y[df2$resp=="lower"])
# ) )
#
# trajectories <- data.frame(x = traj_x, y = traj_y) %>%
# slice(1:boundary_first_hit) %>%
# mutate(
# y = ifelse(
# row_number() == n(),
# max(df2$y[df2$resp=="lower"]),
# traj_y
# )
# )
############
# plotting #
############
df2 %>%
ggplot(aes(x = x, y = y, fill = resp, color = resp) ) +
# plotting densities
geom_segment(
data = . %>% filter(resp == "upper"),
aes(x = 0, xend = max_x, y = min(y), yend = min(y) ),
color = upper_color
) +
geom_segment(
data = . %>% filter(resp == "lower"),
aes(x = 0, xend = max_x, y = max(y), yend = max(y) ),
color = lower_color
) +
# plotting densities
geom_polygon(alpha = 0.8) +
# defining densities colors
scale_fill_manual(
values = c(lower_color, upper_color),
guide = guide_none()
) +
scale_color_manual(
values = c(lower_color, upper_color),
guide = guide_none()
) +
# plots some evidence trajectory
# geom_line(
# data = trajectories,
# aes(x = x, y = y),
# inherit.aes = FALSE,
# alpha = 0.5
# ) +
# stimulus onset
geom_vline(xintercept = 0, lty = 2, col = "grey30") +
annotate(
geom = "text",
x = 0, y = max(y),
hjust = 1,
vjust = -1,
size = 5, angle = 90,
label = "stimulus onset",
color = "grey30"
) +
# starting point
geom_hline(yintercept = beta_s, lty = 3, col = "purple") +
annotate(
geom = "label",
x = 0, y = beta_s,
hjust = 1,
vjust = 0.5,
size = 5,
label = "starting point",
colour = "purple"
) +
# non-decision time
annotate(
geom = "rect",
xmin = 0, xmax = tau,
ymin = -Inf, ymax = Inf,
alpha = 0.25
) +
# drift rate
# geom_segment(
# aes(
# x = tau,
# # xend = tau + 0.1,
# xend = tau + 0.1 * max_x,
# y = beta_s,
# # yend = log(delta / (1 - beta) ) + delta / 2
# # yend = beta_s + delta
# yend = ifelse(beta_s + delta > s, s, beta_s + delta)
# ),
# arrow = arrow(
# length = unit(0.2, "cm"),
# ends = "last", type = "closed"
# ),
# size = 0.5, colour = "darkgreen"
# ) +
geom_spoke(
aes(
x = tau, y = beta_s,
angle = drift_angle * (pi / 180),
radius = 0.5
),
arrow = arrow(
length = unit(0.2, "cm"),
ends = "last", type = "closed"
),
color = "darkgreen"
) +
annotate(
geom = "label",
x = tau,
y = beta_s,
hjust = 1, vjust = 0.5,
size = 5,
label = "drift rate",
color = "darkgreen"
) +
# boundary separation
geom_segment(
aes(
x = mean(x), xend = mean(x),
y = -s, yend = s,
),
arrow = arrow(
length = unit(0.2, "cm"),
ends = "both", type = "closed"
),
size = 0.5, colour = "black"
) +
annotate(
geom = "label",
x = mean(x), y = 0,
hjust = 0.5, vjust = 0.5,
size = 5,
label = "boundary separation",
color = "black"
) +
# labelling distributions
annotate(
geom = "label",
x = min(df2$x[df2$resp == "upper"]),
y = min(df2$y[df2$resp == "upper"]),
hjust = 0, vjust = -0.5,
size = 5,
label = paste0(
"RT distribution for upper responses (",
(n_upper / 1e3) * 100, "% of trials)"
)
) +
annotate(
geom = "label",
x = min(df2$x[df2$resp == "lower"]),
y = max(df2$y[df2$resp == "lower"]),
hjust = 0, vjust = 1.5,
size = 5,
label = paste0(
"RT distribution for lower responses (",
(n_lower / 1e3) * 100, "% of trials)"
)
) +
# aesthetics
theme_ipsum_rc(base_size = 14, axis_title_size = 14) +
theme(
axis.text.y = element_blank(),
plot.margin = unit(c(1, 1, 1, 3), "cm")
) +
labs(x = "Response time (in seconds)", y = "") +
# extends plotting area
coord_cartesian(xlim = c(0, NA), clip = "off") +
# adds a second axis on the top
scale_x_continuous(sec.axis = sec_axis(trans = ~.) )
})
}
# running the application
shinyApp(ui = ui, server = server)
# nobs = 1e3; alpha = 2; beta = 0.3; delta = 0.5; tau = 1;
# df <- rwiener(n = nobs, alpha = alpha, tau = tau, beta = beta, delta = delta)