Skip to main content
1 of 2
Brian Diggs
  • 1.1k
  • 9
  • 18

If your dates are in string or factor form, convert them to Dates with as.Date(DateVariable, format="%Y-%m-%d") (potentially with an as.character around DateVariable if it is a factor.

Sort the data.frame with the date and outcome by surgery date. To get the y variable you describe, the easiest way is to take the cumulative number of outcomes and divide by the number of cases to date (which is just a running sequence from 1 to the number of cases when sorted). In code:

Make some random data to play with

DF <- data.frame(DateVariable = as.Date(runif(100, 0, 800), origin="2005-01-01"),
    outcome = rbinom(100, 1, 0.2))

Sort by date

DF <- DF[order(DF$DateVariable),]

Add case numbers (in order, since sorted)

DF$x <- seq(length=nrow(DF))

Create your definition for y (average to date, which is sum to date divided by number to date)

DF$y <- cumsum(DF$outcome) / DF$x

Plot it

ggplot(DF, aes(x,y)) + geom_line()

I don't think, however, this is a good metric. Check out some work on CUSUM curves and risk adjusted CUSUM curves. CUSUM is just plotting number of (negative) outcomes versus case number; risk adjusted CUSUM assumes you can determine a probability of negative outcome (based on pre-operative variables) and use that to determine if performance is exceeding or lagging expectations.

Brian Diggs
  • 1.1k
  • 9
  • 18