Last active
December 1, 2019 00:18
-
-
Save mattansb/ba2f1b9dbd9b4479ae31826a6cc32a80 to your computer and use it in GitHub Desktop.
make qq plots with worm holes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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