# Define some init values manually according to (uniform distribution) function

Hi all,

I would like to set starting values for a `brms::brm` model manually for some coefficients only and according to a function (runif).

My current code looks something like this (you can ignore `x` variables and `unit`, I just want to manipulate the starting values of `z` and of `"as.factorFa1` and `"as.factorFa2`):

``````library(tidyverse)
library(brms)

df <- data.frame(x1 = rnorm(100, mean = 1, sd = 2),
x2 = rnorm(100, mean = 2, sd = 2),
x3 = rnorm(100, mean = 3, sd = 2),
z  = rnorm(100, mean = 4, sd = 2),
Fa = wakefield::r_sample_factor(n = 100, x = c(1, 2, 3)),
unit = wakefield::r_sample_factor(n = 100, x = 1:10),
Y = rnorm(100, mean = 15, sd = 5)
) %>% tibble::tibble()

brmsmodel <- brm(Y ~ as.factor(Fa) + # Fa has levels 1, 2, 3
x1 + x2 + x3 + z + (1 + x1 | unit),
data = df,
prior = c(prior(normal(0, 1), class = "b", coef = "z"),
prior_string("normal(0, 1)", class = "b", coef = paste("as.factorFa", 2:3, sep=""))),
chains = 1, iter = 25, warmup = 10, seed = 123,
init = "0")
``````

This code works; however, I would like to set some starting values (`inits`) manually to be drawn from a uniform distribution `(X ~ U[0,1])`, similar to the priors.

I want something like this (pseudo code)

`````` inits: 1) init = runif(0, 1), class = "b", coef = "z"
2) init = runif(0, 1), class = "b", coef = "as.factorFa2"
3) init = runif(0, 1), class = "b", coef = "as.factorFa2"
other inits: random
``````

I only found `inits = "0"` (to set all starting values to 0) or the default `inits = "random"`. Can someone advise how to do this? Thanks in advance!

Maybe I don’t understand something, but if you don’t define any inits for those values, remove the seed number, the behaviour should be similar to what you are actually looking for.

Thank you @aakhmetz. To be honest, I would rather assume I didn’t fully grasp what the “random” means. ;) Does it default to pick random values between 0 and 1? In that case, this gets me to the desired behaviour. Otherwise: how can I set boundaries for randomly generated starting values for some coefficients?

I assume that uniform prior on that parameter will guarantee that initial value would start from within (0,1). Then if you don’t fix the inits, that initial value would be randomly selected according to the seed number.

I may propose to save warm-up and check that it starts randomly from (0,1) unless inits are given.

1 Like

Sounds good, thank you! Re your proposition: how would I achieve that?

For brms packages, you may specify “inc_warmup = TRUE”, then you can investigate from where it starts.

Awesome, thank you a lot for the help with this!

1 Like