Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gather_emmeans_rvars() #326

Open
mattansb opened this issue Aug 6, 2024 · 0 comments
Open

Add gather_emmeans_rvars() #326

mattansb opened this issue Aug 6, 2024 · 0 comments

Comments

@mattansb
Copy link

mattansb commented Aug 6, 2024

Although easy to generate a data frame with rvars from {emmeans} (similar to what's possible with marginaleffects::posterior_draws()), it would be nice to have this option built in - it will fit nicely with the other *_rvars() alternatives to the *_draws() functions.

library(dplyr)
library(brms)
library(emmeans)
library(tidybayes)

set.seed(5)
n <- 10
ABC = tibble(
  condition = rep(c("A","B","C","D","E"), n),
  response = rnorm(n * 5, c(0,1,2,1,-1), 0.5)
)

m <- brm(response ~ condition, data = ABC,
         chains = 1, iter = 500)


eml <- emmeans(m, pairwise ~ condition)


gather_emmeans_draws(eml)
#> # A tibble: 3,750 × 7
#> # Groups:   condition, contrast, .grid [15]
#>    .grid   condition .chain .iteration .draw   .value contrast
#>    <chr>   <chr>      <int>      <int> <int>    <dbl> <chr>   
#>  1 emmeans A             NA         NA     1 -0.271   NA      
#>  2 emmeans A             NA         NA     2 -0.00172 NA      
#>  3 emmeans A             NA         NA     3 -0.167   NA      
#>  4 emmeans A             NA         NA     4  0.0142  NA      
#>  5 emmeans A             NA         NA     5  0.0934  NA      
#>  6 emmeans A             NA         NA     6 -0.0276  NA      
#>  7 emmeans A             NA         NA     7 -0.0110  NA      
#>  8 emmeans A             NA         NA     8  0.249   NA      
#>  9 emmeans A             NA         NA     9  0.162   NA      
#> 10 emmeans A             NA         NA    10  0.165   NA      
#> # ℹ 3,740 more rows
#> # ℹ Use `print(n = ...)` to see more rows


gather_emmeans_rvars <- function(object, ...) {
  tidybayes::nest_rvars(tidybayes::gather_emmeans_draws(object))
}

gather_emmeans_rvars(em_condition)
#> # A tibble: 15 × 4
#> # Groups:   condition, contrast, .grid [15]
#>    condition contrast .grid              .value
#>    <chr>     <chr>    <chr>          <rvar[1d]>
#>  1 A         NA       emmeans     0.1914 ± 0.18
#>  2 B         NA       emmeans     1.0134 ± 0.18
#>  3 C         NA       emmeans     1.8733 ± 0.18
#>  4 D         NA       emmeans     1.0220 ± 0.18
#>  5 E         NA       emmeans    -0.9509 ± 0.17
#>  6 NA        A - B    contrasts  -0.8220 ± 0.27
#>  7 NA        A - C    contrasts  -1.6819 ± 0.26
#>  8 NA        A - D    contrasts  -0.8306 ± 0.24
#>  9 NA        A - E    contrasts   1.1423 ± 0.25
#> 10 NA        B - C    contrasts  -0.8600 ± 0.25
#> 11 NA        B - D    contrasts  -0.0086 ± 0.26
#> 12 NA        B - E    contrasts   1.9643 ± 0.24
#> 13 NA        C - D    contrasts   0.8514 ± 0.25
#> 14 NA        C - E    contrasts   2.8242 ± 0.25
#> 15 NA        D - E    contrasts   1.9729 ± 0.26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant