paul-buerkner / brms

brms R package for Bayesian generalized multivariate non-linear multilevel models using Stan
https://paul-buerkner.github.io/brms/
GNU General Public License v2.0
1.27k stars 183 forks source link

Passing multiple vint aterms when using custom family? #1044

Closed janfreyberg closed 3 years ago

janfreyberg commented 3 years ago

Hello,

I am in the process of defining a custom family that uses two aterms, outcome and last_outcome.

These terms are used to construct additional variables in the family function:

scode <-

   // this is wiener function as comes with brms:
   real wiener_diffusion_lpdf(real y, int decision, real alpha, 
                              real tau, real beta, real delta) {
      if (decision == 1) {
        return wiener_lpdf(y | alpha, tau, beta, delta);
      } else {
        return wiener_lpdf(y | alpha, tau, 1 - beta, - delta);
      }
   }
   // Custom 3-level HDDM function:
   real three_level_ddm_lpdf(
     real rt,
     // wiener process params:
     real drift_rate, real boundary_sep, real ndt, real bias,
     // binocular rivalry variables:
     int outcome, int last_outcome) {
       int binarised_outcome = outcome > last_outcome;
       real boundary_multiplier = fabs(outcome - last_outcome);
       return wiener_diffusion_lpdf(
         rt | binarised_outcome, boundary_sep * boundary_multiplier, ndt, bias, drift_rate
       );
   }
stanvars <- stanvar(scode = stan_functions, block = "functions")
three_level_ddm <- custom_family(
  "three_level_ddm", dpars = c(
    "mu", "bias", "boundarysep", "ndt"),
  links = c(
    "identity", "logit", "log", "log"),
  lb = c(
    NA, 0.0, 0.0, 0.00001),
  ub = c(
    NA, 1.0, NA, NA),
  type = "real",
  vars = c("vint1[n]", "vint2[n]")
)

model_code <- make_stancode(
  rt | vint(response) + vint(last_pressed) ~ 0 + group,
  stan_df,
  family=three_level_ddm,
  stanvars = stanvars
)

This results in the error: Error: Each addition argument may only be defined once.

It works if I instead use vreal for one of the addition terms (and change the custom family accordingly). However, the fact that the variables are called vint1 made me assume that it's possible to provide multiple custom vint aterms. Is there something I'm missing to supply multiple integer aterms?

It's not urgent since vreal seems to work, but would be curious to know if it can be made to work.

Thanks!

PS: I am using brms version 2.13.5

paul-buerkner commented 3 years ago

Pass multiple arguments to vint at once.

Jan Freyberg notifications@github.com schrieb am Fr., 20. Nov. 2020, 16:56:

Hello,

I am in the process of defining a custom family that uses two aterms, outcome and last_outcome.

These terms are used to construct additional variables in the family function:

scode <-

// this is wiener function as comes with brms: real wiener_diffusion_lpdf(real y, int decision, real alpha, real tau, real beta, real delta) { if (decision == 1) { return wiener_lpdf(y | alpha, tau, beta, delta); } else { return wiener_lpdf(y | alpha, tau, 1 - beta, - delta); } } // Custom 3-level HDDM function: real three_level_ddm_lpdf( real rt, // wiener process params: real drift_rate, real boundary_sep, real ndt, real bias, // binocular rivalry variables: int outcome, int last_outcome) { int binarised_outcome = outcome > last_outcome; real boundary_multiplier = fabs(outcome - last_outcome); return wiener_diffusion_lpdf( rt | binarised_outcome, boundary_sep * boundary_multiplier, ndt, bias, drift_rate ); }

stanvars <- stanvar(scode = stan_functions, block = "functions")three_level_ddm <- custom_family( "three_level_ddm", dpars = c( "mu", "bias", "boundarysep", "ndt"), links = c( "identity", "logit", "log", "log"), lb = c( NA, 0.0, 0.0, 0.00001), ub = c( NA, 1.0, NA, NA), type = "real", vars = c("vint1[n]", "vint2[n]") ) model_code <- make_stancode( rt | vint(response) + vint(last_pressed) ~ 0 + group, stan_df, family=three_level_ddm, stanvars = stanvars )

This results in the error: Error: Each addition argument may only be defined once.

It works if I instead use vreal for one of the addition terms (and change the custom family accordingly). However, the fact that the variables are called vint1 made me assume that it's possible to provide multiple custom vint aterms. Is there something I'm missing to supply multiple integer aterms?

It's not urgent since vreal seems to work, but would be curious to know if it can be made to work.

Thanks!

PS: I am using brms version 2.13.5

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/paul-buerkner/brms/issues/1044, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADCW2AAGBXVRRAODEIGHFALSQ2GR5ANCNFSM4T46I3GA .

janfreyberg commented 3 years ago

Ah, cool, that makes sense. Thanks for the help!