-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
368 lines (338 loc) · 10.5 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
# load packages
library("shiny")
library("shinycssloaders")
library("ggplot2")
library("plotly")
library("markdown")
# source required files
source("simVARS.R")
source("plotFunctions.R")
# set options for app
options(shiny.autoreload = TRUE)
options(shiny.error = FALSE)
options(spinner.type = 7)
options(spinner.size = 0.2)
options(spinner.color = "grey")
ui <- tagList(
tags$head(tags$style(HTML("
.navbar-nav {
float: none !important;
}
.navbar-nav > li:nth-child(1) {
float: right;
}
#element {
transform: scale(1);
transform-origin: top center;
}
"))),
navbarPage("Dyadic Interaction Dynamics",
id = "topnavbar",
selected = "simulate",
# TERMS Of USAGE
tabPanel(
"By using this app you agree with the Terms of Usage.",
fluidRow(
column(6, includeMarkdown("Shiny/tou.Rmd"))
)
),
# SIMULATE TAB
tabPanel("Simulate",
value = "simulate",
h4("Simulate Data"),
tabsetPanel(
id = "simulateTabs",
tabPanel("Set up & Visualization",
value = "sim",
br(),
# DATA GENERATING MODEL
selectInput("model", "Choose a data generating model",
list(
"First-order vector autoregressive VAR(1)" = "VAR",
"Latent VAR(1)" = "L",
"Time-varying VAR(1)" = "TV",
"Threshold VAR(1)" = "T",
"Hidden Markov model" = "HMM",
"Markov-switching VAR(1)" = "MS"
),
selected = "VAR"
),
hr(),
# INPUT DATA GENERATION
fluidRow(
column(
2,
style = "padding-top:2em",
methodUI("method") # measurement occasions and seed
),
column(
10,
fluidRow(
# input y
column(
4,
formulaModelUI("formula_model_y"),
paramsUI("innerParamsY"),
hr(),
conditionalPanel(
condition = "input.model == 'T'",
numericInput(
"tau_y", HTML("Threshold 𝜏"),
0, width = "30%"
)
),
formulaUI("formula_y")
),
# input x
column(
4,
formulaModelUI("formula_model_x"),
paramsUI("innerParamsX"),
hr(),
conditionalPanel(
condition = "input.model == 'T'",
numericInput(
"tau_x", HTML("Threshold 𝜏"),
0, width = "30%"
)
),
formulaUI("formula_x")
),
# input residuals
column(
4,
formulaModelUI("formula_model_z"),
errorsUI("errors"),
conditionalPanel(
condition = "input.model == 'T'",
hr(),
h5("For all regime combinations"),
fluidRow(
column(
6,
numericInput("yx_T", "Correlation", .3, -1, 1, .1),
)
)
),
# transition probabilities
conditionalPanel(
condition = "input.model == 'MS' || input.model == 'HMM'",
tabsetPanel(
id = "transition",
tabPanel(
"Transition probabilities",
fluidRow(
style = "padding-top:5px",
column(
6,
numericInput(
"pi_o", "Stay in 1", .5, 0, 1, .1,
width = "60%"
),
tableOutput("pi_ot")
),
column(
6,
tableOutput("pi_to"),
numericInput(
"pi_t", "Stay in 2", .5, 0, 1, .1,
width = "60%"
)
)
)
)
)
),
hr(),
formulaUI_z("formula_z")
)
)
)
),
# time-varying plots
conditionalPanel(
condition = "input.model == 'TV'",
hr(),
h4("Parameters plotted over time"),
plotsTvUI("tvPlots"),
),
hr(),
# generated data plots
plotsInputUI("inputPlots")
),
# tabPanel("Estimation",
# value = "estimation",
# estimationUI("estimation")
# ),
# download generated data
tabPanel("Download",
value = "data",
fluidRow(
style = "padding-top:5px",
column(
9,
withSpinner(DT::dataTableOutput("table"))
),
column(
3,
radioButtons(
"dataFormat", "Choose data format",
choices = list('"Wide"' = "wide", "Long" = "long")
),
downloadButton("downloadData", "Download data")
)
)
)
)
),
# UPLOAD TAB
tabPanel(
"Upload",
h4("Upload Data"),
uploadInputUI("uploadData"),
hr(),
plotsInputUI("uploadPlots")
),
# INFO TAB
tabPanel(
"Info",
value = "info",
h4("Info"),
fluidRow(
column(
8,
navlistPanel(
well = FALSE, widths = c(3, 9),
tabPanel("Plots", shiny::includeMarkdown("Shiny/plots.Rmd")),
"Simulate",
tabPanel("Set up & Visualization", shiny::includeMarkdown("Shiny/setup.Rmd")),
tabPanel("Download", shiny::includeMarkdown("Shiny/download.Rmd")),
"Upload",
tabPanel("Upload Data", shiny::includeMarkdown("Shiny/upload.Rmd"))
)
),
column(
4,
sidebarPanel(includeMarkdown("Shiny/info.Rmd"), width = 12)
)
)
)
)
)
server <- function(input, output, session) {
#--------------------------------- SIMULATE -----------------------------------#
method <- methodServer("method")
formulaModelServer("formula_model_y", reactive({ input$model }), "y")
formulaModelServer("formula_model_x", reactive({ input$model }), "x")
formulaModelServer("formula_model_z", reactive({ input$model }), "z")
# the sim tab should have long format data for plotting
observeEvent(input$simulateTabs, {
if (input$simulateTabs == "sim") {
updateRadioButtons(session, "dataFormat", selected = "long")
}
if (input$simulateTabs == "data") {
updateRadioButtons(session, "dataFormat", selected = "wide")
}
})
# opposite transition probabilities
output$pi_to <- renderTable({
pi_to <- data.frame(1 - input$pi_t)
colnames(pi_to) <- "Switch to 1"
return(pi_to)
},
align = "l"
)
output$pi_ot <- renderTable({
pi_ot <- data.frame(1 - input$pi_o)
colnames(pi_ot) <- "Switch to 2"
return(pi_ot)
},
align = "l"
)
params_y <- paramsServer(
"innerParamsY", model = reactive({ input$model }), t = method$t,
tau = reactive({ input$tau_y }), paramsOther = params_x, partner = "y"
)
params_x <- paramsServer(
"innerParamsX", model = reactive({ input$model }), t = method$t,
tau = reactive({ input$tau_x }), paramsOther = params_y, partner = "x"
)
errors <- errorsServer("errors", model = reactive({ input$model }))
probs <- reactive({
p <- NULL
if (input$model == "MS" || input$model == "HMM") {
p <- c(input$pi_o, input$pi_t)
}
return(p)
})
# GENERATE DATA
dat <- reactive({
ifelse(input$dataFormat == "long", longformat <- TRUE, longformat <- FALSE)
set.seed(method$seed())
dat <- simVARS(
occasions = method$t(),
burnin = 100,
type = input$model,
params_y = params_y()$coefs,
params_x = params_x()$coefs,
probs = probs(),
indicators_y = params_y()$indicator,
indicators_x = params_x()$indicator,
errors = errors()$measurement,
innovations = errors()$dynamic,
longformat = longformat
)
dat
})
# estimationServer("estimation", dataFormat, dat, params, reactive({ input$model }))
formulaServer(
"formula_y", reactive({ input$model }), partner = "y",
params = params_y, reactive({ input$tau_y })
)
formulaServer(
"formula_x", reactive({ input$model }), partner = "x",
params = params_x, reactive({ input$tau_x })
)
formulaServer_z(
"formula_z", reactive({ input$model }), reactive({ input$yx_T }), errors
)
# PLOTS
dataFormat <- reactive({
input$dataFormat
})
plotsServer("inputPlots", dataFormat, reactive({ input$model }), dat,
tau = reactive({ list(y = input$tau_y, x = input$tau_x) })
)
plotsTvServer("tvPlots", method$t, y = params_y, x = params_x)
# TABLE
output$table <- DT::renderDataTable({
dtable <- DT::datatable(dat(),
rownames = FALSE,
options = list(dom = "pt", pageLength = 15)
)
cols <- names(dplyr::select(dat(), where(is.numeric)))
cols <- cols[-which(
cols == "t" | cols == "regime" | cols == "regime_y" | cols == "regime_x"
)]
dtable <- DT::formatRound(dtable, columns = cols, digits = 3)
dtable
})
# DOWNLOAD BUTTON
output$downloadData <- downloadHandler(
filename = "dyadic-interaction-dynamics.csv",
content = function(file) {
write.csv(dat(), file, row.names = FALSE)
}
)
#----------------------------------- UPLOAD -----------------------------------#
uploaded <- uploadInputServer("uploadData")
plotsServer("uploadPlots", dataFormat,
model = function() { "VAR" },
dat = uploaded$datLong, uploaded = TRUE,
uploadedFile = uploaded$file
)
}
shinyApp(
ui = ui,
server = server
)