mjskay / tidybayes

Bayesian analysis + tidy data + geoms (R package)
http://mjskay.github.io/tidybayes
GNU General Public License v3.0
712 stars 59 forks source link

Accessing Nested Random Effects with 'spread_draws()' from BRMS Model #239

Closed JCruk closed 4 years ago

JCruk commented 4 years ago

I have fitted a BRMS model with nested random effects and am working on plotting with tidybayes.

get_variables(brms_model) gives me these variables:

[1] "b_Intercept"                                  "b_ConditionIntervention"                     
 [3] "b_SessionInitial"                             "b_SessionFollowMup"                          
 [5] "b_FacilitatorFacilitator2"                    "b_SessionInitial:FacilitatorFacilitator2"    
 [7] "b_SessionFollowMup:FacilitatorFacilitator2"   "sd_Condition__Intercept"                     
 [9] "sd_Condition:Group__Intercept"                "r_Condition[Control,Intercept]"              
[11] "r_Condition[Intervention,Intercept]"          "r_Condition:Group[Control_10,Intercept]"     
[13] "r_Condition:Group[Control_12,Intercept]"      "r_Condition:Group[Control_20,Intercept]"     
[15] "r_Condition:Group[Control_36,Intercept]"      "r_Condition:Group[Control_4,Intercept]"      
[17] "r_Condition:Group[Control_40,Intercept]"      "r_Condition:Group[Control_6,Intercept]"      
[19] "r_Condition:Group[Control_8,Intercept]"       "r_Condition:Group[Intervention_1,Intercept]" 
[21] "r_Condition:Group[Intervention_13,Intercept]" "r_Condition:Group[Intervention_17,Intercept]"
[23] "r_Condition:Group[Intervention_19,Intercept]" "r_Condition:Group[Intervention_29,Intercept]"
[25] "r_Condition:Group[Intervention_3,Intercept]"  "r_Condition:Group[Intervention_33,Intercept]"
[27] "r_Condition:Group[Intervention_41,Intercept]"

I can't seem to find the correct syntax to access the nested r_Condition:Group[] intercepts

brms_model %>%
 spread_draws(r_Condition:Group[Condition_Group, Intercept])

Give the following error and warnings:

Error in r_Condition:Group[Condition_Group, Intercept] : NA/NaN argument
In addition: Warning messages:
1: In r_Condition:Group[Condition_Group, Intercept] :
  numerical expression has 3 elements: only the first used
2: In r_Condition:Group[Condition_Group, Intercept] :
  numerical expression has 3 elements: only the first used

What is the correct way to spread draws over the nested random effects?

JCruk commented 4 years ago

I was able to find the solution myself. Back ticks around the variable corrected my call:

brms_model %>%
 spread_draws(`r_Condition:Group`[Condition_Group, Intercept])