药物切换模式

Medication Switching Patterns

提问人:Dudeii 提问时间:3/18/2023 最后编辑:Dudeii 更新时间:3/18/2023 访问量:53

问:

我正在尝试从处方数据中研究药物转换模式,如表所示(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")

我在 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)

然而,这并没有考虑到起始治疗方案;如果两种药物具有相同的开始日期,则意味着患者开始接受多种方案治疗(多种药物),并且应检查所有先前方案类别中的下一行类别,以确定它是否被添加或切换或没有任何变化。

我正在寻找的结果类似于以下内容:

Result

我有没有办法修改这个脚本来做到这一点,或者如果你能推荐一个包来做到这一点,我将不胜感激。

R 切换

评论

2赞 zephryl 3/18/2023
您能否以文本而不是屏幕截图的形式提供您的数据和所需的输出?例如,使用 或 .另请查看如何制作出色的 R 可重现示例。谢谢!dput(prescriptions)dput(head(prescriptions, 20))
1赞 Dudeii 3/18/2023
@zephryl我已经根据您的建议更新了帖子。

答: 暂无答案