Trying to make a R function for a brms::brm model

I am trying to make a function for a non-linear sigmoid brms::brm model I commonly use for re-usability sake, but I am getting a couple of errors for when I want to fix one of my terms in the equation. For example, below I want to fix t = 1000.
When I run the model to have all the variables predicted, it runs fine, but as soon as I try to have one of the variables fixed it says:
"Error: The following variables can neither be found in ‘data’ nor in ‘data2’: ‘t’ "
then when I uncomment d_2 = list(t = 1000) I get:
“Error in get(covars[i], data) : object ‘t’ not found”

(The data is not mine to share, but I can edit it if it is necessary to receive the help I need)
(I am relatively new to R and Stan)

a_model <- function(b, 
                     t,
                     h, 
                     i,
                     predict_equation,
                     d_1,
                     #d_2,
                     priors, 
                     inits) {
  brms::brm(
    formula = brms::brmsformula(
      normalized_measurement ~ (b + (t - b) / (1 + 10^((i - log_val)*h))),
      predict_equation, 
      nl = TRUE),
    data = d_1,
    #data2 = d_2,
    prior = priors,
    inits = inits,
    iter = 8000,
    control = list(adapt_delta = 0.99, max_treedepth = 10)
  )
}

predict_eq = i + h + b ~ 0 + G

priors = c(brms::prior(normal(-90,20), nlpar = "i"),
           brms::prior(normal(-100,50), nlpar = "h"),
           brms::prior(normal(0,5), nlpar = "b")
           )

inits_list = list(i = -90, h = -100, b = 0)
inits = list(inits_list, inits_list, inits_list, inits_list)

a_model(b = b,
         t = 1000,
         h = h,
         i = i,
         predict_equation = predict_eq, 
         d_1 = my_data,
         #d_2 = list(top = 1000),
         priors = priors, 
         inits = inits)

Thank you in advance!

1 Like

Can you post a small data sample so that we can run your code? It can be fake data, as long as it reproduces the problems you’re seeing when run with your code.

1 Like

Hi,
not sure if it addresses your problem, but note that you can fix a parameter to a constant by giving it a constant() prior, i.e. prior(constant(5), nlpar = "i") should fix i = 5.

This is very helpful and works with what I am trying to do! Thank you!