rformulabrms

R: pass a character object with a comma inside it to a formula


I am trying to build a function in which I have to use a comma separator between two strings to pass as formula (brmsformula needs this format to specify sigma), but I get an error because the formula does not allow a , inside it:

library(brms)
term1 <- "mpg ~ 0 + Intercept + cyl"
term2 <- "sigma ~ 0 + cyl"

formula0 <- brms::brmsformula(paste( term1, term2, sep = ', ' ))

Error in str2lang(x) : <text>:1:26: unexpected ',' 1: mpg ~ 0 +
Intercept + cyl,
                ^

I know I can simply pass it from terminal as:

> brms::brmsformula( mpg ~ 0 + Intercept + cyl, sigma  ~ 0 + cyl )
mpg ~ 0 + Intercept + cyl 
sigma ~ 0 + cyl

but I need to create it inside a function that can have (or not) sigma specifications, thus pasting these two terms (if sigma is specified).

I have googled around and looked at similar posts (like this and this) but these do not apply to my case. Does anybody know how to achieve this?


Solution

  • As Friede smartly suggested, looks like the solution was simply to pass multiple arguments to the formula:

    > formula0 <- brms::brmsformula( term1, term2 )
    
    mpg ~ 0 + Intercept + cyl 
    sigma ~ 0 + cyl