Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Illustration of negative binomial.
#' Graph of number of tries needed to obtain K successes.
#' @param K number of studies.
#' @return NULL.
plotter = function(K){
kk = 0:(K*70)
plot(kk + K, dnbinom(kk, K, 0.05), bty = "l", type = "b", pch = 20,
xlab = "Number of studies",
ylab = "Probability",
main = paste0("Number of studies before ", K, " successes"))
abline(v = 0.95/0.05*K + K, lty = 2)
abline(v = floor(0.95/0.05*(K-1) + K), lty = 2, col = "red")
abline(v = qnbinom(0.5, K, 0.05) + K, lty = 2, col = "blue")
legend("topright", col = c("black", "blue", "red"),
lty = c(2, 2, 2), legend = c(
paste0("Mean: ", 0.95/0.05*K + K),
paste0("Mode: ", floor(0.95/0.05*(K-1) + K)),
paste0("Median: ", qnbinom(0.5, K, 0.05) + K)
), bty = "n")
}
plotter(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment