I'm asked to simulate 50 survival times from an exponential distribution with rate 1.
n <- 50
Tstar <- rexp(n, rate = 1)
Then I have the following quantities:
Y(t) capturing the individuals at risk at time t, i.e.
Y <- function(t){sum(Tstar > t)}
and S(t) is the Kaplan-Meier estimator
S <- function(t)(1 - 1/n * sum(Tstar < t)
But how do I define the following function?
Here Tstar[i] indicates T_i.
If I understand my mathematical notations, consider Reduce
+ sapply
to serve as iterator and summation across specific values:
set.seed(4621)
n <- 50
Tstar <- rexp(n, rate = 1)
Y <- function(t) sum(Tstar > t)
S <- function(t) (1 - 1/n * sum(Tstar < t))
sigma_sq <- function(t) {
Tstar <- Tstar[Tstar < t]
S(t)^2 * Reduce(`+`, sapply(Tstar, function(T_i) 1/(Y(t)*(T_i)^2)))
}