tidybayes icon indicating copy to clipboard operation
tidybayes copied to clipboard

Add a multivariate model example to brms vignette

Open mjskay opened this issue 6 years ago • 3 comments

Should be able to demo fitted_draws and predicted_draws for multivariate models now.

mjskay avatar Nov 09 '18 02:11 mjskay

Hi Matthew, would you have a simple example on how to plot posterior predictions from a multivariate brms model (if it's already possible) ?

In the following code, add_fitted_draws seems to work, but add_predicted_draws does not seem to work...

library(tidyverse)
library(tidybayes)
library(brms)

set.seed(1234)

df <- data_frame(
    y1 = rnorm(20), y2 = rnorm(20, y1), y3 = rnorm(20, -y1),
    group = sample(x = c(-0.5, 0.5), size = 20, replace = TRUE)
    )

m <- brm(cbind(y1, y2, y3) ~ 1 + group, data = df)

df %>%
    gather(response, value, y1:y3) %>%
    modelr::data_grid(group) %>%
    #add_fitted_draws(m) %>%
    add_predicted_draws(m) %>%
    ggplot(aes(x = .value, y = group) ) +
    geom_halfeyeh() +
    facet_wrap(~.category, scales = "free")

It gives this error: "Error in fitted_predicted_draws_brmsfit_(predict, model, newdata, output_name = prediction, : argument "category" is missing, with no default"

EDIT: my bad, it works when we specify the "category" argument... for instance:

df %>%
    gather(response, value, y1:y3) %>%
    modelr::data_grid(group) %>%
    #add_fitted_draws(m) %>%
    add_predicted_draws(m, category = "response") %>%
    ggplot(aes(x = .prediction, y = group) ) +
    geom_halfeyeh() +
    facet_wrap(~response)

lnalborczyk avatar Nov 16 '18 11:11 lnalborczyk

No worries! That was, in fact, a bug (it should work even without specifying that argument) that should be fixed in the dev version now.

mjskay avatar Nov 16 '18 14:11 mjskay

Hi Matthew,

Thanks a lot for developing such an nice toolbox!! I love it.

I have an issue when combined tidybayes::stat_halfeye and grid_wrap.

Here is the situation. I used brms fitted two models, one for reaction time, one for accuracy (using d prime in signal detection theory). I can plot these two data separately.

For example, I used the following code for the RTs:

# get the posterior
exp1a_rt_p <- exp1a_rt_m1 %>%
  emmeans::emmeans( ~ ismatch | Valence) %>%
  tidybayes::gather_emmeans_draws() %>%
  dplyr::mutate(ismatch = ifelse(ismatch == 0, 'nonmatch', 'match'),
              Valence = factor(Valence, levels = c('Good', 'Neutral',  'Bad'))) %>%
  dplyr::filter(ismatch == 'match') %>%  # only plot the match trials
  #dplyr::rename() %>%
  dplyr::mutate(.value = (exp(.value))*1000, 
                DV = 'Reaction times (ms)') 

# plot the posteior
exp1a_rt_p %>%
  # rbind(exp1a_sdt_p) %>%
  # dplyr::mutate(DV=factor(DV, levels = c('Reaction times (ms)', 'd prime'))) %>%
  ggplot2::ggplot(aes(x = Valence, y = `.value`)) +
  tidybayes::stat_halfeye() +
  ggplot2::stat_summary(aes(group = NA), fun.y = mean, geom = "line") +
  facet_wrap( ~ DV, scales = "free_y", nrow = 1) +
  # facet_grid(cols = vars(label), scales = "free_y") +
  theme_classic() + 
  theme(axis.title.x = element_blank())

it worked: rt_alone

I hope to improve the plot a bit by plotting RTs and d prime together. So, I added an additional column DV to the RT and d prime dataframe gathered (and successfully plotted). Then, I used rbind to concat these two dataframes because they have the same colnames. Then I tried to plot them used grid_wrap, but only the d prime data showed the distribution. Now I have now clue.

exp1a_rt_p %>%
  rbind(exp1a_sdt_p) %>%
  dplyr::mutate(DV=factor(DV, levels = c('Reaction times (ms)', 'd prime'))) %>%
  ggplot2::ggplot(aes(x = Valence, y = `.value`)) +
  tidybayes::stat_halfeye() +
  ggplot2::stat_summary(aes(group = NA), fun.y = mean, geom = "line") +
  facet_wrap( ~ DV, scales = "free_y", nrow = 1) +
  theme_classic() + 
  theme(axis.title.x = element_blank())

rt_sdt

Thanks in advance.

hcp4715 avatar Nov 12 '20 02:11 hcp4715