Skip to content

Instantly share code, notes, and snippets.

@gabrielburcea
Last active July 26, 2018 14:12
Show Gist options
  • Save gabrielburcea/06a7bd71e45f864a945e0c654aabba02 to your computer and use it in GitHub Desktop.
Save gabrielburcea/06a7bd71e45f864a945e0c654aabba02 to your computer and use it in GitHub Desktop.
slide 13 Readmission in 7 days
####################################
# Readmission_7days ################
####################################
library(tidyverse)
library(lubridate)
data <- read.csv("/df.csv")
data$X <- NULL
#######################################################
# Code that converts all the columns names to lowercase
df_tolower <- function(x) {
colnames(x) <- tolower(colnames(x))
x
}
#####################################################
df <- df_tolower(data )
################################################################################################################
# subseting data set
################################################################################################################
dt_rmds<- df[c("pat_code","start_datetime", "end_datetime", "spell.type", "spell.number", "episode.order")]
colname_ct <- function(df, colname){
df[,colname] = as.POSIXct(df[,colname])
df
}
dt_rmds_ct <- dt_rmds %>%
colname_ct("start_datetime") %>%
colname_ct("end_datetime")
str(dt_rmds_ct)
dt_rmds_dates <-subset(dt_rmds_ct, (start_datetime >= "2013-05-01" & end_datetime <= "2014-10-01"))
#################################################################################################################
# first, we create a table that contains the csn of the relevant cases
# and the days since last admission
#dt_rmds_4000 <- dt_rmds_dates[4000:5,]
#dt_rdms_40_filt_ep <- dt_rmds_40 %>%
#dplyr::filter(spell.type == "Emergency")
dt_final <- dt_rmds_dates %>% # we take our data frame
mutate(readm_date = as_date(end_datetime)) %>% # turn the dates into date format
filter(spell.type == "Emergency") %>% # filter out cases within the time period wanted
arrange(pat_code, readm_date) %>% # sort them first by mrn and then by admit_date
group_by(pat_code) %>% # group them by mrn so we can for each patient...
mutate(daysSinceLastAdmit = readm_date - lag(readm_date)) %>% # ...get the days since last admit
mutate(daysSinceLastAdmit = as.integer(daysSinceLastAdmit)) %>% # turn this into an integer
filter(daysSinceLastAdmit <= 7|is.na(daysSinceLastAdmit)) %>% # & daysSinceLastAdmit > 0???? keep the NA's as well yet get rid of zeros as this reveals there are no readmissions
mutate(readmissions_7days = if_else(is.na(daysSinceLastAdmit), "no", "yes")) %>% #create the wanted variable
ungroup() %>% # ungroup it (must be done - don't ask)
mutate(one_month = lubridate::round_date(start_datetime, "1 month", "month")) %>%
select(pat_code, daysSinceLastAdmit, end_datetime, one_month, readmissions_7days, spell.type ) #readmissions_7days, start_datetime, one_month
#########################
#rounding the months ####
#########################
df_readm_7d <- dt_final %>%
mutate(one_month = lubridate::round_date(end_datetime, "1 month", "month")) %>%
filter(spell.type == "Emergency")
###################################################################
# taking out the NA and rows with zero and days bigger than 7 #####
###################################################################
df_na_omit <- dt_final %>%
filter(daysSinceLastAdmit > "0" & daysSinceLastAdmit <= "7")
# counting the Readmissions
sum_readm <- df_na_omit %>%
group_by(one_month) %>%
dplyr::summarise(Readm = n())
# counting the discharges
df_count_disc <- dt_final %>%
group_by(one_month) %>%
tally()
##############################################
# and joining with the readmission dataset ###
##############################################
df_read_and_count <- left_join(df_count_disc, sum_readm, by = "one_month")
######################################################
#This is in case we want all the dates on the plot ###
######################################################
# Set levels for date variable -- ensure they are unique
#TwoWeeks_levels <- format( unique (dt_final$TwoWeeks), "%d %m %Y")
#dt_final1 <- sum_readm %>%
#convert date variable t a facot with set levels
#mutate(one_month = factor(format(one_month, "%d %m %Y"), levels = one_month_levels))
######################################################
#pct$data$x <- as.Date(pct$data$x, tz = 'Europe/London')
#pct1 <- ggplot(pct$data, aes(x,y, label =x))
#pct1
library(qicharts2)
######################################################
pct <- qic(Readm,
x = one_month,
data = df_read_and_count,
chart = 'pp',
#standardised = TRUE,
#multiply= 100,
title = "Readmissions within 7 days, counts n*",
ylab = "Percent patients",
xlab = "Readmissions within 7 days",
x.angle = 45)
pct
#Error in qic(Count_readm_7days, n = Count_discharges, x = one_month, data = dt_join, :
#unused argument (standardised = TRUE)
####################################################
#Readmission in 7 days
####################################################
plot3 <- qicharts2::qic(Readm,
n = n,
x = one_month,
data = df_read_and_count,
chart = 'pp',
ylab = "percent",
#freeze = 1,
#part = 11,
multiply = 100,
#x.format = "%d-%m-%Y",
x.angle = 45,
xlab = "Readmissions within 7 days, Percentages %",
title = "Readmissions within 7 days, 1st May, 2013 - 10th of October, 2014")
plot3
p3 <- plot3$data
#######################################################################
# This isn't necessary but really hope you can help me finding #######
# an answer to how I can plot all the dates #######
#######################################################################
# trying to plot the qic chart with ggplot ######
# so that I will be able to see all dates with their variation ######
#######################################################################
library(ggplot2)
library(plyr)
p3.1 <- rename(p3, c("x" = "one_month"))
p3.1$one_month <- as.character(p3.1$x, format="%Y-%m-%d")
plot4 <- ggplot(p3.1, aes(x = one_month,y = y )) +
geom_ribbon(ymin = p3.1$lcl, ymax = p3.1$ucl, alpha = 0 ) + # fill = "" #
geom_line(colour = "red", size = .75) +
geom_line(aes(one_month, cl)) +
geom_point(colour = "blue" , fill = "blue", size = 1.5) +
#x.axis(1, p3$x, format(p3$x, "%Y-%m-%d"), cex.axis = 0.7)+
ggtitle(label = "Readmissions within 7 days") +
labs(x = NULL,
y = NULL)+
theme_minimal()+
theme(axis.text.x = element_text(angle = 45 , hjust = 1)) + #hjust = 1
theme_bw()
plot4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment