Skip to content

Instantly share code, notes, and snippets.

@mattansb
Last active December 1, 2019 00:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattansb/ba2f1b9dbd9b4479ae31826a6cc32a80 to your computer and use it in GitHub Desktop.
Save mattansb/ba2f1b9dbd9b4479ae31826a6cc32a80 to your computer and use it in GitHub Desktop.
make qq plots with worm holes
# From https://twitter.com/mattansb/status/1199936633413476358
qq_worm_plot <- function(x, distribution = "norm", ...) {
d <- match.fun(paste0("d",distribution))
p <- match.fun(paste0("p",distribution))
q <- match.fun(paste0("q",distribution))
dparams <- list(...)
if (!require(ggplot2)) stop("Need 'ggplot2' to use this function")
worm_ci_UL <- function(nx){
1.96 * sqrt(p(nx, ...) * (1 - p(nx, ...)) / length(nx)) / d(nx, ...)
}
worm_ci_LL <- function(nx){
-1.96 * sqrt(p(nx, ...) * (1 - p(nx, ...)) / length(nx)) / d(nx, ...)
}
ggplot(mapping = aes(sample = x)) +
stat_qq(aes(y = stat(sample - theoretical)), distribution = q, dparams = dparams) +
stat_qq(aes(y = stat(worm_ci_UL(theoretical))), geom = "line", distribution = q, dparams = dparams) +
stat_qq(aes(y = stat(worm_ci_LL(theoretical))), geom = "line", distribution = q, dparams = dparams) +
labs(x = paste0("theoretical ", distribution, " quantiles")) +
NULL
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment