Last active
July 26, 2018 14:17
-
-
Save gabrielburcea/7be162b7253c3be2137baf63cb66786f to your computer and use it in GitHub Desktop.
Slide 22 - Occupancy vs. Arrivals - hospital
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################### | |
# Arrivals vs Occupancy - ##################### | |
# for one month - from 01-03-2015 to 27-04-2015 ### | |
################################################### | |
################################################## | |
# loading the libraries | |
################################################## | |
library(tidyverse) | |
library(lubridate) | |
################################################## | |
################################################# | |
# reading the data | |
################################################# | |
data <- read.csv("df.csv", header = TRUE) | |
data$X <- NULL | |
####################################################### | |
# Code that converts all the columns names to lowercase | |
####################################################### | |
df_tolower <- function(x) { | |
colnames(x) <- tolower(colnames(x)) | |
x | |
} | |
##################################################### | |
# Applying the lower case function to the data | |
##################################################### | |
df <- df_tolower(data ) | |
################################################################################################################ | |
# subseting data set | |
################################################################################################################ | |
dt_rmds<- df[c("pat_code","start_datetime", "end_datetime", "spell.type", "spell.number", "episode.order")] | |
################################################### | |
# Function that transforms the dates into POSIXct## | |
################################################### | |
colname_ct <- function(df, colname){ | |
df[,colname] = as.POSIXct(df[,colname]) | |
df | |
} | |
#################################################### | |
# Applying the POSIXct function to the dt_rmds data | |
#################################################### | |
dt_rmds_ct <- dt_rmds %>% | |
colname_ct("start_datetime") %>% | |
colname_ct("end_datetime") | |
str(dt_rmds_ct) | |
#################################################### | |
# Selecting one month only ########## | |
#################################################### | |
dt_month <- subset(dt_rmds_ct, (start_datetime >= "2015-03-01 00:00:00" & start_datetime <= "2015-04-27 23:59:00") | | |
(end_datetime >= "2015-03-01 00:00:00" & end_datetime <= "2015-04-27 23:59:00")) | |
################################################################################# | |
# using gather function to create a new column with dates from both columns; #### | |
# and filter only by Emergency Department #### | |
################################################################################# | |
dt_hrs <- dt_month %>% | |
gather(key = Type, Time, 2:3) %>% | |
filter(spell.type == "Emergency") | |
################################################################################# | |
# extracting year, month, day and hour from Time column and Count = occupancy ### | |
################################################################################# | |
dt_sep <- dt_hrs %>% | |
mutate( | |
Time = ymd_hms(Time), | |
year = year(Time), | |
month = month(Time), | |
day = day(Time), | |
hour = hour(Time) | |
) %>% | |
dplyr::count(year, month, day, hour) | |
############################################################ | |
## find the averages of the Occupancy | |
############################################################ | |
tbl_avg_occ <- dt_sep %>% | |
group_by(hour) %>% | |
dplyr::summarize(Average_occ = mean(n)) %>% | |
ungroup() | |
#tbl_avg_date <- tbl_count %>% | |
# group_by(Date) %>% | |
#dplyr::summarize(mean_score = mean(n)) %>% | |
#ungroup() | |
############################################################################## | |
# take out the start-datetime only -> ###### | |
# this will be used later to be added to a plot alongside the occupancy ###### | |
############################################################################## | |
# count the patients | |
############################################################################## | |
dt_sep_arrivals <- dt_month %>% | |
select(pat_code, start_datetime) %>% | |
mutate( | |
year = year(start_datetime), | |
month = month(start_datetime), | |
day = day(start_datetime), | |
hour = hour(start_datetime) | |
) %>% | |
dplyr::count(year, month, day, hour) | |
##################################################################### | |
# Calculating the mean of the arrivals ############################## | |
##################################################################### | |
tbl_avg_arrivals <- dt_sep_arrivals %>% | |
group_by(hour) %>% | |
dplyr::summarize(Average_arrivals = mean(n)) %>% | |
ungroup() | |
########################################################### | |
# join the two tables - Averages - arrivals and occupancy # | |
########################################################### | |
tbl_occ_arriv <- left_join(tbl_avg_arrivals, tbl_avg_occ, by = "hour") | |
############################################################### | |
# Plot Occupancy vs. Arrivals = Averages ###################### | |
############################################################### | |
plt_occ_percent <- ggplot(tbl_occ_arriv, aes(x = as.numeric(hour), y = Average_occ, group = hour)) | |
plt_occ_percent + geom_bar(stat = "identity", alpha=0.7, width = 0.50, aes(fill = "Occupancy")) + | |
scale_x_continuous(breaks = 0:23) + | |
scale_y_continuous(expand = c(0, 0), limits = c(0, 30)) + | |
geom_point(aes(y = Average_arrivals)) + | |
geom_line(aes(y = Average_arrivals, group = 1, color = "Arrivals in A&E")) + | |
scale_fill_manual("",values="yellow4") + | |
scale_color_manual("",values = 1) + | |
theme_bw()+ | |
labs(title = "Average Hourly A&E Occupancy vs. Arrivals, 1st of March, 2015 to 27th of April, 2015", | |
subtitle = "Averages - Hourly ED Occupancy, % , by Hour of the day. | |
Note: results are intended for management information only", | |
y = "Average occupancy, n", x = "Hour of the day", caption = "Source: CLAHRC NWL") + | |
theme(axis.title.y = element_text(margin = margin(t = 0, r = 21, b = 0, l = 0)), | |
plot.title = element_text(size = 12, face = "bold"), | |
plot.subtitle = element_text(size = 10), | |
legend.position = "bottom", legend.box = "horizontal") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment