How to find cumulative hazard when we have both start time and event time in r I have the time at start($x_0$) of the study and failure time/censor time($x_t$) for each patient. I want to calculate the Nelson Aalen estimate for each patient and bind it to the data.
First of all, I would like to clarify whether my theoretical understanding is right :
$$\tilde{H}(t)=\sum_{t_{i-1}\leq t \leq t_{i}}\frac{d_i}{n_i}$$
or is it:
$$\tilde{H}(t)=\sum_{t_i\leq t}\frac{d_i}{n_i}$$
Now moving to the coding part, my data set is :
N <- 10^4 # population
H <- within(data.frame(start_time=runif(N, 0, 50), x1=rnorm(N, 2, 1), x2=rnorm(N, -2, 1)), {
  lp <-   0.05*x1 + 0.2*x2 
  Tm <- qweibull(runif(N,pweibull(start_time,shape = 7.5, scale = 84*exp(-lp/7.5)),1), shape=7.5, scale=84*exp(-lp/7.5))
  Cens1 <- 100
  event_time <- pmin(Tm,Cens1)
  status <- as.numeric(event_time == Tm)})  


If it is the first equation, which of the ones below should I choose?:
cox_out1 = basehaz(coxph(Surv(event_time,status)~1)
idx1 = match(event_time, cox_out1[,"time"])
cox_out2 = basehaz(coxph(Surv(start_time,status)~1)
idx2 = match(start_time, cox_out2[,"time"])
hazard = cox_out1[idx1,"hazard"] - cox_out2[idx2,"hazard"]
return(hazard)

OR
cox_out = basehaz(coxph(Surv(start_time,event_time,status)~1)

If the second code above is the right one then how do I combine it with my data since the output is the pair hazard and time(event_time) arranged in ascending order?
And if it is the second one then I think the r code will be as follows (but in my personal opinion I don't think this is the correct one):
cox_out = basehaz(coxph(Surv(event_time,status)~1)
hazard = cox_out[,"hazard"] 

 A: The Surv(startTime,endTime,status) format is used for things like modeling with time-dependent covariates or multi-state models. That doesn't seem to be the case here.
The time reference for each individual in a survival model is usually set to time=0 for the time of that individual's entry into the study, $x_0$ in your terminology. Under that standard convention, the event or censoring time for each individual should be expressed as $x_t-x_0$. If for some reason you also want to model the entry times for individuals relative to some common time point then you have a more complicated situation that would require a multi-state model and some assumptions about the individuals before formal entry into the study.
The Nelson-Aalen estimate is for the cumulative hazard, expressed from time=0 through the time of interest $t$. Under that convention only the second form that you present makes sense, as only it goes back to the earliest event time. Your first formula seems to be the cumulative hazard from time $t_{i-1}$ to time $t_i$, inclusive of both end points.
Coding questions per se are off topic on this site. What you wish to accomplish seems to be implemented in the nelsonaalen() function of the R mice package. The central part of its code is:
hazard <- survival::basehaz(survival::coxph(survival::Surv(time,status) ~ 1))
idx <- match(time, hazard[, "time"])
return(hazard[idx, "hazard"])

Hidden in these function calls is the issue of handling tied event times. Your second, standard form for the Nelson-Aalen estimate uses the Breslow handling of ties, while many prefer the Efron handling. See this page for example. In the R survival package the choice between the 2 methods is set by an option that can be adjusted.
