Skip to content

Instantly share code, notes, and snippets.

@mmparker
Created February 21, 2013 18:24
Show Gist options
  • Save mmparker/5006892 to your computer and use it in GitHub Desktop.
Save mmparker/5006892 to your computer and use it in GitHub Desktop.
An experiment in how case rates change
# How do case rates vary as sensitivity changes, given
# fixed population
# fixed % positive
# fixed number of cases?
# Strings ain't factors
options(stringsAsFactors = FALSE)
library(ggplot2)
library(reshape2)
# Set up a sample dataset
x <- data.frame(n = 1000,
n.pos = 50,
n.neg = 950,
cases = 10,
pos.sens = seq(.5, 1, length.out = 100)
)
# Calculate the number of cases falling into each group
x$pos.cases <- with(x, cases * pos.risk)
x$neg.cases <- with(x, cases * (1 - pos.risk))
# Corresponding rates per 100k
x$overall.rate <- with(x, (cases / n) * 1e5)
x$pos.rate <- with(x, (pos.cases / n.pos) * 1e5)
x$neg.rate <- with(x, (neg.cases / n.neg) * 1e5)
# Melt it down for ggplot
xplot <- melt(x, measure.vars = c("overall.rate", "pos.rate", "neg.rate"))
ggplot(xplot, aes(x = pos.risk, y = value,
group = variable, color = variable)) +
geom_line(size = 1.5) +
scale_color_discrete("Rate Groups") +
labs(x = "Proportion of cases who tested positive",
y = "Cases per 100,000 dudes")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment