jkropko / coxed

Duration-Based Quantities of Interest and Simulation Methods for the Cox Proportional Hazards Model
23 stars 6 forks source link

redundant command in your R code #14

Open jkropko opened 3 years ago

jkropko commented 3 years ago

From Jonathan Golub:

Dear Jon,

I wonder, have I found a redundant command in your R code?

y <- apply(survival, 1, FUN = function(x) {

draw <- runif(1)

z <- diff(x < draw)

r <- ifelse(all(z == 0), T, which.max(z))

return(c(r, draw))

})

If I understand things, the diff(x<draw) command (and also the original diff(x<runif(1)) command) evaluates the individual survivor function contained in vector x, and assigns a "1" to the highest location along x where the value in vector x is greater than a draw from runif. So for example the highest location here is six:

x=c(1, 0.5, 0.5, 0.4, 0.3, 0.29, 0.28, 0.27, 0.15, 0.1) diff(x < 0.2875775)

[1] 0 0 0 0 0 1 0 0 0

But since every individual survivor function ends in zero, by definition you’ll never find a location along x where the value in vector x is NOT greater than a draw from runif.

x=c(1, 0.5, 0.5, 0.4, 0.3, 0.29, 0.28, 0.27, 0.15, 0.1, 0)

diff(x < 0.00005)

[1] 0 0 0 0 0 0 0 0 0 1

And this means that you’ll never have a situation where all(z==0) is true.

So I’d suggest that you simply use r <- which.max(z).

Best wishes,

Jonathan