Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Last active November 27, 2023 18:36
Show Gist options
  • Save alexpghayes/ed0670fc26f4fe844302394eb1cfaf71 to your computer and use it in GitHub Desktop.
Save alexpghayes/ed0670fc26f4fe844302394eb1cfaf71 to your computer and use it in GitHub Desktop.
library(tidyverse)

p_at_least_one_event <- function(p_event, periods = 50) {
  tibble(
    time = 1:periods,
    p_at_least_one_event = 1 - (1 - p_event)^(1:periods)
  )
}

tibble(
  p_event = seq(0.01, 0.1, by = 0.01)
) |> 
  mutate(
    curve = map(p_event, p_at_least_one_event),
    p_event = forcats::fct_inseq(as.factor(p_event))
  ) |> 
  unnest(c(curve)) |> 
  ggplot(aes(time, p_at_least_one_event, color = p_event)) +
  geom_line() +
  scale_color_viridis_d() +
  labs(
    title = "The cumulative risk of repeated small hazards",
    x = "Years",
    y = "Probability of at least one event",
    color = "Event chance\neach year"
  ) +
  theme_minimal(18)

Created on 2023-11-27 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment