# Brms include offset to nonlinear formula

Hi everyone,
I have questions on how to include offset to a nonlinear formula in brms.
I’m fitting a nonlinear model to some gene expression read count data (negative binomial assumed).
The response is gene expression, the predictor is time, the group is id.
Here’s my data,named df.

expression time geno id myoffset
2 0 JEPS 1 32.1
1 0 xEPS 2 37.0
5 0 JxPS 3 36.0
1 0 JExS 4 35.0
0 0 JEPx 5 33.0
4 0 xxPS 6 36.0

Everything works fine if I don’t have offset. Here’s the code, where m, ka and ke are coefficients within nonlinear function.

f1 <- expression ~ m * ka/(ka - ke) * (exp(-ke * time) - exp(-ka * time))
form <- bf(f1,nl=TRUE) + list(m~ id - 1, ka~ id - 1, ke~ id - 1)
brm(form, data = df, family = negbinomial())

But if I add offset, it reports the following error.

f1 <- expression ~ m * ka/(ka - ke) * (exp(-ke * time) - exp(-ka * time)) + offset(log(myoffset))
form <- bf(f1,nl=TRUE) + list(m~ id - 1, ka~ id - 1, ke~ id - 1)
brm(form, data = df, family = negbinomial())
SYNTAX ERROR, MESSAGE(S) FROM PARSER:

No matches for:

offset(real)

error in ‘model35b77297c9a_file35b2839fd5f’ at line 55, column 135
53: mu_ke[n] += r_1_ke_3[J_1[n]] * Z_1_ke_3[n];
54: // compute non-linear predictor
55: mu[n] = mu_m[n] * mu_ka[n] / (mu_ka[n] - mu_ke[n]) * (exp( - mu_ke[n] * C_1[n]) - exp( - mu_ka[n] * C_1[n])) + offset(log(C_2[n]));
^
56: }
Error in stanc(model_code = paste(program, collapse = “\n”), model_name = model_cppname, :
failed to parse Stan model ‘file35b2839fd5f’ due to the above error.

It works when I do the following, but in this way, offset is considered a linear predictor instead of true offset.

f1 <- expression ~ m * ka/(ka - ke) * (exp(-ke * time) - exp(-ka * time)) + log(myoffset)
form <- bf(f1,nl=TRUE) + list(m~ id - 1, ka~ id - 1, ke~ id - 1)
brm(form, data = df, family = negbinomial())

Can anyone help me to correctly implement offset function to this nonlinear formula?
Thanks a lot!
Xiaotong

My understanding of offset() for a negbinomial regression is that you can also just divide the outcome variable by the offset. This changes the standard errors so you just need to weight by the offset. Maybe something like:

``````f1 <- expression/myoffset | weights(myoffset) ~ m * ka/(ka - ke) * (exp(-ke * time) - exp(-ka * time)) )
form <- bf(f1, nl = TRUE) + list(m ~ id - 1, ka ~ id - 1, ke ~ id - 1)
brm(form, data = df, family = negbinomial())
``````

Sounds good. Will try that.
Thanks!