-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql & waiting.R
100 lines (67 loc) · 3.3 KB
/
sql & waiting.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
con = dbConnect(MySQL(),
user = 'root',
password = '@newconvenant',
dbname = 'garkidb',
host = 'localhost')
dbListTables(con)
##############################################################################
# USING LUBRIDATE DIFF TIME TO CALCULATE WAITING TIME
#FROM THE TABLE 'ENCOUNTER'
waity = tbl(con, 'encounter') %>% na.omit() %>%
select(start_date, triaged_on, canceled, follow_up) %>%
filter(canceled == 0 & follow_up == 1) %>%
collect() %>%
mutate(start_date = as.POSIXct(start_date)) %>%
mutate(triaged_on = as.POSIXct(triaged_on)) %>%
mutate(pxwait = difftime(triaged_on, start_date, units = "mins")) %>%
mutate(year = year(start_date)) %>%
select(year, everything()) %>%
mutate(pxwait = as.numeric(pxwait)) %>%
mutate(min_wait = pxwait/60) %>%
mutate(min_wait = as.numeric(min_wait))
#######################################################################
#Removing Outliers
wait_outlier =boxplot(waity$min_wait, plot=FALSE)$out
#CREATE ANOTHER DF TO PRESERVE ORIGINAL VALUE
temp = waity
#REMOVE OUTLIERS FROM THE NEW DF
temp = temp[-which(temp$min_wait %in% outliers),]
#CHECK THE MEAN FOR THE YEAR YOU ARE INTERESTED IN
#USE SUMMARY TO KNOW THE MEAN
temp %>% filter(year== 2019) %>% summary(pxwait)
#################################################################################################
#ANOTHER WAY TO DO THIS: DIRECTLY IN SQL
################################################################################################
dbListTables(con)
dsql = "SELECT start_date, triaged_on, consulted_on, consulted_by,
TIMESTAMPDIFF(MINUTE, triaged_on, consulted_on)
AS patient_wait, YEAR(start_date) AS visit_year FROM encounter
WHERE (start_date > '2017-12-31 23:59:59' AND
(triaged_on IS NOT NULL AND consulted_on IS NOT NULL) AND
TIMESTAMPDIFF(DAY, consulted_on, triaged_on) < 1 AND
TIMESTAMPDIFF(DAY, start_date, consulted_on) < 1)"
a = dbGetQuery(con, dsql)
b = a %>% filter(visit_year == 2020) %>% mutate(wait_time = patient_wait/60) %>% summary(mean(wait_time))
b
waity = tbl(con, 'encounter') %>% na.omit() %>%
select(start_date, triaged_on, canceled, follow_up) %>%
filter(canceled == 0 & follow_up == 1) %>%
collect() %>%
mutate(start_date = as.POSIXct(start_date)) %>%
mutate(triaged_on = as.POSIXct(triaged_on)) %>%
mutate(pxwait = time_length(start = triaged_on, end = start_date, tzone = triaged_on)) %>%
mutate(year = year(start_date)) %>%
select(year, everything()) %>%
mutate(pxwait = as.numeric(pxwait)) %>%
mutate(min_wait = pxwait/60) %>%
mutate(min_wait = as.numeric(min_wait))
###################################################################################
# GET THE STAFF THAT CONSULTED ON THE PATIENTS
dstaff = "SELECT start_date, consulted_by, doctor_start_time,
CONCAT(firstname, ' ', lastname) AS provider, departments.name AS department,
staff_type AS specialization, YEAR(start_date) AS year FROM encounter
LEFT JOIN staff_directory ON staff_directory.staffID = encounter.consulted_by
LEFT JOIN departments ON departments.id = encounter.department_id
JOIN staff_specialization ON staff_specialization.id = encounter.specialization_id
WHERE staff_directory.is_consultant IS NOT NULL"
dstaff = dbGetQuery(con, dstaff)