-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVived.fxns.r
1040 lines (936 loc) · 33.9 KB
/
Vived.fxns.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
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##############################################################
# Description: Functions required to run VIVED.
# Usage: source(Vived.fxns.r)
# Author: Haley Hunter-Zinck
# Date: September 29, 2016
# Dependencies: ggplot2, Vived.param.r,
# Vived.calc.fxns.r, Vived.plot.fxns.r,
# Vived.read.fxns.r, Vived.report.fxns.r
##############################################################
# packages
source("Vived.param.r")
source("Vived.calc.fxns.r")
source("Vived.plot.fxns.r")
source("Vived.read.fxns.r")
# for testing
testing=0
if(testing==1)
{
efile="../data/1-3.edis.csv"
dateRange=c("3001-01-01","3001-01-31")
metric=OCC_DISP
agg=HIST
md="All"
disp="All"
acuity="All"
sHour="00"
eHour="23"
sMin="00"
eMin="59"
dow=c("Sun","Mon","Tue","Wed","Thu","Fri","Sat")
#dow=c("Mon","Tue","Wed","Thu","Fri")
#dow=c("Mon")
excludeHolidays=FALSE
}
######################################################
# MAIN FUNCTIONS #
#####################################################
# plot according to criteria
createPlot=function(raw, dateRange, metric, agg, md, dow, disp, acuity,
sHour, eHour, sMin, eMin, excludeHolidays)
{
if(DEBUG)
{
cat("startDate=",as.character(dateRange[1]),"\n",sep="")
cat("endDate=",as.character(dateRange[2]),"\n",sep="")
}
# check input
if(!is.na(as.character(dateRange[1])))
{
# format input
startDate=as.POSIXct(as.character(dateRange[1]),tz=TZ)
endDate=as.POSIXct(as.character(dateRange[2]),tz=TZ)+24*3600-1
msg=illegal(startDate, endDate, metric, agg, disp)
if(msg!="")
{
return(plotInvalid(msg))
}
}
# read data and process for validity
#raw=readVisitData(efile)
if(length(raw$ti==1) && is.na(raw$ti[1]))
{
msg="Data file lacks required column(s) named \"PatientID\", \"TimeIn\", and/or \"TimeOut\". Please check data file format."
return(plotInvalid(msg))
}
# globally assign color maps for acuity and disposition
uAcuity=getAcuity(raw)
uDisp=getDispositions(raw)
COLOR_ACUITY<<-assignColorFromPal(PAL_ACUITY,PAL_ACUITY_BACKUP,uAcuity)
COLOR_DISP<<-assignColorFromPal(PAL_DISP,PAL_DISP_BACKUP,uDisp)
# preprocess for validity
preproc=preprocess(raw)
# filter data according to metric
filtered=filter(preproc, startDate, endDate, md, dow, disp, acuity,
excludeHolidays, metric=metric, sHourNum=as.double(sHour),
eHourNum=as.double(eHour), sMinNum=as.double(sMin), eMinNum=as.double(eMin))
# generate plot
locName=NA
plotted=plotMetric(preproc,filtered, locName, metric, agg, startDate, endDate, dow, md, disp,
sHour, eHour, sMin, eMin, excludeHolidays)
# default file name downloads
plotted$fileNamePlot=defaultFileName(dateRange, metric, agg, "plot")
plotted$fileNameTable=defaultFileName(dateRange, metric, agg, "table")
return(plotted)
}
# get list of aggregate views for currently selected metric
getViews=function(metric)
{
# by day
if(is.element(metric,METRICS$Day) || is.element(metric,METRICS$DayPercent))
{
return(AGG$AGG_DAY)
}
# by visit
if(is.element(metric,METRICS$Visit))
{
return(AGG$AGG_VISIT)
}
# by minute
if(metric==OCC)
{
return(AGG$AGG_MIN)
}
# by minute but subdivided into subgroups
if(metric==OCC_BY)
{
return(AGG$AGG_OCC_BY)
}
# ED census subdivided into subgroups
if(is.element(metric,METRICS_OCC_SPLIT))
{
return(AGG$AGG_OCC_SPLIT)
}
# default: return all
return(AGG$AGG_ALL)
}
# preproces raw data
preprocess=function(obj)
{
preproc=obj
# filtering by validity
toRemove=which(is.na(preproc$ti) | is.na(preproc$to) | preproc$ti>=preproc$to)
toRemove=unique(toRemove)
if(length(toRemove)>0)
{
preproc=updateObj(preproc,-toRemove)
}
# make sure we have no missing timestamps
labels=names(INTERVAL_MAX)
for(i in (length(labels)-1):1)
{
nIndeces=NA
if(is.na(preproc[[labels[i]]][1]))
{
preproc[[labels[i]]]=preproc[[labels[i+1]]]
} else
{
nIndeces=which(is.na(preproc[[labels[i]]]))
preproc[[labels[i]]][nIndeces]=preproc[[labels[i+1]]][nIndeces]
}
if(DEBUG)
{
if(length(nIndeces)==1 && is.na(nIndeces))
{
cat("All ",labels[i]," are missing.\n",sep="")
} else
{
perc=round(length(nIndeces)/length(preproc[[labels[i]]])*100,2)
cat("Number of missing ",labels[i]," filled in: ",
length(nIndeces)," / ",length(preproc[[labels[i]]]),
" (",perc,"%)","\n",sep="")
}
}
}
# make sure progression of time stamps is consistent
labels=names(INTERVAL_MAX)
for(i in (length(labels)-1):1)
{
# get difference between timestamps in each interval
diff=as.double(preproc[[labels[i+1]]])-as.double(preproc[[labels[i]]])
# if timestamp is out of order, set it equal to the next timestamp in the progression
wIndeces=which(diff<0)
preproc[[labels[i]]][wIndeces]=preproc[[labels[i+1]]][wIndeces]
diff=as.double(preproc[[labels[i+1]]])-as.double(preproc[[labels[i]]])
if(DEBUG)
{
perc=round(length(wIndeces)/length(preproc[[labels[i]]])*100,2)
cat("Number of inconsistent ",labels[i]," modified: ",
length(wIndeces)," / ",length(preproc[[labels[i]]]),
" (",perc,"%)","\n",sep="")
cat("Number of inconsistent labels:",length(which(diff<0)),"\n",sep="")
}
}
# make sure td is consistent as well
preproc$td=preproc$tdisp
preproc$td[which(!is.element(preproc$disp,DISP_ADMIT))]=NA
# debug
if(DEBUG)
{
cat("EDIS visits removed during preprocessing: ",length(obj$ti)-length(preproc$ti)," / ",length(obj$ti)," visits.\n",sep="")
}
return(preproc)
}
# filter functions
filter=function(obj, start, end, md, dow, disp, acuity, excludeHolidays, metric="", sHourNum=NA, eHourNum=NA, sMinNum=NA, eMinNum=NA)
{
# filter by date range
if(metric==OCC || is.element(metric,METRICS_OCC_SPLIT))
{
# keep all overlapping intervals for occupancy metrics
indeces=which(!(obj$to<start | obj$ti>end))
} else
{
# filter by start time of visit for other metrics
indeces=which(obj$ti>=start & obj$ti<=end)
}
fil=updateObj(obj,indeces)
# filter by day of the week and hour of the day (if applicable)
if(is.element(metric, METRICS$Visit))
{
indeces=which(is.element(format(fil$ti,"%a"),dow)
& is.element(format(fil$ti,"%H:%M"),getMinutes(sHourNum, eHourNum, sMinNum, eMinNum)))
fil=updateObj(fil,indeces)
} else if(length(dow)<length(LABEL_DOW) & !(metric==OCC || is.element(metric,METRICS_OCC_SPLIT)))
{
visitDow=as.double(format(fil$ti,"%w"))
dowNum=match(dow,LABEL_DOW)-1
iDow=is.element(visitDow, dowNum)
fil=updateObj(fil,iDow)
}
# exclude holidays
if(excludeHolidays & !(metric==OCC || is.element(metric,METRICS_OCC_SPLIT)))
{
holidays=getFedHolidays(start,end)
visitDate=as.character(format(fil$ti,"%Y-%m-%d"))
iHoliday=which(is.element(visitDate, holidays))
if(length(iHoliday)>0)
{
fil=updateObj(fil,-iHoliday)
}
}
# filter by provider
if(md!=ALL_MD)
{
iMd=which(fil$md==md)
fil=updateObj(fil,iMd)
}
# filter by disposition
if(disp!=ALL_DISP)
{
visitAdmit=which(!is.na(fil$td))
if(disp==ALL_ADMIT)
{
fil=updateObj(fil, visitAdmit)
} else if(disp==ALL_NOT_ADMIT)
{
fil=updateObj(fil, -visitAdmit)
} else
{
fil=updateObj(fil, which(fil$disp==disp))
}
}
# filter by acuity
if(acuity!=ALL_ACUITY)
{
aIndeces=which(fil$acuity==acuity)
fil=updateObj(fil,aIndeces)
}
# debug
if(DEBUG)
{
cat("EDIS visits removed during filtering: ",length(obj$ti)-length(fil$ti)," / ",length(obj$ti)," visits.\n",sep="")
}
return(fil)
}
# calculate the metric of interest on the filtered data
plotMetric=function(preproc,obj, locName, metric, agg, start, end, dow, md, disp,
sHour, eHour, sMin, eMin, excludeHolidays)
{
# initialize
myPlot=NULL
msg=list()
myTable=NA
params=getPlottingParameters(locName, metric, obj)
# format input
sHourNum=as.double(sHour)
eHourNum=as.double(eHour)
sMinNum=as.double(sMin)
eMinNum=as.double(eMin)
# holidays
if(excludeHolidays)
{
holidays=getFedHolidays(start,end)
} else
{
holidays=c()
}
# store appropriate plot for metric - aggregation pair
if(is.element(metric,METRICS$DayPercent) || is.element(metric,METRICS$Day))
{
# day metric
nday=ceiling(hours(start,end)/24)
if(metric==NVISIT)
{
# nvisit
dayMetric=calcNumVisit(obj,start, nday, dow, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
} else if(metric==PVISIT)
{
# percentage of visits for preprocessed and filtered
dayMetricFil=calcNumVisit(obj,start, nday, dow, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
# calculate percentage over vector or matrix
if(agg!=HOUR)
{
# get denominator
dayMetricTotal=calcNumVisit(preproc,start, nday, LABEL_DOW, 0, 23, 0, 59, holidays, agg=agg)
# align and divide to get percentage and account for dividing by 0
indeces=match(names(dayMetricFil),names(dayMetricTotal))
dayMetric=dayMetricFil/dayMetricTotal[indeces]*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
names(dayMetric)=names(dayMetricFil)
} else
{
# get denominator (without hours)
dayMetricTotal=calcNumVisit(preproc,start, nday, LABEL_DOW, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
# align and divide to get percentage and account for dividing by 0
indeces=match(rownames(dayMetricFil),rownames(dayMetricTotal))
dayMetric=dayMetricFil/dayMetricTotal[indeces,]*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
rownames(dayMetric)=rownames(dayMetricFil)
}
} else if(metric==PBOARD_4PLUS)
{
# get indeces of visits that boarded > 4 hours
indeces=which(as.double(obj$to-obj$td)>4*3600)
# percentage of visits for preprocessed and filtered
dayMetricSubset=calcNumVisit(updateObj(obj,indeces),start, nday, dow, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
# calculate percentage over vector or matrix
if(agg!=HOUR)
{
# get denomintor
dayMetricFil=calcNumVisit(obj,start, nday, LABEL_DOW, 0, 23, 0, 59, holidays, agg=agg)
# align and divide to get percentage and account for dividing by 0 for vector
indeces=match(names(dayMetricFil),names(dayMetricSubset))
dayMetric=dayMetricSubset[indeces]/dayMetricFil*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
names(dayMetric)=names(dayMetricFil)
} else
{
# get denominator
dayMetricFil=calcNumVisit(obj,start, nday, LABEL_DOW, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
# align and divide to get percentage and account for dividing by 0 for matrix
indeces=match(rownames(dayMetricFil),rownames(dayMetricSubset))
dayMetric=dayMetricSubset[indeces,]/dayMetricFil*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
rownames(dayMetric)=rownames(dayMetricFil)
}
} else if(metric==NBOARD_4PLUS)
{
# get indeces of visits that boarded > 4 hours
indeces=which(as.double(obj$to-obj$td)>4*3600)
# number of visits boarded > 4 hours
dayMetric=calcNumVisit(updateObj(obj,indeces),start, nday, dow, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
} else if(metric==PRETURN72)
{
# get only the returned with the same filters
filForReturn=filter(preproc, start, end+RETURN_HOUR_MAX*3600, md=ALL_MD, dow=LABEL_DOW,
disp=ALL_DISP, acuity=ALL_ACUITY, excludeHolidays=FALSE)
iIndeces=returnedIndex(filForReturn, RETURN_HOUR_MAX, returnInitial=TRUE, rAdmit=FALSE)
objRet=filter(updateObj(filForReturn,iIndeces), start, end, md=md, dow=dow,
disp=disp, acuity=ALL_ACUITY, excludeHolidays=excludeHolidays)
# calculate metrics for numerator and denominator
dayMetricRet=calcNumVisit(objRet,start, nday, LABEL_DOW, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
dayMetricFil=calcNumVisit(obj,start, nday, LABEL_DOW, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
# calculate percentage over vector or matrix
if(agg!=HOUR)
{
# align and divide to get percentage and account for dividing by 0 for vector
indeces=match(names(dayMetricFil),names(dayMetricRet))
dayMetric=dayMetricRet[indeces]/dayMetricFil*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
names(dayMetric)=names(dayMetricFil)
} else
{
# align and divide to get percentage and account for dividing by 0 for matrix
cIndeces=match(colnames(dayMetricFil),colnames(dayMetricRet))
rIndeces=match(rownames(dayMetricFil),rownames(dayMetricRet))
dayMetric=dayMetricRet[rIndeces,cIndeces]/dayMetricFil*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
colnames(dayMetric)=colnames(dayMetricFil)
}
} else if(metric==NRETURN72)
{
# get only the returned with the same filters
filForReturn=filter(preproc, start, end+7*24*3600, md="All", dow=LABEL_DOW, disp="All", acuity=ALL_ACUITY, excludeHolidays=FALSE)
iIndeces=returnedIndex(filForReturn,RETURN_HOUR_MAX,returnInitial=TRUE, rAdmit=FALSE)
objRet=filter(updateObj(filForReturn,iIndeces), start, end, md, dow, disp, acuity=ALL_ACUITY, excludeHolidays)
# nvisit that are quick returns
dayMetric=calcNumVisit(objRet,start, nday, dow, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
} else if(metric==NRETURN72ADMIT)
{
# get only the returned with the same filters
filForReturn=filter(preproc, start, end+7*24*3600, md="All", dow=LABEL_DOW, disp="All", acuity=ALL_ACUITY, excludeHolidays=FALSE)
iIndeces=returnedIndex(filForReturn,RETURN_HOUR_MAX,returnInitial=TRUE, rAdmit=TRUE)
objRet=filter(updateObj(filForReturn,iIndeces), start, end, md, dow, disp, acuity=ALL_ACUITY, excludeHolidays)
# nvisit that are quick returns
dayMetric=calcNumVisit(objRet,start, nday, dow, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
} else if(metric==PRETURN72ADMIT)
{
# get only the returned with the same filters
filForReturn=filter(preproc, start, end+7*24*3600, md="All", dow=LABEL_DOW, disp="All", acuity=ALL_ACUITY, excludeHolidays=FALSE)
iIndeces=returnedIndex(filForReturn,RETURN_HOUR_MAX,returnInitial=TRUE, rAdmit=TRUE)
objRet=filter(updateObj(filForReturn,iIndeces), start, end, md, dow, disp, acuity=ALL_ACUITY, excludeHolidays)
# calculate metrics for numerator and denominator
dayMetricRet=calcNumVisit(objRet,start, nday, LABEL_DOW, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
dayMetricFil=calcNumVisit(obj,start, nday, LABEL_DOW, sHourNum, eHourNum, sMinNum, eMinNum, holidays, agg=agg)
# calculate percentage over vector or matrix
if(agg!=HOUR)
{
# align and divide to get percentage and account for dividing by 0 for vector
indeces=match(names(dayMetricFil),names(dayMetricRet))
dayMetric=dayMetricRet[indeces]/dayMetricFil*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
names(dayMetric)=names(dayMetricFil)
} else
{
# align and divide to get percentage and account for dividing by 0 for matrix
cIndeces=match(colnames(dayMetricFil),colnames(dayMetricRet))
rIndeces=match(rownames(dayMetricFil),rownames(dayMetricRet))
dayMetric=dayMetricRet[rIndeces,cIndeces]/dayMetricFil*100
dayMetric[which(dayMetric==Inf | dayMetric==-Inf)]=NA
colnames(dayMetric)=colnames(dayMetricFil)
}
}
msg[[length(msg)+1]]=paste(" Number of visits analyzed = ",length(which(!is.na(obj$ti)))," / ",length(obj$ti),sep="")
# check for no days
if(length(dayMetric)==0)
{
return(plotInvalid("No valid days to plot after filtering"))
}
# plot day metric
if(agg==HIST)
{
# day metric, hist
myPlot=plotHistogram(dayMetric, metric)
myTable=makeTable(dayMetric,agg)
} else if(agg==YM)
{
# day metric, ym
yearMonth=format(as.POSIXlt(names(dayMetric),tz=TZ),"%Y-%m")
myPlot=plotBoxplot(dayMetric, yearMonth, metric)
myTable=makeTable(dayMetric,agg,yearMonth)
} else if(agg==DATE)
{
# day metric, date
yearMonthDay=format(as.POSIXlt(names(dayMetric),tz=TZ),"%Y-%m-%d")
myPlot=plotLine(vec=dayMetric, labels=yearMonthDay, ylab=metric)
myTable=makeTable(dayMetric,agg,yearMonthDay)
} else if(agg==YEAR)
{
# day metric, year
date=format(as.POSIXlt(names(dayMetric),tz=TZ),"%Y-%m-%d",tz=TZ)
year=factor(format(as.POSIXlt(date,tz=TZ),"%Y"))
myPlot=plotBoxplot(dayMetric, year, metric)
myTable=makeTable(dayMetric,agg,year)
} else if(agg==MONTH)
{
# day metric, month
date=format(as.POSIXlt(names(dayMetric),tz=TZ),"%Y-%m-%d")
month=as.double(format(as.POSIXlt(date,tz=TZ),"%m"))
month=factor(mapLabel(month, c(1:12), LABEL_MONTH), levels=LABEL_MONTH)
myPlot=plotBoxplot(dayMetric, month, metric)
myTable=makeTable(dayMetric,agg,month)
} else if(agg==DOW)
{
# day metric, dow
date=format(as.POSIXlt(names(dayMetric),tz=TZ),"%Y-%m-%d")
dow=format(as.POSIXlt(date,tz=TZ),"%w")
dow=factor(mapLabel(dow, c(0:6), LABEL_DOW), levels=LABEL_DOW)
myPlot=plotBoxplot(dayMetric, dow, metric)
myTable=makeTable(dayMetric,agg,dow)
} else if(agg==HOUR)
{
# add missing hours
missingHours=setdiff(getHours(sHourNum, eHourNum),as.double(colnames(dayMetric)))
if(length(missingHours)>0)
{
aIndeces=(ncol(dayMetric)+1):(ncol(dayMetric)+length(missingHours))
tmat=matrix(c(dayMetric,rep(0,length(missingHours))),nrow=nrow(dayMetric),ncol=ncol(dayMetric)+length(missingHours))
colnames(tmat)=c(colnames(dayMetric),missingHours)
rownames(tmat)=rownames(dayMetric)
dayMetric=tmat
}
# day metric, hour (arrivals by hour)
matHour=c()
for(i in 1:ncol(dayMetric))
{
matHour=append(matHour,rep(colnames(dayMetric)[i],nrow(dayMetric)))
}
hour=factor(mapLabel(matHour, c(0:23), LABEL_HOUR), levels=LABEL_HOUR)
# get plot and table
myPlot=plotBoxplot(c(dayMetric), hour, metric)
myTable=makeTable(c(dayMetric),agg,hour)
} else if(agg==BY_ACUITY)
{
# plot day metric by acuity
} else if(agg==WEEK)
{
# day metric, week
week=weekLabel(as.POSIXlt(names(dayMetric),tz=TZ),dowStart=DOW_START)
myPlot=plotBoxplot(dayMetric, week, metric)
myTable=makeTable(dayMetric,agg,week)
}
# update message
msg[[length(msg)+1]]=paste(" Number of days analyzed = ",length(which(!is.na(dayMetric)))," / ",length(dayMetric),sep="")
msg[[length(msg)+1]]=paste(metric," median = ",round(median(dayMetric, na.rm=TRUE),digits=2),sep="")
} else if(is.element(metric,METRICS$Visit))
{
# calculate visit metric
if(metric==LOS)
{
visitMetric=calcTimeIntervals(obj$ti,obj$to,LOS_MAX)
} else if(metric==WAIT_IT)
{
visitMetric=calcTimeIntervals(obj$ti,obj$tt,TRIAGE_MAX)
} else if(metric==WAIT_IS)
{
visitMetric=calcTimeIntervals(obj$ti,obj$ts,SEEN_MAX)
} else if(metric==WAIT_ID)
{
visitMetric=calcTimeIntervals(obj$ti,obj$tdisp,DISP_MAX)
} else if(metric==BOARD_DO)
{
visitMetric=calcTimeIntervals(obj$tdisp,obj$to,BOARD_MAX)
names(visitMetric)=obj$ti
}
# update message
msg[[length(msg)+1]]=paste(" Number of visits analyzed = ",length(which(!is.na(visitMetric)))," / ",length(visitMetric),sep="")
msg[[length(msg)+1]]=paste(metric," median = ",round(median(visitMetric, na.rm=TRUE),digits=2),sep="")
# check for no valid visits
if(length(visitMetric)==0)
{
return(plotInvalid("No valid visits to plot after filtering"))
}
# plot visit metric
if(agg==HIST)
{
# visit metric, hist
myPlot=plotHistogram(visitMetric, metric)
myTable=makeTable(visitMetric,agg)
} else if(agg==YM)
{
# visit metric, ym
visYearMonth=format(as.POSIXlt(names(visitMetric),tz=TZ),"%Y-%m")
myPlot=plotBoxplot(visitMetric, visYearMonth, metric)
myTable=makeTable(visitMetric,agg,visYearMonth)
} else if(agg==DATE)
{
# visit metric, date
visYearMonthDay=format(as.POSIXlt(names(visitMetric),tz=TZ),"%Y-%m-%d")
myPlot=plotBoxplot(visitMetric, visYearMonthDay, metric)
myTable=makeTable(visitMetric,agg,visYearMonthDay)
} else if(agg==YEAR)
{
# visit metric, year
visYear=factor(format(as.POSIXlt(names(visitMetric),tz=TZ),"%Y"))
myPlot=plotBoxplot(visitMetric, visYear, metric)
myTable=makeTable(visitMetric,agg,visYear)
} else if(agg==MONTH)
{
# visit metric, month
visMonth=format(as.POSIXlt(names(visitMetric),tz=TZ),"%m")
month=factor(mapLabel(as.double(visMonth), c(1:12), LABEL_MONTH), levels=LABEL_MONTH)
myPlot=plotBoxplot(visitMetric, month, metric)
myTable=makeTable(visitMetric,agg,month)
} else if(agg==DOW)
{
# visit metric, dow
visDayOfWeek=format(as.POSIXlt(names(visitMetric),tz=TZ),"%w")
dow=factor(mapLabel(visDayOfWeek, c(0:6), LABEL_DOW), levels=LABEL_DOW)
myPlot=plotBoxplot(visitMetric, dow, metric)
myTable=makeTable(visitMetric,agg,dow)
} else if(agg==HOUR)
{
# visit metric, hour
visHour=as.character(as.double(format(as.POSIXlt(names(visitMetric),tz=TZ),"%H")))
hour=factor(mapLabel(visHour, as.character(c(0:23)), LABEL_HOUR), levels=LABEL_HOUR)
myPlot=plotBoxplot(visitMetric, hour, metric)
myTable=makeTable(visitMetric,agg,hour)
} else if(agg==BY_ACUITY)
{
# visit metric, acuity
indeces=match(obj$acuity,names(MAP_ACUITY))
visAcuity=factor(MAP_ACUITY[indeces],levels=as.character(MAP_ACUITY))
#visAcuity=factor(MAP_ACUITY[obj$acuity],levels=as.character(MAP_ACUITY))
myPlot=plotBoxplot(visitMetric, visAcuity, metric)
myTable=makeTable(visitMetric, agg, visAcuity)
} else if(agg==BY_DISP)
{
# visit metric, acuity
visDisp=obj$disp
visDisp[which(is.na(visDisp))]=obj$disp[which(is.na(visDisp))]
visDisp=factor(visDisp)
myPlot=plotBoxplot(visitMetric, visDisp, metric)
myTable=makeTable(visitMetric, agg, visDisp)
} else if(agg==BY_PROV)
{
# visit metric, acuity
visProv=factor(obj$md)
myPlot=plotBoxplot(visitMetric, visProv, metric)
myTable=makeTable(visitMetric, agg, visProv)
} else if(agg==WEEK)
{
# visit metric, week
week=weekLabel(as.POSIXlt(names(visitMetric),tz=TZ),dowStart=DOW_START)
myPlot=plotBoxplot(visitMetric, week, metric)
myTable=makeTable(visitMetric,agg,week)
}
} else if(metric==OCC)
{
# occupancy per minute
occ=calcOcc(obj$ti, obj$to, start, end)
occTime=start+c(0:(length(occ)-1))*60
# remove dow if date range is less than a week
dow=intersect(dow,names(table(format(occTime,"%a"))))
# filter by day of the week if necessary
if(length(dow)<length(LABEL_DOW))
{
dowNum=match(dow,LABEL_DOW)-1
indeces=which(is.element(format(occTime,"%w"),dowNum))
occ=occ[indeces]
occTime=occTime[indeces]
}
# filter by holidays if necessary
if(excludeHolidays)
{
indeces=which(is.element(format(occTime,"%Y-%m-%d"),holidays))
if(length(indeces)>0)
{
occ=occ[-indeces]
occTime=occTime[-indeces]
}
}
# filter by minute if necessary
myMinutes=getMinutes(sHourNum,eHourNum,sMinNum,eMinNum)
if(agg!=MD)
{
# filter occupancy by hours
indeces=which(is.element(format(occTime,"%H:%M"),myMinutes))
occ=occ[indeces]
occTime=occTime[indeces]
}
# check for no valid occupancy time slots
if(length(occ)==0)
{
return(plotInvalid("No valid minutes to plot after filtering"))
}
if(agg==HIST)
{
# occ, hist
myPlot=plotHistogram(occ, metric)
myTable=makeTable(occ,agg)
} else if(agg==YM)
{
# occ, ym
occYearMonth=format(occTime,"%Y-%m")
myPlot=plotBoxplot(occ, occYearMonth, metric)
myTable=makeTable(occ,agg, occYearMonth)
} else if(agg==DATE)
{
# occ, date
occYearMonthDay=format(occTime,"%Y-%m-%d")
myPlot=plotBoxplot(occ, occYearMonthDay, metric)
myTable=makeTable(occ,agg, occYearMonthDay)
} else if(agg==YEAR)
{
# occ, year
occYear=factor(format(occTime,"%Y"))
myPlot=plotBoxplot(occ, occYear, metric)
myTable=makeTable(occ,agg, occYear)
} else if(agg==MONTH)
{
# occ, month
occMonth=as.double(format(occTime,"%m"))
month=factor(mapLabel(occMonth, c(1:12), LABEL_MONTH), levels=LABEL_MONTH)
myPlot=plotBoxplot(occ, month, metric)
myTable=makeTable(occ,agg, month)
} else if(agg==DOW)
{
# occ, dow
occDayOfWeek=format(occTime,"%w")
dow=factor(mapLabel(occDayOfWeek, c(0:6), LABEL_DOW), levels=LABEL_DOW)
myPlot=plotBoxplot(occ, dow, metric)
myTable=makeTable(occ,agg, dow)
} else if(agg==HOUR)
{
# occ, hour
occHour=as.character(as.double(format(occTime,"%H")))
hour=factor(mapLabel(occHour, as.character(c(0:23)), LABEL_HOUR), levels=LABEL_HOUR)
myPlot=plotBoxplot(occ, hour, metric)
myTable=makeTable(occ,agg, hour)
} else if(agg==WEEK)
{
# occ, date
occWeek=weekLabel(occTime,dowStart=DOW_START)
myPlot=plotBoxplot(occ, occWeek, metric)
myTable=makeTable(occ,agg, occWeek)
} else if(agg==MD)
{
# occ, min x dow
nweek=round(as.double(table(format(occTime,"%w")))/(60*24))
dowNum=match(dow,LABEL_DOW)-1
dowTime=factor(mapLabel(format(occTime,"%w"), dowNum, dow), levels=dow)
minute=factor(as.character(as.double(format(occTime,"%H"))*60+as.double(format(occTime,"%M"))),levels=as.character(c(0:(24*60-1))))
# aggregate by day of the week and minute of the day
df=data.frame(occ=occ, dow=dowTime, minute=minute)
meltMD=aggregate(occ ~ dow + minute, df, FUN=sum)
# setup for multiple lines
#occHourMin=format(occTime,"%H:%M")
matMD=matrix(meltMD$occ/as.double(nweek), nrow=length(dow), ncol=24*60, byrow=FALSE)
colnames(matMD)=as.character(c(0:(24*60-1)))
if(length(which(nweek>1))>0)
{
rownames(matMD)=dow
} else
{
oIndeces=match(as.character(format(occTime[seq(from=1,to=length(occTime),by=24*60)],"%a")),dow)
matMD=matrix(matMD[oIndeces,],nrow=nrow(matMD))
rownames(matMD)=as.character(format(occTime[seq(from=1,to=length(occTime),by=24*60)],"%Y-%m-%d\n%a"))
}
# get minutes to keep based on time of day filter
mIndeces=which(is.element(HOUR_MINUTES,myMinutes))
if(length(mIndeces)<ncol(matMD))
{
matMD[,-mIndeces]=NA
}
# define xlimits
if(sHourNum <= eHourNum)
{
xlimits=c(sHourNum*60+sMinNum, eHourNum*60+eMinNum)
} else
{
xlimits=c(0,24*60)
}
# plot
myPlot=plotMultiLine(matMD, nbed=getNumBed(locName), xlab="", ylab=metric, label=metric, xlimits=xlimits)
# table
myTable=t(rbind(getMinLabel(c(1:ncol(matMD))),matMD))
colnames(myTable)[1]="Minute"
}
} else if(is.element(metric,METRICS_OCC_SPLIT))
{
# get occupancy subset
occ=calcOccSubset(obj,metric,start,end)
occTime=start+c(0:(length(occ[[1]])-1))*60
# remove dow if date range is less than a week
dow=intersect(dow,names(table(format(occTime,"%a"))))
# filter by day of the week if necessary
if(length(dow)<length(LABEL_DOW))
{
dowNum=match(dow,LABEL_DOW)-1
indeces=which(is.element(format(occTime,"%w"),dowNum))
for(i in 1:length(occ))
{
occ[[i]]=occ[[i]][indeces]
}
occTime=occTime[indeces]
}
# filter by holidays if necessary
if(excludeHolidays)
{
indeces=which(is.element(format(occTime,"%Y-%m-%d"),holidays))
if(length(indeces)>0)
{
for(i in 1:length(occ))
{
occ[[i]]=occ[[i]][-indeces]
}
occTime=occTime[-indeces]
}
# remove dow if holiday was the only representation of that day
dow=intersect(dow,names(table(format(occTime,"%a"))))
}
# get minutes to keep based on time of day filter
myMinutes=getMinutes(sHourNum,eHourNum,sMinNum,eMinNum)
mIndeces=which(is.element(HOUR_MINUTES,myMinutes))
if(agg!=MD)
{
# initialize
occDate=format(occTime,"%Y-%m-%d")
occFil=list()
# for each occ, get mean across each day for requested hours
for(i in 1:length(occ))
{
# only retain hours of the day
omat=matrix(occ[[i]],ncol=length(unique(occDate)),byrow=FALSE)
occFil[[i]]=c(omat[mIndeces,])
}
# filter occTime by minutes
dmat=matrix(as.character(occTime),ncol=length(unique(occDate)),byrow=FALSE)
occTimeFil=as.POSIXlt(c(dmat[mIndeces,]),tz=TZ)
# make sure labels are still present
names(occFil)=names(occ)
}
# check for no valid occupancy time slots
if(length(which(unlist(lapply(occ,length))>0))==0)
{
return(plotInvalid("No valid minutes to plot after filtering"))
}
# format and plot split occupancy
if(agg==HIST)
{
# plot occupancy means for each date as a stacked barplot
msg=params$msg
myPlot=plotMultiDensity(occFil, xlab=metric, myParams=params)
# table
myTable=makeTable(occFil,agg)
if(length(names(params$myLevels))>0)
{
myTable[,1]=params$myLevels[myTable[,1]]
}
} else if(agg==YEAR)
{
# get plot and table for aggregation type year
results=edCensusPlotAndTable(occFil=occFil,occAggFil=format(occTimeFil,"%Y"), metric=metric, myParams=params)
myTable=results$myTable
myPlot=results$myPlot
} else if(agg==YM)
{
# get plot and table for aggregation type year
results=edCensusPlotAndTable(occFil=occFil,occAggFil=format(occTimeFil,"%Y-%m"), metric=metric, myParams=params)
myTable=results$myTable
myPlot=results$myPlot
} else if(agg==MONTH)
{
# get plot and table for aggregation type year
results=edCensusPlotAndTable(occFil=occFil,occAggFil=factor(format(occTimeFil,"%b"),levels=LABEL_MONTH), metric=metric, myParams=params)
myTable=results$myTable
myPlot=results$myPlot
} else if(agg==DATE)
{
# get plot and table for aggregation type date
results=edCensusPlotAndTable(occFil=occFil,occAggFil=format(occTimeFil,"%Y-%m-%d"), metric=metric, myParams=params)
myTable=results$myTable
myPlot=results$myPlot
} else if(agg==DOW)
{
# get plot and table for aggregation type year
results=edCensusPlotAndTable(occFil=occFil,occAggFil=factor(format(occTimeFil,"%a"),levels=LABEL_DOW), metric=metric, myParams=params)
myTable=results$myTable
myPlot=results$myPlot
} else if(agg==HOUR)
{
# get plot and table for aggregation type year
results=edCensusPlotAndTable(occFil=occFil,occAggFil=LABEL_HOUR[as.double(format(occTimeFil,"%H"))+1], metric=metric, myParams=params)
myTable=results$myTable
myPlot=results$myPlot
} else if(agg==WEEK)
{
# get plot and table for aggregation type date
occAggWeek=weekLabel(occTimeFil,dowStart=DOW_START)
results=edCensusPlotAndTable(occFil=occFil,occAggFil=occAggWeek, metric=metric, myParams=params)
myTable=results$myTable
myPlot=results$myPlot
} else if(agg==MD)
{
# occ, min x dow
nweek=round(as.double(table(format(occTime,"%w")))/(60*24))
dowNum=match(dow,LABEL_DOW)-1
dowTime=factor(mapLabel(format(occTime,"%w"), dowNum, dow), levels=dow)
minute=factor(as.character(as.double(format(occTime,"%H"))*60+as.double(format(occTime,"%M"))),levels=as.character(c(0:(24*60-1))))
# create dow x min matrix for each type of occ calculation
toPlotOcc=list()
for(i in 1:length(occ))
{
# aggregate by day of the week and minute of the day
df=data.frame(occType=occ[[i]], dowTime=dowTime, minute=minute)
meltMD=aggregate(occType ~ dowTime + minute, df, FUN=sum)
# setup for multiple lines
matMD=matrix(meltMD$occType/as.double(nweek), nrow=length(dow), ncol=24*60, byrow=FALSE)
colnames(matMD)=as.character(c(0:(24*60-1)))
if(length(which(nweek>1))>0)
{
rownames(matMD)=dow
} else
{
oIndeces=match(as.character(format(occTime[seq(from=1,to=length(occTime),by=24*60)],"%a")),dow)
matMD=matrix(matMD[oIndeces,],nrow=nrow(matMD))
rownames(matMD)=as.character(format(occTime[seq(from=1,to=length(occTime),by=24*60)],"%Y-%m-%d\n%a"))
}
# set indeces to 0 if not in time range
if(length(mIndeces)<ncol(matMD))
{
matMD[,-mIndeces]=NA
}
toPlotOcc[[i]]=matMD
}
names(toPlotOcc)=names(occ)
# define xlimits
if(sHourNum <= eHourNum)
{
xlimits=c(sHourNum*60+sMinNum, eHourNum*60+eMinNum)
} else
{
xlimits=c(0,24*60)
}
# plot as multi-barplot
myPlot=plotMultiBar(myList=toPlotOcc, xlab="", ylab=metric, label=metric, xlimits=xlimits, myParams=params, metric=metric)
# table
myTable=t(rbind(getMinLabel(c(1:ncol(toPlotOcc[[1]]))), rep(names(toPlotOcc)[1],,ncol(toPlotOcc[[i]])), toPlotOcc[[1]]))
if(length(toPlotOcc)>1)
{
for(i in 2:length(toPlotOcc))
{
tmat=t(rbind(getMinLabel(c(1:ncol(toPlotOcc[[i]]))), rep(names(toPlotOcc)[i],ncol(toPlotOcc[[i]])), toPlotOcc[[i]]))
myTable=rbind(myTable,tmat)
}
}
colnames(myTable)[1:2]=c("Minute",tail(strsplit(metric," ")[[1]],1))
occLabels=MAP_ALL_OCC[myTable[,2]]
occLabels[which(is.na(occLabels))]=myTable[,2]
myTable[,2]=occLabels
}