Coder Social home page Coder Social logo

ggtails's Introduction

ggtails

The goal of ggtails is to make the use of fat tails specific plots as easy as possible within the usual ggplot2 framework. To do so, I took the computational steps in the great evir package and turned them into ggplot2 stats.

Check this blogpost for an intuitive explanation of these types of plots.

Installation

# install.packages("devtools")
devtools::install_github("David-Salazar/ggtails")

Example

For example, you can create a zipf plot for different random variables in the same way as would use stat_qq()

library(ggtails)
library(tidyverse)

gaussian <- rnorm(10000)
lognormal <- rlnorm(10000)
exponential <- rexp(10000)
pareto <- 1/(runif(10000))^(1/1.16) # inverse transform sampling

tibble(sim = 1:10000, lognormal, exponential, pareto) %>% 
  pivot_longer(-sim, names_to = "random_variable", values_to = "value") %>% 
  ggplot(aes(sample = value, color = random_variable)) +
  stat_zipf(alpha = 0.1) +
  scale_x_log10() +
  scale_y_log10() +
  theme_minimal() +
  labs(title = "Zipf plot in ggplot2 workflow",
       x = "Log(x)",
       y = "Log(Survival(x))")

In the same way, we can create a mean excess plot:

tibble(sim = 1:10000, gaussian, lognormal, exponential, pareto) %>% 
  pivot_longer(-sim, names_to = "random_variable", values_to = "value") %>% 
  ggplot(aes(sample = value)) +
  stat_mean_excess(alpha = 0.1) +
  facet_wrap(~random_variable, scales = "free") +
  theme_minimal() +
  labs(title = "Mean Excess plot works with facetting!")

Finally, we can perform a visual examination of fat-tailedness by examining the convergence of moments with the max-to-sum plots. For finite moments, the ratio should converge to 0.

tibble(sim = 1:10000, gaussian, lognormal, exponential, pareto) %>% 
  pivot_longer(-sim, names_to = "random_variable", values_to = "value") %>% 
  ggplot(aes(sample = value)) +
  stat_max_sum_ratio_plot(p = 4) +
  facet_wrap(~random_variable) +
  theme_minimal()

ggtails's People

Contributors

david-salazar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.