# How can a hazard function be negative?

I've been reading through Survival Analysis in R, an ebook that I found for independent study, and have run into a point of confusion. They define the hazard function as follows:

where T is whichever of the event time or the right censoring time comes first. The numerator is a probability, and thus nonnegative, and I assume we should take δ to be positive because P(t < T < t + δ) = 0 for δ ≤ 0. It seems, then, that the hazard function should be nonnegative. However, in Chapter 3 on parametric modeling, they show the following example of an observed instantaneous hazard function:

How does this align with the hazard function definition? What is the interpretation of the hazard function being negative?

• Both "estimates" as well as various intervals (such as confidence and prediction bands) can have unrealistic values. What the context of this figure shows is that you are actually asking how software (that knows nothing about your data) can smooth positive data into negative territory. (The culprit is the line geom_smooth(color = "red", method = "loess", formula = "y ~ x") that computed the red curve and the gray band from the data.)
– whuber
Commented Jul 15 at 20:04
• The simplest example that comes to my mind: think of what may happen if you construct a confidence interval for a proportion close to 0 or 1 using normal approximation. Commented Jul 17 at 9:59

The hazard certainly can't become negative, as you rightly point out.

The problem is the way that plot is constructed. Here's the associated code, which assumes that you have loaded the ggplot2 package and have the epiR package available:

epiR::epi.insthaz(km_fit) %>%
ggplot(aes(x = time, y = hest)) +
geom_smooth(color = "red", method = "loess", formula = "y ~ x") +
theme_light() +
labs(title = "Kaplan-Meier Hazard Function Estimate",
x = "Time", y = "Instantaneous Hazard")


The epi.insthaz() function does not return a negative value. It returns hazards at event times; the hazard is identically 0 between event times. The cumulative hazard function in Figure 3.4 shows that the last few observations were right-censored times (vertical marks on the plot), so that the hazard at late observation times (after about Time = 875) is 0.

But the geom_smooth() function used to generate the smooth plot had no way to know the nature of the data returned by epi.insthaz(). It just tried to fit a smooth curve to the data that it was fed. With no events after about Time = 875, and thus a hazard of 0 beyond that, you see the problem you identified in that plot.

It would have been better to limit the plotted hazard estimates (and smoothing) to values at times no later than the last event. In general, you should be very cautious with survival data beyond the last event time.

A warning about epi.insthaz()

Later playing with this example showed that this function returns instantaneous hazard estimates at all observation times, not just those with events. If there was no event, then the reported hazard is 0.

This attempt to display a smooth baseline hazard is thus even more erroneous, as the smoothing tries to incorporate the 0-hazard values at all intermediate observation times, not just after the last event. Yet those right-censored observation times provide no information about the true baseline hazard, which is necessarily underestimated after the first right-censoring time.

Unfortunately, the help page for the epi.insthaz() function includes some similar code (with the warning "Not run"), evidently without taking that problem into account. The example nicely called out by this question evidently drew on that help page without considering the implications.

• +1. Lowess (and other local smoothers) can also interpolate unrealistic or impossible values between data points, depending on the smoothing parameter. The lesson here (for the author of the book!) is that the analyst needs to understand and control what their software does, rather than relying on its default behavior to produce meaningful results.
– whuber
Commented Jul 15 at 20:51