Skip to content

Instantly share code, notes, and snippets.

@statwonk
Last active August 11, 2019 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statwonk/cb9291c63fc4ae19670732b7d85617b8 to your computer and use it in GitHub Desktop.
Save statwonk/cb9291c63fc4ae19670732b7d85617b8 to your computer and use it in GitHub Desktop.
Risk adds up. This code piece answers, "how quickly?" https://twitter.com/statwonk/status/1160542394544267265
library(tidyverse)
expand.grid(
risk = seq(0.001, 0.02, 0.001),
units_of_exposure = seq_len(24*7)
) %>% as_tibble() %>%
mutate(total_risk = map2_dbl(risk, units_of_exposure, ~ 1 - (1 - .x)^(.y))) %>%
ggplot(aes(x = risk, y = units_of_exposure)) +
geom_raster(aes(fill = total_risk), alpha = 0.90) +
scale_fill_gradient2(name = "Total chance",
low = "white", mid = "white", high = "purple4", midpoint = 0.5,
labels = scales::percent, limits = c(0, 1)) +
scale_y_continuous(breaks = seq(1, 24*7, 24) - 1) +
theme(panel.grid.major = element_line(color = "black")) +
ylab("Units of exposure") + xlab("Risk per unit") +
scale_x_continuous(labels = scales::percent)
@statwonk
Copy link
Author

statwonk commented Aug 11, 2019

The visualization shows that the risk in a given unit of time has a strong influence on how many units of exposure can be experienced before the event occur. It also shows how reduction of risk relates to the number of units of exposure we can take without experiencing the event.

Screen Shot 2019-08-11 at 8 19 17 AM

Screen Shot 2019-08-11 at 8 18 36 AM

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