0

I am trying to replicate some analyses that someone performed in Stata. They used a function (or program as they are called in Stata?) called "stexpect3" to generate a "smoothed hazard plot" like this. Wondering if anyone can point me in the direction of how to do this in R? I have come across a few options such as "bshazard" but they don't seem to give me what I am after.
enter image description here

For example, using the lung dataset, I get a plot like this with "hazard rate" on the y-axis. How would I generate the equivalent of the above plot using this lung dataset as an example?

lung <- lung
fit<-bshazard(Surv(time, status) ~ sex,data=lung)
plot(fit)

enter image description here

krtbris
  • 344
  • 1
  • 9

1 Answers1

0

Essentially, you need to create a data frame to hold the variables from the fit result. Then, use ggplot2. Below is the code

library(ggplot2)

df_surv <- data.frame(time = fit$time,
                      hazard = fit$hazard,
                      lower.ci = fit$lower.ci,
                      upper.ci = fit$upper.ci)

p <- ggplot(df_surv, aes(x = time, y = hazard)) +
  geom_line(color = "firebrick4") + 
  geom_segment(aes(x = 0, xend = max(time), y = 0, yend = 0), 
               linetype = 2) +
  geom_ribbon(aes(ymin = lower.ci, ymax = upper.ci), 
              alpha = 0.2,
              fill = "firebrick2",
              color = "firebrick3") + 
  theme_classic()

p 

Well, you could modify the color as needed. color of geom_line is for the central line, color of geom_ribbon is for the border of ribbon, its fill for the fill color.

enter image description here

yuw444
  • 380
  • 2
  • 10