ddediu / AdhereR

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

event-interval-covered odd behaviour with event starting before the OW #175

Closed dffyfe closed 3 years ago

dffyfe commented 3 years ago

Hi Dan,

Not sure what is causing this one ATM (note 'Drug 12' doesn't get plotted so could be to do with that), the event-interval bars for Drug 14 and 15 are off. Works fine if you plot Drug 14 on its own so I've included the full data.frame.

CMAExamples <- structure(list(CHI = c(9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 
9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 
9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 
9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 9999L, 
9999L, 9999L, 9999L, 9999L, 9999L, 9999L), AdhereRLabel2 = c("Drug 01", 
"Drug 01", "Drug 01", "Drug 01", "Drug 01", "Drug 01", "Drug 01", 
"Drug 01", "Drug 01", "Drug 10", "Drug 11", "Drug 12", "Drug 02", 
"Drug 02", "Drug 02", "Drug 02", "Drug 02", "Drug 02", "Drug 02", 
"Drug 02", "Drug 02", "Drug 03", "Drug 04", "Drug 04", "Drug 05", 
"Drug 05", "Drug 06", "Drug 06", "Drug 07", "Drug 07", "Drug 08", 
"Drug 09", "Drug 04", "Drug 13", "Drug 13", "Drug 13", "Drug 14", 
"Drug 14", "Drug 15"), DisDate = structure(c(18201, 18203, 18256, 
18347, 18408, 18611, 18612, 18616, 18642, 18408, 18611, 18642, 
18201, 18203, 18256, 18347, 18408, 18504, 18536, 18616, 18642, 
18201, 18256, 18611, 18408, 18408, 18611, 18201, 18642, 18347, 
18201, 18256, 18347, 18347, 18347, 18347, 18201, 18441, 18201
), class = "Date"), MarkerLength = c(28, 60, 28, 28, 55, 60, 
60, 28, 28, 55, 60, 28, 28, 60, 28, 28, 160, 160, 160, 28, 28, 
28, 28, 60, 55, 160, 60, 28, 28, 28, 28, 28, 28, 1e-11, 1e-11, 
1e-11, 450, 1e-10, 450), Quantity = c(28L, 28L, 28L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 
28L, 28L, 28L, 28L, 28L, 28L, 28L, 28L), PISSectionCode = c(901L, 
901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 
901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 
901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 901L, 
901L, 901L, 901L, 901L, 901L), AN = c("FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", "FOLIC ACID", 
"FOLIC ACID", "FOLIC ACID"), Indexing = c(1L, 2L, 3L, 4L, 5L, 
6L, 7L, 8L, 9L, 5L, 6L, 9L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
1L, 3L, 6L, 5L, 5L, 6L, 1L, 9L, 4L, 1L, 3L, 4L, 4L, 4L, 4L, 1L, 
1L, 1L), X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "333", 
"331", "332", "221", "2211", "2212"), class = "data.frame")

cma7 <- CMA7(
  data=CMAExamples,
  ID.colname = "AdhereRLabel2",
  event.date.colname = "DisDate", 
  event.duration.colname = "MarkerLength", 
  #medication.groups = medgroupsdf,
  #medication.groups = "AdhereRLabel2",
  date.format = "%Y-%m-%d",
  followup.window.start = as.Date("2019-11-01"),
  followup.window.start.unit = "days",
  followup.window.duration = 500,
  followup.window.duration.unit = "days",
  observation.window.start = as.Date("2020-01-01"),
  observation.window.start.unit = "days",
  observation.window.duration = 365,
  observation.window.duration.unit = "days",
)

plot(cma7, 
     show.period = "dates", 
     period.in.days = 60,
     show.legend = FALSE,
     ylab = c(withCMA = "Section - Drug Name"),
     cex.axis = 1,
     title = "CHIno",
     col.cats = colorblind,
     CMA.cex = 0.8,
     plot.CMA = F,
     plot.CMA.as.histogram = F,
     force.draw.text = TRUE,
     CMA.plot.ratio = 0.000000000000000000000001,
     min.plot.size.in.characters.horiz=0,
     min.plot.size.in.characters.vert=0)
dffyfe commented 3 years ago

Hi Dan,

Adding

cma$event.info <- cma$event.info[ !(cma$event.info[,col.patid] %in% patids.no.events.to.plot), ];

above

cma$data <- cma$data[ !(cma$data[,col.patid] %in% patids.no.events.to.plot), ];

in the inherits(cma, "CMA0") conditional fixes this. Probably needed above the other 3 instances as well but not 100% sure yet!

ddediu commented 3 years ago

Indeed, it was due to "Drug 12" being empty and that messed up the following event intervals -- your suggested fix indeed worked, thanks :)