Posterior Draws for a Random Effect at a Different Level of a Predictor

I would like to know if there is a way to obtain posterior draws for a random effect (standard deviation) at a different level of a predictor variable when using brms. For example, if I have a model with an interaction between a continuous predictor and a binary predictor (coded 0, 1) as fixed effects, the effect of the continuous predictor and its corresponding random slope (as a standard deviation) for the continuous predictor would correspond to the effect of that variable when the binary predictor is 0. But, I also want to access the posterior draws for when the binary predictor is 1 (not 0). I would like to know if there is a way to do this without having to swap the coding of the binary predictor and then completely rerun the model.

Here is an example to make this more concrete, in case that helps.

Thank you!


# Example using the BLch9 dataset from the bmlm package:

# add a new within-person variable called cond (dummy coded)
mydf <- BLch9 %>% 
  cond = sample(c(0, 1), nrow(BLch9), 
replace = T, prob = c(.5, .5)))

# Fit model. Here, the reference group is conducting = 0
fit <- brm(freldis ~ x * cond + (x * cond | id), data= mydf, seed = 111, iter = 4000)
post_fit <- posterior_samples(fit)

I am wondering if there is a way to get the posterior samples for sd(x) (and possibly the model summary as well) for when cond = 1 without fitting the model again swapping the variable coding for cond. Basically, the equivalent of post_fit$sd_id__x, but for when cond is 1 instead of 0.

From looking at the documentation, it seems that extract_draws might work, and I have tried that:

fitdata <- subset(fit$data, cond == 1)
newdf <- data.frame(freldis = fitdata$freldis,
                    x = fitdata$x,
                    cond = 1,
                    id = fitdata$id)

fit_extract <- extract_draws(fit, newdata = newdf, allow_new_levels = T)

That runs, but I am having trouble figuring out the right code to pull out the information I want from fit_extract.

Thanks very much.

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggplot2_3.3.0 dplyr_0.8.3   brms_2.10.0   bmlm_1.3.11   Rcpp_1.0.2   

loaded via a namespace (and not attached):
 [1] Brobdingnag_1.2-6     gtools_3.8.1          StanHeaders_2.18.1-10 threejs_0.3.1         shiny_1.3.2          
 [6] assertthat_0.2.1      stats4_3.6.1          yaml_2.2.0            backports_1.1.5       pillar_1.4.2         
[11] lattice_0.20-38       glue_1.3.1            digest_0.6.22         promises_1.0.1        colorspace_1.4-1     
[16] htmltools_0.4.0       httpuv_1.5.1          Matrix_1

You can get something close to that SD. First, extract the individual coefficients via coef(..., summary = FALSE). Then, add intercept and slope on a per-draw basis to get the draws for the binary predictor being 1 Then compute the SD across levels of the grouping factor on a per-basis. Then summarise the draws, for instance, via posterior_summary.

1 Like

Thanks very much, I will try that!