Home > Enterprise >  Comparing 2 distribution using ggplot
Comparing 2 distribution using ggplot

Time:10-03

I want to compare two continuous distributions and their corresponding 95% quantiles. So I have found below example to implement such, where 2 distributions are placed in same place to facilitate the comparison

library(dplyr)
library(tidyr)
library(distributional)
library(ggdist)
library(ggplot2)
library(cowplot)

dists = tribble(
                ~ dist,      ~ args,
                "norm",      list(0, 1),
                "student_t", list(3, 0, 1)
            ) 

    dists %>%
        ggplot(aes(y = dist, dist = dist, args = args))  
        stat_dist_halfeye(aes(fill = stat(abs(x) < 1.5)))

However in above implementation, the boundaries are static for both distributions i.e. 1.5. How can I make it dynamic i.e. 95th quantiles for corresponding distributions?

Secondly, how can I use different distribution like Exponential (double) distribution in place of normal?

Any pointer will be very appreciated.

CodePudding user response:

The y variable is an index to the function used (the row of dists).
So you can leverage this to add some generalization.
But I chose to add more columns to dists, which gives you even more flexibility.

dists <- tribble(
  ~ dist,      ~ args,         ~ qfun,    ~ qfunargs, 
  "norm",      list(0, 2),     "qnorm",   list(p = 0.95, mean = 0, sd = 2),
  "student_t", list(3, 0, 1),  "qt",      list(p = 0.95, df = 3)
) 

dists %>%
  ggplot(aes(y = dist, dist = dist, args = args, qfun = qfun, qfunargs = qfunargs))  
  stat_dist_halfeye(aes(fill = stat(abs(x) < Map(do.call, qfun, qfunargs))))    
  labs(fill = "95% Confidence")

Which gives: enter image description here

I changed the sd of the normal, so you can see the effect.

  • Related