How to have conditional_effects plots display actual category values in facet labels, rather than generic integers

If I specify a categorical variable for the conditions argument in conditional_effects, the resulting plot is facetted by the levels of the categorical variable. However, rather than using the actual value labels in the facet strips, conditional_effects returns a plot with generic integer values for the facet strip labels. I can post-process the plot to use the actual value labels, but I’m hoping there’s a way to have conditional_effects return a properly labeled plot and avoid the extra steps. Here’s a reproducible example:

library(tidyverse)
theme_set(theme_bw())
library(brms) 

fit1 = brm(formula=Petal.Width ~ Species + Sepal.Width, data=iris,
           backend="cmdstanr")

Here’s the plot directly returned by conditional_effects. Note the generic integer values in the strip labels:

cond.eff = conditional_effects(fit1, effects="Sepal.Width",
                               conditions=data.frame(Species=unique(iris$Species)))

cond.eff

In the output below, I can see in the data frame returned by conditional_effects that the cond__ column was used for the facet labels. But since the Species column is included in the data frame, I can switch to Species as the facet column, which I’ve done below. However, I’m hoping there’s a way to have conditional_effects do this and avoid the extra post-processing steps.

str(cond.eff)
#> List of 1
#>  $ Sepal.Width:'data.frame': 300 obs. of  9 variables:
#>   ..$ Sepal.Width: num [1:300] 2 2.02 2.05 2.07 2.1 ...
#>   ..$ Petal.Width: num [1:300] 1.2 1.2 1.2 1.2 1.2 ...
#>   ..$ Species    : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ cond__     : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
#>   ..$ effect1__  : num [1:300] 2 2.02 2.05 2.07 2.1 ...
#>   ..$ estimate__ : num [1:300] -0.159 -0.152 -0.145 -0.138 -0.132 ...
#>   ..$ se__       : num [1:300] 0.0658 0.065 0.0641 0.0631 0.0622 ...
#>   ..$ lower__    : num [1:300] -0.295 -0.286 -0.277 -0.268 -0.259 ...
#>   ..$ upper__    : num [1:300] -0.02319 -0.01852 -0.01302 -0.0085 -0.00348 ...
#>   ..- attr(*, "effects")= chr "Sepal.Width"
#>   ..- attr(*, "response")= chr "Petal.Width"
#>   ..- attr(*, "surface")= logi FALSE
#>   ..- attr(*, "categorical")= logi FALSE
#>   ..- attr(*, "ordinal")= logi FALSE
#>   ..- attr(*, "points")='data.frame':    150 obs. of  4 variables:
#>   .. ..$ Sepal.Width: num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#>   .. ..$ resp__     : num [1:150] 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#>   .. ..$ cond__     : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
#>   .. ..$ effect1__  : num [1:150] 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#>  - attr(*, "class")= chr "brms_conditional_effects"
pcond.eff = plot(cond.eff, ask=FALSE, plot=FALSE)[[1]] 

pcond.eff + facet_wrap(~Species)

Created on 2022-04-29 by the reprex package (v2.0.1)

@paul.buerkner do you have any suggestions about this?

I recommend preprocessing your newdata via make_conditions.

1 Like