I am trying to study medication switching patterns from prescription data as in the table (result of dput):
structure(list(ID = c("A2M8K", "A2M8K", "A2M8K", "A2M8K", "A2M8K",
"D2N4", "D2N4", "D2N4", "D2N4", "D2N4", "D13L", "D13L", "D13L",
"D13L", "D13L", "D13L", "D13L", "D13L", "D13L", "D13L"), Class = c("Biguanides ",
"Sulfonylureas", "Sulfonylureas", "Biguanides ", "Sulfonylureas",
"DPP ", "Sulfonylureas", "GLP ", "GLP ", "Sulfonylureas", "Biguanides ",
"Sulfonylureas", "Biguanides ", "Sulfonylureas", "DPP ", "Insulins",
"Biguanides ", "Sulfonylureas", "Insulins", "Biguanides "), start_date = c("7/1/2018",
"7/1/2018", "10/3/2018", "8/27/2020", "8/27/2020", "7/31/2019",
"7/31/2019", "3/1/2020", "6/15/2020", "6/15/2020", "2/15/2016",
"2/15/2016", "2/20/2017", "2/20/2017", "1/21/2018", "7/26/2018",
"5/15/2019", "5/15/2019", "7/24/2019", "1/30/2020"), end_date = c("6/22/2020",
"8/30/2018", "6/22/2020", "10/26/2020", "10/26/2020", "10/29/2019",
"3/28/2020", "3/31/2020", "10/8/2020", "10/13/2020", "11/9/2016",
"11/9/2016", "10/24/2018", "10/24/2018", "4/21/2018", "10/29/2018",
"10/22/2019", "10/22/2019", "10/22/2019", "11/25/2020")), row.names = c(NA,
20L), class = "data.frame")
I tried the following in R:
###DF sample:
med_changes <- data.frame(ID = character(),
Change.type = character(),
Med.change = character(),
stringsAsFactors = FALSE)
#arrange:
library(dplyr)
prescriptions <- prescriptions %>%
group_by(ID) %>%
mutate(prev_med = lag(Class),
prev_date = lag(end_date)) %>%
ungroup() %>%
filter(!is.na(prev_med))
### Switch:
med_changes <- prescriptions %>%
filter(as.numeric(difftime(start_date, prev_date, units = "days")) > 60) %>%
mutate(Change.type = "Switch",
Med.change = paste(prev_med, "to", Class),
Time.to.change = as.numeric(difftime(start_date, prev_date, units = "days"))) %>%
select(ID, Change.type, Med.change, Time.to.change)
### Add-On:
add_on <- prescriptions %>%
filter(as.numeric(difftime(start_date, prev_date, units = "days")) > -60 & as.numeric(difftime(start_date, prev_date, units = "days")) <= 60) %>%
mutate(Change.type = "Add-on",
Med.change = paste(prev_med, "added to", Class),
Time.to.change = as.numeric(difftime(start_date, prev_date, units = "days"))) %>%
select(ID, Change.type, Med.change, Time.to.change)
med_changes <- rbind(med_changes, add_on)
However this does not take the starting regimen into account; if two medications have the same start date it means the patient started on multi regimen treatment(multiple medications) and the following row class should be checked in all the previous regimen classes to determine if it was added on or switched or nothing has been changed.
The result I am looking for is similar to this:
Is there I way to modify this script to do it, or if you can recommend a package to do this, I will appreciate it.