Skip to content

Instantly share code, notes, and snippets.

@ivelasq
Created March 7, 2022 02:26
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 ivelasq/405d78a800216c0c420ba1684dad22b1 to your computer and use it in GitHub Desktop.
Save ivelasq/405d78a800216c0c420ba1684dad22b1 to your computer and use it in GitHub Desktop.
Demonstration of quantiles in R
set.seed(123)
dat <- rnorm(1001)
a <- quantile(dat, type = 1)
b <- quantile(dat, type = 2)
quantile(dat, type = 3)
quantile(dat, type = 4)
quantile(dat, type = 5)
quantile(dat, type = 6)
quantile(dat, type = 7)
quantile(dat, type = 8)
quantile(dat, type = 9)
results <- NULL;
for (i in 1:9){
tmp <- quantile(dat, type = i)
results <- rbind(results, tmp)
}
ggplot(df, aes(x, y)) +
geom_line() +
eom_ribbon(aes(ymin = 0, ymax = y, fill =
quant)) + scale_x_continuous(breaks = quantiles) + scale_fill_brewer(guide =
"none")
library(tidyverse)
set.seed(123)
quantile(x <- rpois(n = 25, lambda = 10))
quantAll <- function(x, prob){
t(vapply(1:9, function(typ)
quantile(x, prob = prob, type = typ),
quantile(x, prob, type = 1))) %>%
as_tibble()
}
p <- c(0, 0.2, 0.6, 0.8, 1)
(ps <- signif(quantAll(x, p), 4))
ps %>%
rownames_to_column("id") %>%
tidyr::pivot_longer(cols = 2:9,
names_to = "prob",
values_to = "value") %>%
ggplot(aes(x = value, y = id, group = id)) +
geom_point()
# - Final Demonstration
library(tidyverse)
set.seed(123)
x <- rpois(n = 11, lambda = 10)
purrr::map_df(1:9, ~ quantile(x, type = .))
quantiles_all
quantiles_all %>%
rownames_to_column("id") %>%
tidyr::pivot_longer(cols = 2:6,
names_to = "prob",
values_to = "value") %>%
ggplot(aes(x = value, y = id, group = id)) +
geom_point() +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment