Skip to main content
shortened code
Source Link
gung - Reinstate Monica
  • 147.5k
  • 89
  • 406
  • 717
#------package------
library(survival)
#-------------------

#------some data------
data(kidney)
#---------------------

#------preparation------
tab <- data.frame(table(kidney[kidney$status == 1, "time"])) 
y <- as.numeric(levels(tab[, 1]))[tab[, 1]] #ordered distinct event times
d <- tab[, 2]                               #number of events
#-----------------------

#------Cox model------
fit<-coxph(Surv(time, status)~age, data=kidney)
#---------------------

#------cumulative hazard obtained from basehaz()------
H0 <- basehaz(fit, centered=FALSE)
H0 <- H0[H0[, 2] %in% y, ] #only keep rows where events occurred
#-----------------------------------------------------

#------my quick implementation------
betaHat <- fit$coef

h0 <- rep(NA, length(y))
for(l in 1:length(y))
{
  h0[l] <- d[l] / sum(exp(kidney[kidney$time >= y[l], "age"] * betaHat))
}
#-----------------------------------

#------comparison------
cbind(H0, cumsum(h0))
#----------------------
#------package------
library(survival)
#-------------------

#------some data------
data(kidney)
#---------------------

#------preparation------
tab <- data.frame(table(kidney[kidney$status == 1, "time"])) 
y <- as.numeric(levels(tab[, 1]))[tab[, 1]] #ordered distinct event times
d <- tab[, 2]                               #number of events
#-----------------------

#------Cox model------
fit<-coxph(Surv(time, status)~age, data=kidney)
#---------------------

#------cumulative hazard obtained from basehaz()------
H0 <- basehaz(fit, centered=FALSE)
H0 <- H0[H0[, 2] %in% y, ] #only keep rows where events occurred
#-----------------------------------------------------

#------my quick implementation------
betaHat <- fit$coef

h0 <- rep(NA, length(y))
for(l in 1:length(y))
{
  h0[l] <- d[l] / sum(exp(kidney[kidney$time >= y[l], "age"] * betaHat))
}
#-----------------------------------

#------comparison------
cbind(H0, cumsum(h0))
#----------------------
#------package------
library(survival)

#------some data------
data(kidney)

#------preparation------
tab <- data.frame(table(kidney[kidney$status == 1, "time"])) 
y <- as.numeric(levels(tab[, 1]))[tab[, 1]] #ordered distinct event times
d <- tab[, 2]                               #number of events

#------Cox model------
fit<-coxph(Surv(time, status)~age, data=kidney)

#------cumulative hazard obtained from basehaz()------
H0 <- basehaz(fit, centered=FALSE)
H0 <- H0[H0[, 2] %in% y, ] #only keep rows where events occurred

#------my quick implementation------
betaHat <- fit$coef

h0 <- rep(NA, length(y))
for(l in 1:length(y))
{
  h0[l] <- d[l] / sum(exp(kidney[kidney$time >= y[l], "age"] * betaHat))
}

#------comparison------
cbind(H0, cumsum(h0))
added 1 characters in body
Source Link
ocram
  • 22.4k
  • 5
  • 85
  • 83

Apparently, basehaz() actually computes a cumulative hazard rate, rather than the hazard rate itself. The formula is aas follows: $$ \hat{H}_0(t) = \sum_{y_{(l)} \leq t} \hat{h}_0(y_{(l)}), $$ with $$ \hat{h}_0(y_{(l)}) = \frac{d_{(l)}}{\sum_{j \in R(y_{(l)})} \exp(\mathbf{x}^{\prime}_j \mathbf{\beta})} $$ where $y_{(1)} < y_{(2)} < \cdots$ denote the distinct event times, $d_{(l)}$ is the number of events at $y_{(l)}$, and $R(y_{(l)})$ is the risk set at $y_{(l)}$ containing all individuals still susceptible to the event at $y_{(l)}$.

I suspect that the slight difference tomight be due to the approximation of the partial likelihood in coxph() due to ties in the data...

Apparently, basehaz() actually computes a cumulative hazard rate, rather than the hazard rate itself. The formula is a follows: $$ \hat{H}_0(t) = \sum_{y_{(l)} \leq t} \hat{h}_0(y_{(l)}), $$ with $$ \hat{h}_0(y_{(l)}) = \frac{d_{(l)}}{\sum_{j \in R(y_{(l)})} \exp(\mathbf{x}^{\prime}_j \mathbf{\beta})} $$ where $y_{(1)} < y_{(2)} < \cdots$ denote the distinct event times, $d_{(l)}$ is the number of events at $y_{(l)}$, and $R(y_{(l)})$ is the risk set at $y_{(l)}$ containing all individuals still susceptible to the event at $y_{(l)}$.

I suspect the slight difference to be due to the approximation of the partial likelihood in coxph() due to ties in the data...

Apparently, basehaz() actually computes a cumulative hazard rate, rather than the hazard rate itself. The formula is as follows: $$ \hat{H}_0(t) = \sum_{y_{(l)} \leq t} \hat{h}_0(y_{(l)}), $$ with $$ \hat{h}_0(y_{(l)}) = \frac{d_{(l)}}{\sum_{j \in R(y_{(l)})} \exp(\mathbf{x}^{\prime}_j \mathbf{\beta})} $$ where $y_{(1)} < y_{(2)} < \cdots$ denote the distinct event times, $d_{(l)}$ is the number of events at $y_{(l)}$, and $R(y_{(l)})$ is the risk set at $y_{(l)}$ containing all individuals still susceptible to the event at $y_{(l)}$.

I suspect that the slight difference might be due to the approximation of the partial likelihood in coxph() due to ties in the data...

added 99 characters in body
Source Link
ocram
  • 22.4k
  • 5
  • 85
  • 83

Let's try this. (The following code is there for illustration only and is not intended to be very well written.)

Let's try this.

Let's try this. (The following code is there for illustration only and is not intended to be very well written.)

added 132 characters in body
Source Link
ocram
  • 22.4k
  • 5
  • 85
  • 83
Loading
Source Link
ocram
  • 22.4k
  • 5
  • 85
  • 83
Loading