Closed awellis closed 5 years ago
There are a few directions for simplification. The first thing to note is that this:
draws_var <- m %>%
spread_draws(r_subject[subject,term]) %>%
spread(term, r_subject)
Is equivalent to using |
within spread_draws:
draws_var <- m %>%
spread_draws(r_subject[subject,term] | term)
Then the other thing is that spread_draws will do the join for you as long as you include all the variables you want in a single call, so you can combine the entire definition of draws
plus the mutate into this:
draws <- m %>%
spread_draws(b_Intercept, b_conditionB, r_subject[subject,term] | term) %>%
mutate(
alpha = b_Intercept + Intercept,
beta = b_conditionB + conditionB
)
That said, personally I probably wouldn't do this by munging parameters manually, since that tends to get messy when you do things like add more conditions. Instead, since what you need to derive alpha and beta are means conditional on subject
and condition
, I would consider using modelr::data_grid
combined with add_fitted_draws
to get the conditional means and go from there.
E.g. this:
AB %>%
modelr::data_grid(condition, subject) %>%
add_fitted_draws(m)
Which will give you a column .value
containing draws from the mean conditional on condition
and subject
. Then alpha and beta are something like (ungrouping and dropping .row
are needed because otherwise the spread
breaks):
AB %>%
modelr::data_grid(condition, subject) %>%
add_fitted_draws(m) %>%
ungroup() %>%
select(-.row) %>%
spread(condition, .value) %>%
mutate(
alpha = A,
beta = B - A
)
Does that help?
Hi, thanks for your reply, it's very helpful, and thanks in general for this package.
I haven't had much time to try this out, as in the end, I used brms::coef()
to extract the population + group level parameter estimates. I will try this out and get back to you.
Great, thanks! I'll close this for now, but please do let me know if you end up trying out this (or anything else) and it doesn't do what you need.
What is the obvious way to extract parameter estimates for each item of a grouping variable, if there are several population level parameters (and possibly interactions), and varying (group-level) effects? You explain how to do this for a varying intercept in your documentation, but it’s not clear to me how to generalise this.
For example (using brms), if my formula is
response ~ condition + (condition | subject)
the resulting variables will be
(There are only two subjects)
I can get the draws
but this seems seems a rather roundabout way of adding together the population and group level effects.
Is there some intended usage that I am missing?
Here is the R code (dataset makes no sense, just for illustration):
This is just an example, what I am really trying to do is to estimate d’ (signal detection) for each individual subject in a multilevel model.
Best wishes, Andrew