Git Product home page Git Product logo

tukeygrps's Introduction

tukeygrps

Lifecycle: experimental CRAN status

Tukeygrps provides simple wrapper functions for the annotation of (gg)plots according to statistical differences between groups determined by a parametric Tukey-HSD test from {stats} or a non-parametric Kruskal-Wallis test with Dunn’s test for multiple comparisons from {dunn.test}.

Installation

You can install tukeygrps from github using remotes:

install.packages("remotes")
library("remotes")
install_github("leonardblaschek/tukeygrps")

Examples

Parametric multiple comparisons like the Tukey HSD (honest significant differences) test shown in section 1 are only recommended in cases where the data fulfill all of the following conditions:

  • normally distributed
  • homoscedastic
  • independent within and between groups
  • equal in sample size

If you have strong evidence that they do not fulfill these conditions, consider a non-parametric method of comparison, like the Kruskal-Wallis test followed by Dunn’s multiple comparisons shown in section 2.

1. Parametric multiple comparisons

Here we use letter_groups() with stat_method = “tukey” to add letters to a geom_point plot. Alpha is set to 0.001, the letters are printed at y = 0, and there are no additional grouping variables.

library(tukeygrps)
library(tidyverse)

data(mpg)
head(mpg)
#> # A tibble: 6 x 11
#>   manufacturer model displ  year   cyl trans      drv     cty   hwy fl    class 
#>   <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr> <chr> 
#> 1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p     compa…
#> 2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p     compa…
#> 3 audi         a4      2    2008     4 manual(m6) f        20    31 p     compa…
#> 4 audi         a4      2    2008     4 auto(av)   f        21    30 p     compa…
#> 5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p     compa…
#> 6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p     compa…

tukey_letters <- letter_groups(mpg, hwy, class, "tukey", print_position = 0, stat_alpha = 0.001)

head(tukey_letters)
#>        class Letters hwy
#> 1    compact       a   0
#> 2 subcompact       a   0
#> 3    midsize       a   0
#> 4    2seater      ab   0
#> 5    minivan      bc   0
#> 6        suv      cd   0

ggplot() +
  geom_jitter(
    data = mpg,
    aes(
      x = class,
      y = hwy
    ),
    width = 0.1
  ) +
  geom_text(
    data = tukey_letters,
    aes(
      x = class,
      y = hwy,
      label = Letters
    )
  ) +
  coord_flip()

Here we split the statistical analysis by two grouping variables (“cut” and “color”), set the alpha to 0.05 and print the letters 0.5 standard deviations below the respective minimum value.

library(tukeygrps)
library(tidyverse)

data(diamonds)
diamonds <- diamonds %>%
  filter(cut %in% c("Ideal", "Premium", "Very Good") & color %in% c("D", "E", "F"))
head(diamonds)
#> # A tibble: 6 x 10
#>   carat cut       color clarity depth table price     x     y     z
#>   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
#> 1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
#> 2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
#> 3  0.22 Premium   F     SI1      60.4    61   342  3.88  3.84  2.33
#> 4  0.2  Premium   E     SI2      60.2    62   345  3.79  3.75  2.27
#> 5  0.32 Premium   E     I1       60.9    58   345  4.38  4.42  2.68
#> 6  0.23 Very Good E     VS2      63.8    55   352  3.85  3.92  2.48

tukey_letters <- letter_groups(
  diamonds,
  price,
  clarity,
  "tukey",
  cut,
  color,
  print_position = "below",
  print_adjust = 0.5,
  stat_alpha = 0.05,
)

head(tukey_letters)
#> # A tibble: 6 x 5
#> # Groups:   cut, color [1]
#>   cut       color Letters clarity  price
#>   <ord>     <ord> <chr>   <chr>    <dbl>
#> 1 Very Good D     a       IF       -591.
#> 2 Very Good D     b       SI2     -1349.
#> 3 Very Good D     c       SI1     -1287.
#> 4 Very Good D     c       VS2     -1405.
#> 5 Very Good D     bc      VVS1    -1304.
#> 6 Very Good D     c       VS1     -1405.

ggplot() +
  geom_jitter(
    data = diamonds,
    aes(
      x = clarity,
      y = price
    ),
    size = 1,
    width = 0.1,
    alpha = 0.25
  ) +
  geom_boxplot(
    data = diamonds,
    aes(
      x = clarity,
      y = price
    ),
    outlier.alpha = 0,
    fill = rgb(1, 1, 1, 0.5)
  ) +
  geom_text(
    data = tukey_letters,
    aes(
      x = clarity,
      y = price,
      label = Letters
    ),
    size = 3
  ) +
  facet_grid(cut ~ color) +
  coord_flip()

2. Non-parametric multiple comparisons

In case the above requirements for parametric tests are not met, we can fall back to the non-parametric Kruskal–Wallis test followed by Dunn’s test and p-value adjustment for multiple comparisons. Here we place the letter codes 0.5 standard deviations above the maximum values.

library(tukeygrps)
library(tidyverse)

data(diamonds)
diamonds <- diamonds %>%
  filter(cut %in% c("Ideal", "Premium", "Very Good") & color %in% c("D", "E", "F"))

kruskal_letters <- letter_groups(
  diamonds,
  price,
  clarity,
  "kruskal",
  cut,
  color,
  print_position = "above",
  print_adjust = 0.5,
  p_adj_method = "holm"
)

head(kruskal_letters)

ggplot() +
  geom_jitter(
    data = diamonds,
    aes(
      x = clarity,
      y = price
    ),
    size = 1,
    width = 0.1,
    alpha = 0.25
  ) +
  geom_boxplot(
    data = diamonds,
    aes(
      x = clarity,
      y = price
    ),
    outlier.alpha = 0,
    fill = rgb(1, 1, 1, 0.5)
  ) +
  geom_text(
    data = kruskal_letters,
    aes(
      x = clarity,
      y = price,
      label = Letters
    ),
    size = 3
  ) +
  facet_grid(cut ~ color) +
  coord_flip()

tukeygrps's People

Contributors

leonardblaschek avatar

Stargazers

 Tagh bio avatar

Forkers

dauntaun

tukeygrps's Issues

Error messages from missing data

In cases where groups only contain one observation, the Tukey-HSD function understandably throws an error. The error message is however not very helpful (Error in if (pvalue[k] <= 0.001) sig[k] <- "***" else if (pvalue[k] <= : missing value where TRUE/FALSE needed). The following workaround avoids the issue for now, but there should be a filtering step included in the function to either make the error message understandable or automatically filter out groups with n<2.

data <- data %>%
  group_by(Condition, Time) %>%
  filter(sum(!is.na(Value)) > 1) %>%
  group_by(Time) %>%
  filter(length(levels(droplevels(as.factor(Condition)))) > 1)

tukey_letters <- letter_groups(data, Value, Condition, 
  "tukey", Time, print_position = "above")

add "print_nudge" argument

It would be nice to have a "nudge" argument to adjust the position of the letters by absolute values, in addition to just a multiplier of the standard deviation.

positon "below" subtract SD

print_position = "below" should subtract rather than add the standard deviation as the lettering appears above the minimum values by default (negative numbers for print_adjust).

Return "p" values

It would be nice if the function also returned the numeric p-values as a separate column for us to compare the most significant differences.

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.