# Simulate recurrent events data where last observation is censored

I would like to simulate a recurrent events dataset for survival analysis with cox regression in R. The data should have the following properties:

• multiple, varying number of observations per ID
• only the last observation for an ID is censored
• start/stop data structure
• one time-varying coefficients
• exponential distribution of events
da <- data.frame(id = c(1,1,1,1,2,2,2,3,3,3,3,3,3), start =                     c(0,1,2,3,0,1,2,0,1,2,3,4,5), stop = c(1,2,3,4,1,2,3,1,2,3,4,5,6), x = c(7,6,1,3,4,1,8,9,4,1,2,2,1), event = c(1,0,1,0,1,1,0,1,1,0,1,1,0))

da
id start stop x event
1   1     0    1 7     1
2   1     1    2 6     0
3   1     2    3 1     1
4   1     3    4 3     0
5   2     0    1 4     1
6   2     1    2 1     1
7   2     2    3 8     0
8   3     0    1 9     1
9   3     1    2 4     1
10  3     2    3 1     0
11  3     3    4 2     1
12  3     4    5 2     1
13  3     5    6 1     0

# id = subject identifier
# start / stop = used to set the time range for each subject
# x = time-varying covariate
# event = a recurrent event of interest (1), a non-recurrent event (0) which can also mean last observation for which we dont know the outcome