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
#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.21))
Sort by date
#Sort by date
DF <- DF[order(DF$DateVariable),]
Add case numbers (in order, since sorted)
DF <- rbind.fill(data.frame(outcome=1),DF)
#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)
#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
#Plot it
library(ggplot2)
ggplot(DF, aes(x,y)) + geom_line
geom_point(shape=4) +
geom_point(aes(x,outcome),shape=3) +
stat_smooth(method="loess", se=FALSE, color="darkgreen", size=1) +
scale_y_continuous(name= "Failure rate", limits=c(0, 1)) +
scale_x_continuous(name= "Operations performed")
Result:
(plus marks success and error cases. X - cumulative sum, line - loess curve)
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.