# [R] R Plots for Recurrent Events - Suggestions are needed

Michael Bedward michael.bedward at gmail.com
Mon Dec 13 05:59:28 CET 2010

Hi Haoda,

I couldn't find a package that implements this, although I'm not
familiar with the field so there could be something but using
different terminology.

However, looking at the the Google preview of Nelson (2003) which is
cited by the page that you linked to, the calculations seem very
simple (see page 46).  Here is a function which I think does what is
described there (warning: untested !)...

mcf <- function(events) {
# events a data.frame or matrix where:
#   col 1 is unitID
#   col 2 is event time (integer)
#   col 3 is event type coded as:
#      0: recurrence
#      1: first appearance (left censoring time)
#      2: last appearance (right censoring time)
#
# Order events data by time, unit id, event type
events <- events[ order(events[,2], events[,1], events[,3]), ]

m <- matrix(0, nrow=nrow(events), ncol=4)
colnames(m) <- c("time", "Nrisk", "incr", "mcf")

# copy event times
m[, 1] <- events[, 2]

# number of units observed at each time
m[, 2] <- cumsum(ifelse(events[, 3] == 2, -1, events[, 3]))

# incremental risk
irecurrence <- events[,3] == 0
m[irecurrence, 3] <- 1 / m[irecurrence, 2]

# cumulative risk (MCF estimate)
m[, 4] <- cumsum(m[, 3])

# return results (matrix rows with recurrent events)
m[events[,3] == 0, ]
}

Hope this helps.

Michael

On 13 December 2010 03:47, Haoda Fu <fuhds at yahoo.com.cn> wrote:
> Hi,
>
> I am wondering if there is a simple way to plot MCF(mean cumulative function) for recurrent events in R?
>
> MCF (http://www.weibull.com/hotwire/issue57/relbasics57.htm)
>
> And do you have some other recommendation in visualize recurrent events?
>
> I did some research online. Some people use ggplot2 to plot MCF, but it requires some additional coding.
> http://user2010.org/slides/Shentu.pdf
>
> Your help and suggestions are highly appreciated.
>
> Warm regards from Haoda
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help