n = 100  
vreme = sort(runif(n, 0, 100))  # simulacija vremena
status = sample(c(0, 1), n, replace = TRUE, prob = c(0.2, 0.8))  # 20% cenzurisanih

# broj dogadjaja (d)
vreme_unique = unique(vreme)
d = sapply(vreme_unique, function(t) sum(vreme == t & status == 1))

# broj prezivelih (R)
R = sapply(vreme_unique, function(t) sum(vreme >= t))

podaci = data.frame(vreme = vreme_unique, d = d, R = R)
head(podaci)

# Gausovo jezgro
gausovo_jezgro = function(t) {
  return((1 / sqrt(2 * pi)) * exp(- t^2 / 2))
}

# glatka ocena funkcije hazarda
hazard_glatka_ocena = function(t, x, d, R, h) {
  delta_lambda = d / R  # Delta H (y(i)) = d(i) / R(y(i))
  
  hazard = sum(gausovo_jezgro((t - x) / h) * delta_lambda) / h
  
  return(hazard)
}

# Vektorizacija funkcije
hazard_ocena_vec = Vectorize(hazard_glatka_ocena, vectorize.args = "t")

par(mfrow = c(1, 3))
for (h in c(2, 4, 12)) {
  hazard_vrednosti = hazard_ocena_vec(vreme_unique, vreme_unique, d, R, h = h)
  
  plot(vreme_unique, hazard_vrednosti, type = "l", col = "blue",
       xlab = "Vreme", ylab = "Ocena stope hazarda")
}