Conditional_effects plot in brms plotting question (plot logit values; using int_conditions)

Short summary of the problem

Hello! I’ve just started using brms, and I’m really amazed at how good it is, especially compared other other MLM packages that I’m unfortunately more used to. I’ve found the documentation for conditional_effects very useful, but I still have not managed to figure out a couple of questions and would really appreciate any help.

With the conditional_effects() command, I’m trying to plot interaction effects of a logit regression. The predictor variable (VIOLENCE) that comes on the x axis is binary, and the moderating variable (STR.factor) is a factor with five levels (“0”,“1”,“2”,“3”,“4”). I’ve attached a small dataset and a simple regression for illustration:

lv1_violxstr <- brm(SUI_CON ~ VIOLENCE*STR.factor, data=smalldat, family=‘bernoulli’)

smalldat.csv (16.5 KB)

I want to
1. Plot in a log-odds scale and not in a probability scale

2. Use just some of the values of the moderating variables. For continuous moderators, I could just use the int_conditions option, like int_conditions = list(x1=c(-0.12, 0.12)). However, for my factor moderator variable STR.factor, I have not been able to make any changes to the plot by adding int_conditions = list(STR.factor=c(“0”, “4” )). I wonder why?

3 Finally, how could I change the moderator variable name shown in the legend?


For all three questions, I’ve tried to manipulate the data.frame obtained by

conditional_effects(lv1_violxstr_dummy,“VIOLENCE:STR.factor”)$VIOLENCE:STR.factor

, but just can’t get it to plot it in the way I described.

Thanks a lot!

  • Operating System: Windows 10
  • brms Version: 2.12.0

Hi Tay,

  1. works in the dev version of brms from github my setting method = "posterior_linpred".
  2. should have work but did not. This should now be fixed in the dev version. Thanks for making me aware of it!
  3. in the doc of ?conditional_effects, there are examples of how to extract the underlying ggplot object, which you can then amend to your needs using ggplot2.

Paul

Thanks a lot Paul for the reply (and for the useful update). I’ve tried both 1. and 2. and they work perfectly.

If I may ask one more thing: I think I’ve run into other problems with the dev version.

I downloaded the dev version from your github
remotes::install_github(“paul-buerkner/brms”)

, and brms seems to return errors whenever there is a random effect (but no problem when there are only fixed effects). So when I do, for example,

lv1_violxstr <- brm(SUI_CON ~ VIOLENCE*STR.factor + (1 | CLASS), data=bigdat1, family=‘bernoulli’, cores=4, chains=4)
Error in str2lang(re_parts$rhs[i]) : could not find function “str2lang”

here is the traceback

split_re_terms(re_terms)
9.
terms_re(.x1) at #1
8.
eval(expr, envir, …)
7.
eval(expr, envir, …)
6.
eval2(call, envir = args, enclos = parent.frame())
5.
do_call(paste0(“terms_”, t), list(formula))
4.
terms_lf(dpar_forms[[dp]])
3.
brmsterms.brmsformula(formula)
2.
brmsterms(formula)
1.
brm(SUI_CON ~ VIOLENCE * STR.factor + (1 | CLASS), data = bigdat1,
family = “bernoulli”, cores = 4, chains = 4)


The same goes for conditional_effects() when I load a brmsfit object that has random effects (but no problem with those with only fixed effects):

load(“brm_sadxses.Rdata”) #this is a random intercept model.
conditional_effects(brm_sadxses,“SAD:SES”)
Error in str2lang(re_parts$rhs[i]) : could not find function “str2lang”

split_re_terms(re_terms)
15.
terms_re(.x1) at #1
14.
eval(expr, envir, …)
13.
eval(expr, envir, …)
12.
eval2(call, envir = args, enclos = parent.frame())
11.
do_call(paste0(“terms_”, t), list(formula))
10.
terms_lf(dpar_forms[[dp]])
9.
brmsterms.brmsformula(x$formula)
8.
brmsterms(x$formula)
7.
withCallingHandlers(expr, warning = function(w) invokeRestart(“muffleWarning”))
6.
base::suppressWarnings(expr)
5.
SW(brmsterms(x$formula))
4.
restructure_v2(x)
3.
restructure(x)
2.
conditional_effects.brmsfit(brm_sadxses, “SAD:SES”)
1.
conditional_effects(brm_sadxses, “SAD:SES”)

I’ve tried this on two different Window 10 PCs but got the same errors. Do you have any idea why this might be?

Thanks a lot!
Tay

str2lang is a base R function so I don’t see why this couldn’t be found. Which R version are you using?
What happens if you try base::str2lang?

If the error persists, can you provide a minimal reproducible example? Something where I don’t have to load specific data but can just run a script would be perfect.

Hmm… I can’t seem to figure it out… Here is a really simple intercept-only regression script that I tried

set.seed(432)
nschool = 20
nstudent = 500
interc = 1

school = rep(LETTERS[1:nschool], each=nstudent)
student = c(1:(nschool * nstudent))
schooleff = rep(rnorm(nschool, 0, 1.5), each=nstudent)
studenteff = rnorm(nschool * nstudent, 0, 1)

dat = data.frame(school, schooleff, student, studenteff)
dat$outcome = with(dat, interc + schooleff + studenteff)

fit_brm <- brm(outcome~1+(1|school), data=dat, cores=3, chains=3)

Error in str2lang(re_parts$rhs[i]) : could not find function “str2lang”

I’m really finding it strange… I’ve removed the brms package in all my computers and reinstalled the dev version, and I keep getting the same message. When I reinstall the CRAN version, it starts to work again normally… Do you (or anybody) get the same error message?

Also, (forgive the noob question) how can I try base::str2lang in the above code?

Thanks so much again,
Tay

Thanks! Just type base::str2lang in the console and see if it can find the function. Also, again, what is your R version?

Thanks, and sorry I forgot to check the R version. It is version 3.5.3 (2019-03-11).

I checked base::str2lang and

base::str2lang
Error in get(name, envir = ns, inherits = FALSE) :
object ‘str2lang’ not found

maybe I should reinstall R, or perhaps use a different version?

Can you try installing R3.6 or higher and see if it solves the problem?

Oki, that could very well be the problem. I’ll do it in a minute and report back.

Whew, I ran into some rather silly trivial troubles updating R and the packages, so it took me some time. Anyway, I can confirm that I did not run into this problem in R 3.6.2 and 3.6.3, and everything just works fine. Thanks again for helping me solve the issue Paul.

Best,
Tay

Addendum: Just in case you weren’t aware, the option “posterior_linpred” is missing in ?conditional_effects of the dev. version.

Thanks. The doc should be updated now.