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

**URL:** <https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637>\
**Category:** Modeling\
**Tags:** brms\
**Created:** [May 21, 2021, 8:42am UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637 "2021-05-21T08:42:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibantel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ibantel/32/11739_2.png) [@ibantel](https://discourse.mc-stan.org/u/ibantel)\
**Post date:** [May 21, 2021, 8:42am UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637/1 "2021-05-21T08:42:12Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aakhmetz/32/18672_2.png) [@aakhmetz](https://discourse.mc-stan.org/u/aakhmetz)\
**Post date:** [May 21, 2021, 10:42am UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637/2 "2021-05-21T10:42:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ibantel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ibantel/32/11739_2.png) [@ibantel](https://discourse.mc-stan.org/u/ibantel)\
**Post date:** [May 21, 2021, 11:33am UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637/3 "2021-05-21T11:33:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aakhmetz/32/18672_2.png) [@aakhmetz](https://discourse.mc-stan.org/u/aakhmetz)\
**Post date:** [May 21, 2021, 12:38pm UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637/4 "2021-05-21T12:38:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ibantel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ibantel/32/11739_2.png) [@ibantel](https://discourse.mc-stan.org/u/ibantel)\
**Post date:** [May 21, 2021, 12:54pm UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637/5 "2021-05-21T12:54:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aakhmetz](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/aakhmetz/32/18672_2.png) [@aakhmetz](https://discourse.mc-stan.org/u/aakhmetz)\
**Post date:** [May 22, 2021, 8:35am UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637/6 "2021-05-22T08:35:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ibantel](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ibantel/32/11739_2.png) [@ibantel](https://discourse.mc-stan.org/u/ibantel)\
**Post date:** [May 24, 2021, 6:41am UTC](https://discourse.mc-stan.org/t/define-some-init-values-manually-according-to-uniform-distribution-function/22637/7 "2021-05-24T06:41:56Z")

</div>

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