-
Notifications
You must be signed in to change notification settings - Fork 0
/
N1App.R
423 lines (365 loc) · 17.3 KB
/
N1App.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
shiny.installed <- require(shiny)
if( !shiny.installed ){install.packages("shiny"); require(shiny)}
nlme.installed <- require(nlme)
if( !nlme.installed ){install.packages("nlme"); require(nlme)}
foreign.installed <- require(foreign)
if( !foreign.installed ){install.packages("foreign"); require(foreign)}
server <- shinyServer(function(input, output) {
myupload <- reactive({
input.file <- input$userfile
if (is.null(input.file)){
mydata <- data.frame()
return(mydata)
} else {
if(length(grep( ".sav$", input.file$name)) > 0){
mydata <- as.data.frame(read.spss(input.file$datapath))
}
if(length(grep( ".csv$", input.file$name)) > 0){
mydata <- as.data.frame(read.csv(input.file$datapath, sep = input$mycsvsep))
}
if(length(grep( ".txt$|.dat$", input.file$name)) > 0){
mydata <- as.data.frame(read.table(input.file$datapath, header = TRUE))
}
return(mydata)
}
})
output$mycsvInput <- renderUI({
input.file <- input$userfile
if (!is.null(input.file)){
if(length(grep( ".csv$", input.file$name)) > 0){
selectInput(inputId = "mycsvsep", label =
"Which symbol used to separate the columns in a CSV file?",
choices = c(",",";"))} else {
return(invisible)
}
}
})
output$phasecolumn <- renderUI({
input.file <- input$userfile
if (!is.null(input.file)){
mycolnames <- colnames(myupload())
selectInput(inputId = "myphasecolumn", label =
"Which column contains the phases?",
choices = mycolnames)
}
})
mydatatransform.selectphase <- reactive({
input.file <- input$userfile
if (!is.null(input.file)){
mytransformeddata <- myupload()
missingsymbols <- as.character(input$mymissingsymbol)
missingsymbols <- strsplit(missingsymbols,",")[[1]]
for( i in 1:length(missingsymbols)){
mytransformeddata[mytransformeddata == missingsymbols[i]] <- NA
}
names(mytransformeddata)[names(mytransformeddata) == input$myphasecolumn] <- "phase.column"
mytransformeddata <- mytransformeddata[c("phase.column",setdiff(names(mytransformeddata),"phase.column"))]
return(mytransformeddata)
}
})
output$varscolumn <- renderUI({
input.file <- input$userfile
if (!is.null(input.file)){
mycolnames <- setdiff(colnames(mydatatransform.selectphase()),"phase.column")
selectInput(inputId = "myvarscolumn", label =
"Which variable do you want to include in the analysis?",
choices = mycolnames)
}
})
mydatatransform.selectvars <- reactive({
input.file <- input$userfile
if (!is.null(input.file)){
mytransformeddata <- mydatatransform.selectphase()
mytransformeddata <- mytransformeddata[c("phase.column", input$myvarscolumn)]
return(mytransformeddata)
}
})
output$refphase <- renderUI({
input.file <- input$userfile
if (!is.null(input.file)){
myrownames <- as.character(unique(mydatatransform.selectvars()$phase.column))
selectInput(inputId = "myrefphase", label =
"What is the reference phase?",
choices = myrownames)
}
})
# output$RCIcheck <- renderUI({
# input.file <- input$userfile
# if (!is.null(input.file)){
#
# myrownames <- as.character(unique(mydatatransform.selectvars()$phase.column))
# checkboxInput(inputId = "myRCIcheck", label =
# "Do you want to compute the Reliable Change Index (RCI)?")
# }
# })
#
# output$RCImethod <- renderUI({
# input.file <- input$userfile
# if (!is.null(input.file)){
# if (input$myRCIcheck){
# myrownames <- as.character(unique(mydatatransform.selectvars()$phase.column))
# selectInput(inputId = "myRCIinfo", label =
# "What information is available?",
# choices = c("SD of the difference",
# "SD in norm and test-retest reliability"),
# selected = "SD of the difference")
# }
# }
# })
# output$sddiff <- renderUI({
# input.file <- input$userfile
# if (!is.null(input.file)){
# if (input$myRCIcheck ){
# if (input$myRCIinfo == "SD of the difference"){
# numericInput(inputId = "mysddiffinput", label =
# "What is the standard deviation of the difference?", value = 1)
# }
# }
# }
# })
# output$sdnorm <- renderUI({
# input.file <- input$userfile
# if (!is.null(input.file)){
# if (input$myRCIcheck){
# if (input$myRCIinfo == "SD in norm and test-retest reliability"){
# numericInput(inputId = "mysdnorminput", label =
# "What is the standard deviation in the norm group?", value = 1)
# }
# }
# }
# })
# output$testretestrel <- renderUI({
# input.file <- input$userfile
# if (!is.null(input.file)){
# if (input$myRCIcheck){
# if (input$myRCIinfo == "SD in norm and test-retest reliability"){
# numericInput(inputId = "myreliabilityinput", label =
# "What is the test-retest reliability?", value = 0, min = -1, max = 1)
# }
# }
# }
# })
output$plotButton <- renderUI({
input.file <- input$userfile
if (!is.null(input.file)){
actionButton( inputId = "goButton", label = "Run analysis")
}
})
mydatatransform.reorganized <- reactive({
input.file <- input$userfile
if (!is.null(input.file)){
mytransformeddata <- mydatatransform.selectvars()
char.myrefphase <- as.character(input$myrefphase)
mytransformeddata$phase.column <- relevel( mytransformeddata$phase.column, ref = char.myrefphase)
varname <- as.character(input$myvarscolumn)
mytransformeddata$time <- 1:nrow(mytransformeddata)
mytransformeddata$id <- 1
mytransformeddata$timeinphase <- NA
names(mytransformeddata)[names(mytransformeddata)==varname] <- "y"
for( i in unique(mytransformeddata$phase.column)){
mytransformeddata$timeinphase[mytransformeddata$phase.column == i] <-
(sum(mytransformeddata$phase.column == i)-1):0
}
return(mytransformeddata)
}
})
generatemodel <- reactive({
input.file <- input$userfile
if (!is.null(input.file)){
mytransformeddata <- mydatatransform.reorganized()
mymodel <- try(gls( y ~ 1 + phase.column * timeinphase,
correlation = corAR1(form = ~ time | id), data = na.omit(mytransformeddata)))
if( grepl("glsEstimate", mymodel[[1]])){
modelcheck <- "Error"; print(modelcheck) } else
if( mymodel$apVar[1] == "Non-positive definite approximate variance-covariance")
{ modelcheck <- mymodel$apVar; print(modelcheck) } else {
return(mymodel)
}
}
})
output$phases <- renderUI({
input.file <- input$userfile
if (!is.null(input.file)){
myrownames <- mydatatransform.reorganized()$phase.column
checkboxGroupInput(inputId = "myselectedphases", label =
"Which phases do you want to include in the analysis?",
choices = myrownames, select = myrownames)
}
})
# myRCIresults <- eventReactive(input$goButton, {
# input.file <- input$userfile
# if (!is.null(input.file)){
# if (input$myRCIcheck){
# if (input$myRCIinfo == "SD of the difference between end of the two phases"){
# Sdiff <- input$mysddiffinput
# }
# if (input$myRCIinfo == "SD in norm and test-retest reliability"){
# SE <- input$mysdnorminput * (sqrt(1 - input$myreliabilityinput))
# Sdiff <- sqrt(2 * SE^2)
# }
#
# RCI <- summary(generatemodel())$tTable[2,1] / Sdiff
# RCI <- ifelse( (abs(RCI) > 1.96), paste0( round(RCI,2), "*"), paste0( round(RCI,2)))
# return( data.frame( RCI = RCI))
# }
# }
# })
myresults <- eventReactive(input$goButton, {
input.file <- input$userfile
if (!is.null(input.file)){
mytransformeddata <- mydatatransform.reorganized()
effectslabels <- rownames(summary(generatemodel())$tTable)
effectslabels[effectslabels == "(Intercept)"] <- paste0("Does ''", input$myvarscolumn,"'' at the end of ''", input$myrefphase, "''-phase differ from zero?")
effectslabels[ 2: length(unique(mytransformeddata$phase.column))] <- paste0(gsub("phase.column", paste0("Does ''", input$myvarscolumn,"'' at the end of ''", input$myrefphase, "''-phase differ from ''", input$myvarscolumn,"'' at the end of ''"), effectslabels[ 2: length(unique(mytransformeddata$phase.column))]), "''-phase?")
effectslabels[ length(unique(mytransformeddata$phase.column)) + 1 ] <- paste0("Does time have an effect on ''", input$myvarscolumn, "'' in ''", input$myrefphase, "''-phase?")
effectslabels[ (length(unique(mytransformeddata$phase.column)) + 2) : length(effectslabels) ] <- gsub("phase.column", "Is there a difference between effect of time in ''", effectslabels[ (length(unique(mytransformeddata$phase.column)) + 2) : length(effectslabels) ])
effectslabels[ (length(unique(mytransformeddata$phase.column)) + 2) : length(effectslabels) ] <- gsub(":timeinphase", paste0("''-phase and effect of time in ''", input$myrefphase, "''-phase?"), effectslabels[ (length(unique(mytransformeddata$phase.column)) + 2) : length(effectslabels) ])
if( nrow(mytransformeddata) / length(unique(mytransformeddata$phase)) < 6){
alpha <- 0.01 } else {
alpha <- 0.05
}
significants <- (as.data.frame(summary(generatemodel())$tTable)["p-value"] < alpha) * 1
significants[significants == 1] <- "Yes"
significants[significants == 0] <- "No"
colnames(significants) <- "Significant?"
resultstable <- cbind(effectslabels,
format(round(summary(generatemodel())$tTable,3),digits = 3),
significants)
resultstable <- rbind(resultstable, c("Carry-over from previous measurement (AR(1) parameter)", format(round(coef(generatemodel()$modelStruct, unconstrained = FALSE),2),digits = 2), rep(NA, 4)))
resultstable[is.na(resultstable)] <- " "
colnames(resultstable)[1] <- "Effects"
colnames(resultstable)[2] <- "Parameter value"
return(resultstable)
}
})
myplot <- eventReactive(input$goButton, {
input.file <- input$userfile
if (!is.null(input.file)){
mytransformeddata <- mydatatransform.reorganized()
fitted <- fitted(generatemodel())
varname <- input$myvarscolumn
theplot <- plot(mytransformeddata$time,
mytransformeddata$y, xaxt = "n", xlab = "time", ylab = varname, bty = "n")
axis(side = 1, at = mytransformeddata$time)
phasecolor <- 1
for( phase in unique(mytransformeddata$phase.column)){
phasecolor <- phasecolor + 1
points(mytransformeddata$time[mytransformeddata$phase.column == phase], mytransformeddata$y[mytransformeddata$phase.column == phase], pch = 16, cex = 1.5, col = phasecolor)
lines(mytransformeddata$time[mytransformeddata$phase.column == phase], mytransformeddata$y[mytransformeddata$phase.column == phase], lty = 2, lwd = 3, col = phasecolor)
lines(as.numeric(names(fitted(generatemodel()))), fitted(generatemodel()), col = "black", lty = 1, lwd = 4)
abline( v = min(mytransformeddata$time[mytransformeddata$phase.column == phase]) - 0.5, lwd = 3)
abline( v = max(mytransformeddata$time[mytransformeddata$phase.column == phase]) + 0.5, lwd = 3)
}
text( quantile(mytransformeddata$time,.75, na.rm = TRUE), quantile(mytransformeddata$y,.90, na.rm = TRUE), "black line = model")
text( quantile(mytransformeddata$time,.75, na.rm = TRUE), quantile(mytransformeddata$y,.75, na.rm = TRUE), "colored line = data")
return(theplot)
}
})
mydatatransform.reorganized.output <- eventReactive(input$goButton, {
input.file <- input$userfile
if (!is.null(input.file)){
mytransformeddata <- mydatatransform.reorganized()
return(mytransformeddata)
}
})
output$myuploadeddata <- renderDataTable({
return(myupload())
}, options = list( info = FALSE, paging = FALSE, ordering = FALSE, searching = FALSE))
output$myreorganizeddata <- renderDataTable({
return(mydatatransform.reorganized.output())
}, options = list( info = FALSE, paging = FALSE, ordering = FALSE, searching = FALSE))
output$myresultstable <- renderDataTable({
return(myresults())
}, options = list( info = FALSE, paging = FALSE, ordering = FALSE, searching = FALSE))
# output$myRCItable <- renderDataTable({
# return(myRCIresults())
# }, options = list( info = FALSE, paging = FALSE, ordering = FALSE, searching = FALSE))
#
output$mainOutputPlot <- renderPlot({
myplot()
})
output$mystandardtext <- renderText({
return(infotext())
})
output$downloadReport <- downloadHandler(
filename = 'my-report.docx',
content = function(file) {
src <- normalizePath('mytemplate.Rmd')
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'mytemplate.Rmd')
out <- render('mytemplate.Rmd', output_format = word_document())
file.rename(out, file)
})
})
ui <- shinyUI(fluidPage(
#includeCSS("styles.css"),
# tags$style(type="text/css",
# ".shiny-output-error { visibility: hidden; }",
# ".shiny-output-error:before { visibility: hidden; }"
# ),
# Application title
titlePanel("E-clip, N = 1 analysis"),
# Sidebar with a slider input for number of bins
sidebarLayout(
div(class="mysidebarpanel",
sidebarPanel(
h3("Input"),
fileInput(inputId = "userfile",label = "Upload file", accept = ".sav,.csv,.txt,.dat"
),
uiOutput("mycsvInput"),
textInput(inputId = "mymissingsymbol", value = "9999,99999", label =
"Which symbol(s) used to signify missing values? (separate by comma)"),
uiOutput("phasecolumn"),
uiOutput("refphase"),
uiOutput("varscolumn"),
uiOutput("participants"),
# uiOutput("RCIcheck"),
# uiOutput("RCImethod"),
# uiOutput("sddiff"),
# uiOutput("sdnorm"),
# uiOutput("testretestrel"),
uiOutput("plotButton"),
br()#,
#downloadButton('downloadReport', label = "Download report")
)),
# Show a plot of the generated distribution
mainPanel(
h3("Output"),
tabsetPanel( selected = "Uploaded data",
tabPanel( title = "Uploaded data",
dataTableOutput("myuploadeddata")),
tabPanel( title = "Results",
h3("Results"),
dataTableOutput("myresultstable")),
# h3("Reliable Change Index"),
# dataTableOutput("myRCItable")),
tabPanel( title = "Plot",
plotOutput("mainOutputPlot")),
tabPanel( title = "Additional information",
h3("Assumptions:"),
tags$ul(
tags$li("the effects of time are linear in both phases;"),
tags$li("residuals are normally distributed;"),
tags$li("residuals are correlated according to an autoregressive structure.")
),
h3("Additional information:"),
tags$ul(
tags$li("If the mean number of data points per group is less than six, an alpha of 0.01 is used to determine whether a result is significant, otherwise, an alpha of 0.05 is used;"
)),
h3("References"),
HTML(paste0("Maric, M., de Haan, E., Hogendoorn, S. M., Wolters, L. H., & Huizenga, H. M. (2014).
Evaluating Statistical and Clinical Significance of Intervention Effects in Single-Case
Experimental Designs: An SPSS Method to Analyze Univariate Data. ", tags$i("Behavior Therapy"), ", ", tags$i("46"), ", 230-241.")),
br(),
HTML(paste0("Winston Chang, Joe Cheng, JJ Allaire, Yihui Xie and Jonathan McPherson
(2015). shiny: Web Application Framework for R. R package version
0.12.0.", tags$a(href = "http://CRAN.R-project.org/package=shiny", "http://CRAN.R-project.org/package=shiny"))),
br(),
HTML(paste0("Pinheiro, J. C., & Bates, D. M. (2000).", tags$i("Mixed-effects models in S and S-PLUS."), " Springer Science & Business Media."))),
tabPanel( title = "Reorganized data",
dataTableOutput("myreorganizeddata"))
)
)
)))
shinyApp(ui = ui, server = server, options = list( launch.browser = TRUE ))