I am tyring to create a custom family in brms using an lpdf that works fine in rstan. When I try to run the code below, I get an error that says “could not find function vreal”. I’m guessing that the problem is in the custom_family function below or in the specification in the brm() model, but I cannot find the solution to it anywhere else online or on this forum.

```
paretocounts <- custom_family(
"paretocounts", dpars = c("mu"),
links = c("identity"),
lb = -Inf, ub = Inf,
type = "real", vars = c("vint1[counts]",
"vreal1[xmin]",
"vreal2[xmax]")
)
stan_funs <- "
real paretocounts_lpdf(real x, real mu, real xmin, real xmax, real counts){
if(mu != -1)
return(counts*(log((mu+1) / ( xmax^(mu+1) - xmin^(mu+1))) + mu*log(x)));
else
return(counts*(log(log(xmin) - log(xmax)) + mu*log(x)));
}
"
stanvars <- stanvar(scode = stan_funs, block = "functions")
#### load attached data called dw_short.csv ####
fit2 <- brm(
dw | vint(counts) | vreal(xmin, xmax) ~ mat_s + (1|sample),
data = dw_short,
family = paretocounts,
stanvars = stanvars
)
```

Data attached

Please also provide the following information in addition to your question:

- Operating System: Windows 10 Enterprise
- brms Version: 2.19.0

dw_short.csv (494.4 KB)

just don’t use vint and vreal in the vars argument of custom_family(). brms handles that itself.

Thanks for the quick reply. How should the addition terms be added to the brm formula? Currently I have them as follows:

fit2 ← brm(

dw | vint(counts) | vreal(xmin, xmax) ~ mat_s + (1|sample),

data = dw_short,

family = paretocounts,

stanvars = stanvars

)

That didn’t work to begin with. I’ve tried it by removing the addition terms and the error message appears to be looking for the addition terms somewhere:

Error in stanc(file = file, model_code = model_code, model_name = model_name, :

0

Semantic error in ‘string’, line 58, column 16 to column 47:

Ill-typed arguments supplied to function ‘paretocounts_lpdf’. Available signatures:

(real, real, real, real, real) => real

Instead supplied arguments of incompatible type: real, real.

they should be there but don’t use the vint part of the vars argument. also combine multiple addition terms with a +. please see the brms_customfamilies vignette for more details

Thank you. It’s getting closer but still gives an error and I couldn’t find the solution in the vignette. I think I’m confused about how to enter *multiple* addition terms in the formula. The current error is this:

Error: The following addition terms are invalid:

‘counts’, ‘xmin’, ‘xmax’

Based on this code:

paretocounts ← custom_family(

“paretocounts”, dpars = c(“mu”),

links = c(“identity”),

lb = -Inf, ub = Inf,

type = “real”, vars = c(“counts”,

“xmin”,

“xmax”))

stan_funs ← "

real paretocounts_lpdf(real Y, real mu, real xmin, real xmax, real counts){

if(mu != -1)

return(counts*(log((mu+1) / ( xmax^(mu+1) - xmin^(mu+1))) + mu*log(Y)));*

else

return(counts(log(log(xmin) - log(xmax)) + mu*log(Y)));

}

"

stanvars ← stanvar(scode = stan_funs, block = “functions”)

dw_short = dw %>% filter(sample <= 4)

fit2 ← brm(

dw | (counts + xmin + xmax) ~ mat_s + (1|sample),

data = dw_short,

family = paretocounts,

stanvars = stanvars

)

But that gives an error that:

Thank you. If there are two vreals, how can we add them both?

This doesn’t seem to work:

y|vint(name1) + vreal(name2) + vreal(name3) ~ …

I get the error that addition terms can only occur once.

please read the docs more carefully. you are asking many questions that can be inferred from them. you do vreal(x1,x2)

Thanks. I’ll keep working on it through the docs. I appreciate the help. It is much closer than when I started.