Let's say I have a 'kidney catheter' data set. I'm actually trying to model a survival curve using a Cox model. If I consider a Cox model
h(t,Z)=h0(t)exp(b'Z)
I need the estimate baseline hazard. By using built in survival package R code basehaz(), I can easily do it like this
R code
library(survival)
data(kidney)
fit<-coxph(Surv(time, status) ~ age , kidney)
basehaz(fit)
But if I want to write a step by step function of baseline hazard for given estimate of parameter "b", how can I proceed? I am trying like,
R code
bhaz <- function(beta, time, status, x){
data<-data.frame(time,status,x)
data<-data[order(data$time), ]
dt<-data$time
k<-length(dt)
risk<-exp(data.matrix(data[,-c(1:2)]) %*% beta)
h<-rep(0,k)
for(i in 1:k) {
h[i] <- data$status[data$time==dt[i]]/sum(risk[data$time >= dt[i]])
}
return(data.frame(h, dt))
}
h0 <- bhaz(fit$coef, kidney$time, kidney$status, kidney$age)
But, it does not give the same result like basehaz(fit). What is the problem?