-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEMAeval_Functions_Script.R
More file actions
740 lines (553 loc) · 27.9 KB
/
EMAeval_Functions_Script.R
File metadata and controls
740 lines (553 loc) · 27.9 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
########### Library #####################################
# Below are the package dependencies that will be needed in order for the functions
# listed below to run properly. Also, R version must be later than 3.6.2
# Some of the base functions needed will be in R version 3.6.2 and later.
# library(plyr)
# library(dplyr)
# library(reshape)
# library(lubridate)
# library(ggplot2)
# library(TSA)
# library(smooth)
# library(qualtRics)
# library(DescTools)
########### DESCRIPTION############
# The following script can be used in lue of the EMAeval Package.
# This script includes Qualtrics functions to produce flagging plots, and flag any careless responders.
#
#
# Every function must use data in a data.frame configuration. The variable "data" is present in every function.
# Fill this function by data = YourData.
#
# The functions will be included in the script, all that is needed is to load each function
# and to run the functions as described in each Notes section below the function, with your data.
#
# Please email nkraus@miami.edu if you have any questions.
########## Flagging Plots function#######################
flagging.plots <- function(data, ttc.colnames, item.colnames, ttc.plotx.max, sd.plotx.max, mode.plotx.max, na.rm){
if (missing(ttc.colnames)){
ttc.colnames <- stop("ttc.colnames missing.\nSpecify Start-Date & End-Date / Completion Time with ttc.colnames\n")
}
if (missing(number.items)){
number.items <- stop("number.items missing.\nSpecify number of items with number.items\n")
}
if (missing(item.colnames)){
item.colnames <- stop("item.colnames missing.\nSpecify number of items with item.colnames\n")
}
if (missing(ttc.plotx.max)){
ttc.plotx.max <- 100
}
if (missing(sd.plotx.max)){
sd.plotx.max <- 50
}
if (missing(mode.plotx.max)){
mode.plotx.max <- 100
}
if (missing(na.rm)){
na.rm <- FALSE
}
#comparison DF for plots
calc.df <- data.frame(matrix(NA, nrow = nrow(data), ncol = 4))
data <- as.data.frame(data)
for (idx in 1:nrow(data)){
#calculate TTC and place in calc.df
if (length(ttc.colnames[!is.na(ttc.colnames)]) > 1){
starttime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[1])])
endtime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[2])])
calc.df[idx,1] <- as.numeric(endtime-starttime, units="secs")
} else if (length(ttc.colnames[!is.na(ttc.colnames)]) == 1){
calc.df[idx,1] <- (ttc.colnames[!is.na(ttc.colnames)])
} else {
calc.df[idx,1] <- NA
}
#calculate TPI and place in calc.df
item_length <- length(which(!is.na(data[idx, item.colnames])))
calc.df[idx,2] <- (calc.df[idx,1] / item_length)
#calculate SD and place in calc.df
if (length(item.colnames[!is.na(item.colnames)]) == 0){
calc.df[idx,3] <- NA
} else {
calc.df[idx,3] <- round(sd(data[idx, which(colnames(data) %in% item.colnames[!is.na(item.colnames)])], na.rm = TRUE),2)
}
#calculate mode and place in calc.df
MODE1 = as.vector(lsr::modeOf(as.numeric(data[idx, which(colnames(data) %in% item.colnames[!is.na(item.colnames)])]), na.rm = TRUE))
if (length(MODE1) == 1){
calc.df[idx,4] = MODE1
} else if (length(MODE1) == length(item.colnames[!is.na(item.colnames)])){
calc.df[idx,4] = NA
} else{
calc.df[idx,4] = paste(MODE1, collapse=",")
}
}
#Mode for plots
Mode_List <- NULL
for (jdx in 1:nrow(data)){
new_mode = as.vector(apply(data[jdx, which(colnames(data) %in% item.colnames[!is.na(item.colnames)])], 1, DescTools::Mode))
Mode_List <- c(Mode_List, new_mode)
}
calc.df <- as.data.frame(calc.df)
colnames(calc.df)[1:4] <- c("TTC", "TPI", "SD", "Longstring")
Mode_List <- as.data.frame(Mode_List)
#Plots:
pTTC <- ggplot(data = calc.df, aes(TTC)) +
geom_histogram(breaks= seq(0,ttc.plotx.max, by = 1),
col="blue",
fill="dark blue",
alpha = 0.3) +
labs(subtitle = "Time to Complete Per Assessment") +
labs(x="Time To Complete (seconds)", y="Count", tag = "A") +
theme_classic()+
scale_x_continuous(breaks = seq(0,ttc.plotx.max,10))+
#scale_y_continuous(breaks = seq(0,800,100))+
theme(plot.title = element_text(hjust = 0.5, size = 18), axis.title.x = element_text(face = "bold"), axis.title.y = element_text(face = "bold"))
pTPI <- ggplot(data = calc.df, aes(TPI)) +
geom_histogram(breaks= seq(0,(ttc.plotx.max/number.items), by = 0.5),
col="blue",
fill="dark blue",
alpha = 0.3) +
labs(subtitle = "Time Per Item") +
labs(x="Time Per Item(seconds)", y="Count", tag = "B") +
theme_classic()+
scale_x_continuous(breaks = seq(0,(ttc.plotx.max/number.items),2))+
# scale_y_continuous(breaks = seq(0,30,1))+
theme(plot.title = element_text(hjust = 0.5, size = 18), axis.title.x = element_text(face = "bold"), axis.title.y = element_text(face = "bold"))
pSD <- ggplot(data = calc.df, aes(SD)) +
geom_histogram(breaks= seq(0,sd.plotx.max, by = 1),
col="blue",
fill="dark blue",
alpha = 0.3) +
labs(subtitle = "Standard Deviation Per Assessment") +
labs(x="Standard Deviation", y="Count", tag = "C")+
theme_classic()+
scale_x_continuous(breaks = seq(0,sd.plotx.max,5))+
#scale_y_continuous(breaks = seq(0,800,100))+
theme(plot.title = element_text(hjust = 0.5, size = 18), axis.title.x = element_text(face = "bold"), axis.title.y = element_text(face = "bold"))
pMODE <- ggplot(data = Mode_List, aes(Mode_List)) +
geom_histogram(breaks= seq(0,mode.plotx.max, by = 1),
col="blue",
fill="dark blue",
na.rm = TRUE,
alpha = 0.3) +
labs(subtitle = "Longstring Score Per Assessment") +
labs(x="Modal Score", y="Count", tag = "D")+
theme_classic()+
scale_x_continuous(breaks = seq(0,mode.plotx.max,10))+
#scale_y_continuous(breaks = seq(0,3000,1000))+
theme(plot.title = element_text(hjust = 0.5, size = 18), axis.title.x = element_text(face = "bold"), axis.title.y = element_text(face = "bold"))
library(gridExtra)
library(ggpubr)
gridExtra::grid.arrange(grid.arrange(pTTC, pTPI, pSD, pMODE, nrow = 2, top = text_grob("Flagging Identification Plots", size = 20, just = "center")))
}
# Notes:
# The following variables for the function can be adjusted in order to create a better visualization based on your data.
# For now, the default values for the x axis limits for the following plots are
# ttc.plotx.max = 100, sd.plotx.max = 50, mode.plotx.max = 100
#
#
# The ttc.colnames can have 1 (if your data already has a assessment completion time with the data point),
# or 2 (if a start date and end date are provided in data).
#
# The order for the ttc.colnames MUST be start date, then end date.
#
#
# The commented out code below serves as an example.
# flagging.plots(Data1, ttc.colnames = c("StartDate", "EndDate") , number.items = 11, item.colnames = colnames(Data1[,7:17]))
# flagging.plots(Data2, ttc.colnames = c("CompletionTime") , number.items = 8, item.colnames = c("Upset", "Excited", "Irritable", "Content", "Attentive", "Stressed", "Relaxed", "Anxious"))
#########################################################
########## Flagging DF function##########################
# This function is creates the dataframe that is used to create
# the plots for the last function. This can be used for further analyses
flagging.df <- function(data, ttc.colnames, number.items, item.colnames){
if (missing(ttc.colnames)){
ttc.colnames <- stop("ttc.colnames missing.\nSpecify Start-Date & End-Date / Completion Time with ttc.colnames\n")
}
if (missing(number.items)){
number.items <- stop("number.items missing.\nSpecify number of items with number.items\n")
}
if (missing(item.colnames)){
item.colnames <- stop("item.colnames missing.\nSpecify number of items with item.colnames\n")
}
#comparison DF for plots
calc.df <- data.frame(matrix(NA, nrow = nrow(data), ncol = 4))
data <- as.data.frame(data)
for (idx in 1:nrow(data)){
#calculate TTC and place in calc.df
if (length(ttc.colnames[!is.na(ttc.colnames)]) > 1){
starttime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[1])])
endtime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[2])])
calc.df[idx,1] <- as.numeric(endtime-starttime, units="secs")
} else if (length(ttc.colnames[!is.na(ttc.colnames)]) == 1){
calc.df[idx,1] <- (ttc.colnames[!is.na(ttc.colnames)])
} else {
calc.df[idx,1] <- NA
}
#calculate TPI and place in calc.df
item_length <- length(which(!is.na(data[idx, item.colnames])))
calc.df[idx,2] <- (calc.df[idx,1] / item_length)
#calculate SD and place in calc.df
if (length(item.colnames[!is.na(item.colnames)]) == 0){
calc.df[idx,3] <- NA
} else {
calc.df[idx,3] <- round(sd(data[idx, which(colnames(data) %in% item.colnames[!is.na(item.colnames)])], na.rm = TRUE),2)
}
#calculate mode and place in calc.df
MODE1 = as.vector(lsr::modeOf(as.numeric(data[idx, which(colnames(data) %in% item.colnames[!is.na(item.colnames)])]), na.rm = TRUE))
if (length(MODE1) == 1){
calc.df[idx,4] = MODE1
} else if (length(MODE1) == length(item.colnames[!is.na(item.colnames)])){
calc.df[idx,4] = NA
} else{
calc.df[idx,4] = paste(MODE1, collapse=",")
}
}
calc.df <- as.data.frame(calc.df)
colnames(calc.df)[1:4] <- c("TTC", "TPI", "SD", "Longstring")
calc.df
}
# Notes:
#
# The ttc.colnames can have 1 (if your data already has a assessment completion time with the data point),
# or 2 (if a start date and end date are provided in data).
#
# The order for the ttc.colnames MUST be start date, then end date.
#
#
# The commented out code below serves as an example.
# flagging.df(Data1, ttc.colnames = c("StartDate", "EndDate") , number.items = 11, item.colnames = colnames(Data1[,7:17]))
# flagging.df(Data2, ttc.colnames = c("CompletionTime") , number.items = 8, item.colnames = c("Upset", "Excited", "Irritable", "Content", "Attentive", "Stressed", "Relaxed", "Anxious"))
#########################################################
########## Lonstringr function##########################
# This function creates a vector of all the longstring values for further analysis if needed.
# It does not associate which assessment or ID the lonstring value came from. This should be used
# for visualization of the longstring and analysis of the values in general.
longstringr <- function(data, item.colnames) {
#Mode
Mode_List <- NULL
for (jdx in 1:nrow(data)){
new_mode = as.vector(apply(data[jdx, which(colnames(data) %in% item.colnames[!is.na(item.colnames)])], 1, DescTools::Mode))
Mode_List <- c(Mode_List, new_mode)
}
Mode_List
}
# Notes:
#
# All that is needed are the column names of the Items used for the longstring analysis.
# This will provide you with a list of all possible longstring values.
# There may be more longstring values due to the possiblility of multiple modal values for one participant.
#
# The commented out code below serves as an example.
# longstringr(Data1, item.colnames = colnames(Data1[,7:21]))
# longstringr(Data2, item.colnames = c("Upset", "Excited", "Irritable", "Content", "Attentive", "Stressed", "Relaxed", "Anxious"))
#########################################################
########## TPI.cutoff function##########################
# This function will flag all the assessments that have are identified by the cutoff
# specified by the user. This function will generate a vector of indices for the
# dataframe provided by the user. This will NOT provide a list of how many times a subject is flagged for this cutoff.
TPI.cutoff <- function(data, cutoff, ttc.colnames, number.items, mandatory.response, item.colnames, ID.colname){
if (missing(ttc.colnames)){
ttc.colnames <- stop("ttc.colnames missing. Specify Start-Date & End-Date / Completion Time with ttc.colnames\n")
}
if (isTRUE(mandatory.response) & missing(number.items)){
number.items <- stop("number.items missing. Specify number of items with number.items\n")
}
if (missing(mandatory.response)){
mandatory.response <- stop("mandatory.response missing.\nSpecify if response to all items was mandatory, resulting in with no NAs for item responses.
\nIf responses are mandatory, mandatory.response = TRUE \nIf responses are NOT mandatory, mandatory.response = FALSE")
}
if (isFALSE(mandatory.response) & missing(item.colnames)){
item.colnames <- stop("item.colnames missing.\nSpecify column names of items with item.colnames\n")
}
if (missing(cutoff)){
warning("cutoff missing.\nCutoff value automatically set to 2 seconds. Specify cutoff value to change from 2 seconds with variable 'cutoff = ' \n")
cutoff <- 2
}
newDF <- c()
for(idx in 1:nrow(data)){
# number of items if response to items not mandatory
if(isFALSE(mandatory.response)){
item_length <- length(which(!is.na(data[idx, item.colnames])))
} else {
item_length <- number.items
}
# calc TTC for the assessment
data_TTC <- c()
if (length(ttc.colnames[!is.na(ttc.colnames)]) > 1){
starttime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[1])])
endtime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[2])])
data_TTC <- as.numeric(endtime-starttime, units="secs")
} else if (length(ttc.colnames[!is.na(ttc.colnames)]) == 1){
data_TTC <- (ttc.colnames[!is.na(ttc.colnames)])
} else {
data_TTC <- NA
}
# TPI
data_TPI <- (as.numeric(data_TTC) / as.numeric(item_length))
# check to see if TPI is <= cutoff and paste into dataframe if so
data_point <- c()
if(!is.na(data_TPI) & data_TPI <= cutoff){
data_point <- idx
ID <- data[idx,ID.colname]
flag_point <- cbind(ID, data_point)
} else {
next
}
newDF <- rbind(newDF, flag_point)
}
newDF <- as.data.frame(newDF)
colnames(newDF)[2] <- "Index_of_Flagged_Assessment"
newDF
}
# Notes:
#
# The ttc.colnames can have 1 (if your data already has a assessment completion time with the data point),
# or 2 (if a start date and end date are provided in data). The order for the ttc.colnames MUST be start date, then end date.
#
# If response to items was MANDATORY, then mandatory.response = TRUE. Number of items should be provided through variable: number.items.
# item.colnames does not need to be provided when mandatory.response = TRUE.
#
# If response to items was NOT MANDATORY, then mandatory.response = FALSE. The column names of the items should be provided by variable: item.colnames.
# number.items does not need to be provided when mandatory.response = FALSE
#
# The column name of the Participant ID or assessment identification number should be provided with variable: ID.colname.
#
# The commented out code below serves as an example.
# TPI.cutoff(Data1, cutoff = 2, ttc.colnames = c("StartDate", "EndDate") , mandatory.response = FALSE, item.colnames = colnames(Data1[,7:21]), ID.colname = colnames(Data1[,1]))
# TPI.cutoff(Data2, cutoff = 10, ttc.colnames = c("CompletionTime") , mandatory.response = TRUE, number.items = 8, ID.colname = "ID")
#########################################################
########## SD.cutoff function##########################
# This function will flag all the assessments that have are identified by the cutoff
# specified by the user. This function will generate a vector of indices for the
# dataframe provided by the user. This will NOT provide a list of how many times a subject is flagged for this cutoff.
SD.cutoff <- function(data, cutoff, item.colnames, ID.colname){
if (missing(item.colnames)){
item.colnames <- stop("item.colnames missing.\nSpecify number of items with item.colnames\n")
}
if (missing(ID.colname)){
ID.colname <- stop("ID.colname missing.\nSpecify column name of ID with ID.colname\n")
}
if (missing(cutoff)){
warning("cutoff missing.\nCutoff value automatically set to 5. Specify cutoff value to change from 5 with variable 'cutoff = ' \n")
cutoff <- 5
}
newDF <- c()
suppressWarnings(
for(idx in 1:nrow(data)){
# calculate SD
data_SD <- c()
if (length(item.colnames[!is.na(item.colnames)]) == 0){
data_SD <- NA
} else {
data_SD <- suppressWarnings(sd(data[idx,item.colnames], na.rm = TRUE))
}
# check to see if TPI is <= cutoff and paste into dataframe if so
data_point <- c()
flag_point <- c()
if(data_SD <= cutoff){
data_point <- idx
ID <- data[idx,ID.colname]
flag_point <- cbind(ID, data_point)
}else{
next
}
newDF <- rbind(newDF, flag_point)
}
)
newDF <- as.data.frame(newDF)
colnames(newDF)[2] <- "Index_of_Flagged_Assessment"
newDF
}
# Notes:
#
# Column names of all items to be included in the analysis should be provided by the variable: item.colnames.
#
# The column name of the Participant ID or assessment identification number should be provided with variable: ID.colname.
#
# The commented out code below serves as an example.
# SD.cutoff(Data1, cutoff = 5, item.colnames = colnames(Data1[,7:21]), ID.colname = colnames(Data1[,1]))
# SD.cutoff(Data2, cutoff = 10, item.colnames = c("Upset", "Excited", "Irritable", "Content", "Attentive", "Stressed", "Relaxed", "Anxious"), ID.colname = "ID")
#########################################################
########## Combined.cutoff function##########################
# This is going to identify Assessments and IDs that are flagged by both the TPI and SD cutoffs.
# This will pull all IDs and Indices for the corresponding flagged assessments. This does NOT compute
# percentages of Assessments. That is the next function.
Combined.cutoff <- function(data, SD.cutoff, TPI.cutoff, ttc.colnames, number.items, mandatory.response, item.colnames, ID.colname){
if (missing(TPI.cutoff)){
warning("TPI.cutoff missing.\nTPI cutoff value automatically set to 2 seconds. Specify TPI cutoff value to change from 2 seconds with variable 'TPI.cutoff = ' \n")
TPI.cutoff <- 2
}
if (missing(SD.cutoff)){
warning("SD.cutoff missing.\nSD cutoff value automatically set to 5. Specify SD cutoff value to change from 5 with variable 'SD.cutoff = ' \n")
SD.cutoff <- 5
}
if (missing(ttc.colnames)){
ttc.colnames <- stop("ttc.colnames missing. Specify Start-Date & End-Date / Completion Time with ttc.colnames\n")
}
if (isTRUE(mandatory.response) & missing(number.items)){
number.items <- stop("number.items missing. Specify number of items with number.items\n")
}
if (missing(mandatory.response)){
mandatory.response <- stop("mandatory.response missing.\nSpecify if response to all items was mandatory, resulting in with no NAs for item responses.
\nIf responses are mandatory, mandatory.response = TRUE \nIf responses are NOT mandatory, mandatory.response = FALSE")
}
if (isFALSE(mandatory.response) & missing(item.colnames)){
item.colnames <- stop("item.colnames missing.\nSpecify column names of items with item.colnames\n")
}
newDF <- c()
for(idx in 1:nrow(data)){
# number of items if response to items not mandatory
if(isFALSE(mandatory.response)){
item_length <- length(which(!is.na(data[idx, item.colnames])))
} else {
item_length <- number.items
}
# calc TTC for the assessment
data_TTC <- c()
if (length(ttc.colnames[!is.na(ttc.colnames)]) > 1){
starttime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[1])])
endtime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[2])])
data_TTC <- as.numeric(endtime-starttime, units = "secs")
} else if (length(ttc.colnames[!is.na(ttc.colnames)]) == 1){
data_TTC <- (ttc.colnames[!is.na(ttc.colnames)])
} else {
data_TTC <- NA
}
# TPI
data_TPI <- (as.numeric(data_TTC) / as.numeric(item_length))
# SD
data_SD <- c()
if (length(item.colnames[!is.na(item.colnames)]) == 0){
data_SD <- NA
} else {
data_SD <- suppressWarnings(sd(data[idx,item.colnames], na.rm = TRUE))
}
# check to see if TPI is <= cutoff and paste into dataframe if so
data_point <- c()
flag_point <- c()
if(!is.na(data_TPI) & !is.na(data_SD) & data_TPI <= TPI.cutoff & data_SD <= SD.cutoff){
data_point <- idx
ID <- data[idx,ID.colname]
flag_point <- cbind(ID, data_point)
}
newDF <- rbind(newDF, flag_point)
}
newDF <- as.data.frame(newDF)
colnames(newDF)[2] <- "Index_of_Flagged_Assessment"
newDF
}
# Notes:
#
# The ttc.colnames can have 1 (if your data already has a assessment completion time with the data point),
# or 2 (if a start date and end date are provided in data). The order for the ttc.colnames MUST be start date, then end date.
#
# If response to items was MANDATORY, then mandatory.response = TRUE. Number of items should be provided through variable: number.items.
# item.colnames does not need to be provided when mandatory.response = TRUE.
#
# If response to items was NOT MANDATORY, then mandatory.response = FALSE. The column names of the items should be provided by variable: item.colnames.
# number.items does not need to be provided when mandatory.response = FALSE
#
# Specify Cutoff values for SD by SD.cutoff.
# Specify Cutoff values for Time Per Item by TPI.cutoff.
#
# The column name of the Participant ID or assessment identification number should be provided with variable: ID.colname.
#
# The commented out code below serves as an example.
# Combined.cutoff(Data1, SD.cutoff = 5, TPI.cutoff = 2, ttc.colnames = c("StartDate", "EndDate"), mandatory.response = TRUE,
# number.items = 10, ID.colname = colnames(Data1[,1]))
# Combined.cutoff(Data2, SD.cutoff = 10, TPI.cutoff = 5, ttc.colnames = c("CompletionTime"), mandatory.response = FALSE,
# item.colnames = c("Upset", "Excited", "Irritable", "Content", "Attentive", "Stressed", "Relaxed", "Anxious"), ID.colname = "ID")
#########################################################
########## Combined.cutoff.percent function##########################
# This is going to produce a dataframe of percentage of assessments that are flagged by the combined cutoffs.
Combined.cutoff.percent <- function(data, SD.cutoff, TPI.cutoff, ttc.colnames, number.items, mandatory.response, item.colnames, ID.colname){
if (missing(TPI.cutoff)){
warning("TPI.cutoff missing.\nTPI cutoff value automatically set to 2 seconds. Specify TPI cutoff value to change from 2 seconds with variable 'TPI.cutoff = ' \n")
TPI.cutoff <- 2
}
if (missing(SD.cutoff)){
warning("SD.cutoff missing.\nSD cutoff value automatically set to 5. Specify SD cutoff value to change from 5 with variable 'SD.cutoff = ' \n")
SD.cutoff <- 5
}
if (missing(ttc.colnames)){
ttc.colnames <- stop("ttc.colnames missing. Specify Start-Date & End-Date / Completion Time with ttc.colnames\n")
}
if (isTRUE(mandatory.response) & missing(number.items)){
number.items <- stop("number.items missing. Specify number of items with number.items\n")
}
if (missing(mandatory.response)){
mandatory.response <- stop("mandatory.response missing.\nSpecify if response to all items was mandatory, resulting in with no NAs for item responses.
\nIf responses are mandatory, mandatory.response = TRUE \nIf responses are NOT mandatory, mandatory.response = FALSE")
}
if (isFALSE(mandatory.response) & missing(item.colnames)){
item.colnames <- stop("item.colnames missing.\nSpecify column names of items with item.colnames\n")
}
newDF <- c()
for(idx in 1:nrow(data)){
# number of items if response to items not mandatory
if(isFALSE(mandatory.response)){
item_length <- length(which(!is.na(data[idx, item.colnames])))
} else {
item_length <- number.items
}
# calc TTC for the assessment
data_TTC <- c()
if (length(ttc.colnames[!is.na(ttc.colnames)]) > 1){
starttime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[1])])
endtime <- lubridate::ymd_hms(data[idx, which(colnames(data) %in% ttc.colnames[2])])
data_TTC <- as.numeric(endtime-starttime, units = "secs")
} else if (length(ttc.colnames[!is.na(ttc.colnames)]) == 1){
data_TTC <- (ttc.colnames[!is.na(ttc.colnames)])
} else {
data_TTC <- NA
}
# TPI
data_TPI <- (as.numeric(data_TTC) / as.numeric(item_length))
# SD
data_SD <- c()
if (length(item.colnames[!is.na(item.colnames)]) == 0){
data_SD <- NA
} else {
data_SD <- suppressWarnings(sd(data[idx,item.colnames], na.rm = TRUE))
}
# check to see if TPI is <= cutoff and paste into dataframe if so
data_point <- c()
flag_point <- c()
if(!is.na(data_TPI) & !is.na(data_SD) & data_TPI <= TPI.cutoff & data_SD <= SD.cutoff){
data_point <- idx
ID <- data[idx,ID.colname]
flag_point <- cbind(ID, data_point)
}
newDF <- rbind(newDF, flag_point)
}
newDF <- as.data.frame(newDF)
colnames(newDF)[1:2] <- c("ID", "Index_of_Flagged_Assessment")
newDF2 <- cbind(ID = rep(NA,length(unique(newDF$ID))), Percent_Flagged = rep(NA,length(unique(newDF$ID))))
uniqueID1 <- unique(newDF$ID)
for(idx in 1:length(uniqueID1)){
newDF2[idx,1] <- uniqueID1[idx]
newDF2[idx,2] <- ((length(which(newDF$ID == uniqueID1[idx])) / length(which(data[,ID.colname] == uniqueID1[idx])) ) * 100)
}
newDF2 <- as.data.frame(newDF2)
newDF2 <- na.omit(newDF2)
newDF2
}
# Notes:
#
# The ttc.colnames can have 1 (if your data already has a assessment completion time with the data point),
# or 2 (if a start date and end date are provided in data). The order for the ttc.colnames MUST be start date, then end date.
#
# If response to items was MANDATORY, then mandatory.response = TRUE. Number of items should be provided through variable: number.items.
# item.colnames does not need to be provided when mandatory.response = TRUE.
#
# If response to items was NOT MANDATORY, then mandatory.response = FALSE. The column names of the items should be provided by variable: item.colnames.
# number.items does not need to be provided when mandatory.response = FALSE
#
# Specify Cutoff values for SD by SD.cutoff.
# Specify Cutoff values for Time Per Item by TPI.cutoff.
#
# The column name of the Participant ID or assessment identification number should be provided with variable: ID.colname.
#
# The commented out code below serves as an example.
# Combined.cutoff.percent(Data1, SD.cutoff = 5, TPI.cutoff = 2, ttc.colnames = c("StartDate", "EndDate"), mandatory.response = TRUE,
# number.items = 10, ID.colname = colnames(Data1[,1]))
# Combined.cutoff.percent(Data2, SD.cutoff = 10, TPI.cutoff = 5, ttc.colnames = c("CompletionTime"), mandatory.response = FALSE,
# item.colnames = c("Upset", "Excited", "Irritable", "Content", "Attentive", "Stressed", "Relaxed", "Anxious"), ID.colname = "ID")