ddediu / AdhereR

Computation of adherence to medications from Electronic Healthcare Data in R
26 stars 6 forks source link

AdhereR v0.6.2 $CMA data.frame not always stored. #158

Closed dffyfe closed 3 years ago

dffyfe commented 3 years ago

Hi Dan,

When running CMA_sliding_window using AdhereR v0.6.2 the $CMA data.frame is frequently empty making plotting and extracting CMA figures impossible. The behaviour seems inconsistent as when using med.events instead of my own data.frame it has worked as expected.

Code for scenario where $CMA is empty:

library(AdhereR)
library(dplyr)

df2 <- tribble(~CHI, ~DisDate, ~AdhereRLabel, ~AdhereRLabel2,   ~Medication, ~MarkerLengthUse,
1,"01/01/2020","PARA","PARA   500MG","A",15,
2,"02/01/2020","PARA","PARA   500MG","A",30,
1,"29/01/2020","PARA","PARA   250MG","B",15,
2,"30/01/2020","PARA","PARA   250MG","B",30,
1,"26/02/2020","PARA","PARA   500MG","A",15,
2,"27/02/2020","PARA","PARA   500MG","A",30,
1,"25/03/2020","PARA","PARA   250MG","B",15,
2,"26/03/2020","PARA","PARA   250MG","B",30,
1,"22/04/2020","PARA","PARA   500MG","A",15,
2,"23/04/2020","PARA","PARA   500MG","A",30,
1,"20/05/2020","PARA","PARA   250MG","B",15,
2,"21/05/2020","PARA","PARA   250MG","B",30,
1,"17/06/2020","PARA","PARA   500MG","A",15,
2,"18/06/2020","PARA","PARA   500MG","A",30,
1,"15/07/2020","PARA","PARA   250MG","B",15,
2,"16/07/2020","PARA","PARA   250MG","B",30,
1,"12/08/2020","PARA","PARA   500MG","A",15,
2,"13/08/2020","PARA","PARA   500MG","A",30,
1,"09/09/2020","PARA","PARA   250MG","B",15,
2,"10/09/2020","PARA","PARA   250MG","B",30,
1,"07/10/2020","PARA","PARA   500MG","A",15,
2,"08/10/2020","PARA","PARA   500MG","A",30,
1,"04/11/2020","PARA","PARA   250MG","B",15,
2,"05/11/2020","PARA","PARA   250MG","B",30,
1,"02/12/2020","PARA","PARA   500MG","A",15,
2,"03/12/2020","PARA","PARA   500MG","A",30,
1,"30/12/2020","PARA","PARA   250MG","B",15,
2,"31/12/2020","PARA","PARA   250MG","B",30
) 

df2$DisDate   <- as.Date(df2$DisDate, "%d/%m/%Y")

check <-   CMA_sliding_window(CMA.to.apply = "CMA7",
                                                    data=df2,
                                                    ID.colname = "AdhereRLabel",
                                                    event.date.colname = "DisDate",
                                                    event.duration.colname = "MarkerLengthUse",
                                                    medication.class.colname = "Medication",
                                                    carry.only.for.same.medication = FALSE,
                                                    followup.window.start = as.Date("01/01/2020",   "%d/%m/%Y"),
                                                    followup.window.start.unit = "days",
                                                    followup.window.duration = 365,
                                                    followup.window.duration.unit = "days",
                                                    observation.window.start = as.Date("01/06/2020",   "%d/%m/%Y"),
                                                    observation.window.start.unit = "days",
                                                    observation.window.duration = 90,
                                                    observation.window.duration.unit = "days",
                                                    sliding.window.start = 0,
                                                    sliding.window.start.unit = "days",
                                                    sliding.window.duration = round(90/1,0),
                                                    sliding.window.duration.unit = "days",
                                                    sliding.window.step.duration = 90,
                                                    sliding.window.step.unit = "days",
                                                    sliding.window.no.steps = 1,
                                                    date.format = "%Y-%m-%d")

str(check)

Code for scenario where CMA_sliding_window behaves as expected:

cma7 <- CMA_sliding_window(CMA.to.apply = "CMA7",
                                                  data=med.events[med.events$PATIENT_ID %in% c(1,2,3),],
                                                  ID.colname =   "PATIENT_ID",
                                                  event.date.colname = "DATE",
                                                  event.duration.colname = "DURATION",
                                                  medication.class.colname = "CATEGORY",
                                                  #event.daily.dose.colname = "Min",
                                                  carry.only.for.same.medication = FALSE,
                                                  followup.window.start = 0,
                                                  followup.window.start.unit = "days",
                                                  followup.window.duration = 540,
                                                  followup.window.duration.unit = "days",
                                                  observation.window.start = 180,
                                                  observation.window.start.unit = "days",
                                                  observation.window.duration = 180,
                                                  observation.window.duration.unit = "days",
                                                  sliding.window.start = 0,
                                                  sliding.window.start.unit = "days",
                                                  sliding.window.duration = 90,
                                                  sliding.window.duration.unit = "days",
                                                  sliding.window.step.duration = 180,
                                                  sliding.window.step.unit = "days",
                                                  sliding.window.no.steps = 2,
                                                  date.format =   "%m/%d/%Y")

str(cma7) 

David

ddediu commented 3 years ago

It is due to the fact that the length of the OW (90 days) is exactly the same as the sliding window, and now there's an explicit checking that the sliding window fits "comfortably" in the OW (actually, this fixes a bug with sliding windows being larger than the OW)

ddediu commented 3 years ago

Fixed by allowing sliding windows that are as long as the OW (really, just 1 sliding window). Please note that serious testing is required for corner cases