I am attempting this question to calculate the variance using a survival function I have calculated, and this is my approach, however I am not sure why there is an error. Can someone kindly please help me out?
survfunc <- cancer_obs$KM
B <- 100
N <- 1000
# Replace P(T > t) with the appropriate survival function or probability distribution function for T
integrand1 <- function(t) {survfunc}
midpoint_rule1 <- function(a, b, n) {
h <- (b - a) / n
sum(integrand1(a + h/2 + h * (0:(n-1)))) * h
}
integrand2 <- function(t) {2*t*survfunc}
midpoint_rule2 <- function(a, b, n) {
h <- (b - a) / n
sum(integrand2(a + h/2 + h * (0:(n-1)))) * h
}
approx_integral1 <- midpoint_rule1(0, B, N)
approx_integral2 <- midpoint_rule2(0, B, N)
var_estimation <- approx_integral2 - (approx_integral1)^2