# Setting initial values for parameters when sampling with Stan

**URL:** <https://discourse.mc-stan.org/t/setting-initial-values-for-parameters-when-sampling-with-stan/9439>\
**Category:** General\
**Created:** [June 27, 2019, 9:57pm UTC](https://discourse.mc-stan.org/t/setting-initial-values-for-parameters-when-sampling-with-stan/9439 "2019-06-27T21:57:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kon/32/894_2.png) [@kon](https://discourse.mc-stan.org/u/kon)\
**Post date:** [June 27, 2019, 9:57pm UTC](https://discourse.mc-stan.org/t/setting-initial-values-for-parameters-when-sampling-with-stan/9439/1 "2019-06-27T21:57:53Z")

</div>

Hi everyone,

I am trying to figure out how to set initial values when sampling with Stan.  
I am following the [example from the documentation](https://mc-stan.org/rstan/reference/stan.html#examples) but I cannot verify that the initial values are indeed set. I would expect that the posterior samples all start from the same value (as set from the initialization function). What am I missing?

```
library(rstan)

#### example 2
#### the result of this package is included in the package 

excode <- '
  transformed data {
    real y[20];
    y[1] = 0.5796; y[2] = 0.2276; y[3] = -0.2959; 
    y[4] = -0.3742; y[5] = 0.3885; y[6] = -2.1585;
    y[7] = 0.7111; y[8] = 1.4424; y[9] = 2.5430; 
    y[10] = 0.3746; y[11] = 0.4773; y[12] = 0.1803; 
    y[13] = 0.5215; y[14] = -1.6044; y[15] = -0.6703; 
    y[16] = 0.9459; y[17] = -0.382; y[18] = 0.7619;
    y[19] = 0.1006; y[20] = -1.7461;
  }
  parameters {
    real mu;
    real<lower=0, upper=10> sigma;
    vector[2] z[3];
    real<lower=0> alpha;
  } 
  model {
    y ~ normal(mu, sigma);
    for (i in 1:3) 
      z[i] ~ normal(0, 1);
    alpha ~ exponential(2);
  } 
'
## examples of specify argument `init` for function stan

## define a function to generate initial values that can
## be fed to function stan's argument `init`
# function form 1 without arguments 
initf1 <- function() {
  list(mu = 1, sigma = 4, z = array(rnorm(6), dim = c(3,2)), alpha = 1)
}

# generate a list of lists to specify initial values
n_chains <- 4
init_ll <- lapply(1:n_chains, function(id) initf2(chain_id = id))

exfit0 <- stan(model_code = excode, init = initf1, iter = 10)
ps <- extract(exfit0, pars=c('mu'), permuted = F, inc_warmup = T, include = T)
ps
> ps
, , parameters = mu

          chains
iterations chain:1 chain:2 chain:3 chain:4
      [1,] 2.37772786 1.14155529 0.486704005 -0.31841303
      [2,] 2.37772786 1.14155529 0.486704005 -0.31841303
      [3,] 2.37772786 1.14155529 0.299312252 -0.31841303
      [4,] 1.99967744 -0.58364386 -0.689715122 0.51011513
      [5,] 1.76674347 -0.33900943 0.782739260 0.73774654
      [6,] -0.39387133 -0.16359783 -0.089862952 0.40964769
      [7,] 0.61154738 0.30488430 0.209121992 0.20777764
      [8,] -0.02478551 0.23869682 0.224947809 0.14306751
      [9,] 0.25515937 -0.53618807 -0.002487997 -0.03148008
     [10,] 0.29128538 0.03305439 0.178000276 -0.01576988

```

Thank you in advance for the help!

---

<div class="post-metadata">

**Author:** ![KariOlson](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kariolson/32/5248_2.png) [@KariOlson](https://discourse.mc-stan.org/u/KariOlson)\
**Post date:** [June 30, 2019, 9:43pm UTC](https://discourse.mc-stan.org/t/setting-initial-values-for-parameters-when-sampling-with-stan/9439/2 "2019-06-30T21:43:47Z")

</div>

I’ve been using an init function in R for just a few days. The only difference between yours and mine that I see are the curly braces… This is an R function NOT a stan function. Here’s mine:

init\_f \<- function () list(mu = rnorm(1, 15, 2.), sigma = max(0,rnorm(1, 3, 1) ))

also, you might try using get\_inits(fitobject) to read the actual init vs. looking at the samples… maybe init is sample 0?

---

<div class="post-metadata">

**Author:** ![kon](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/kon/32/894_2.png) [@kon](https://discourse.mc-stan.org/u/kon)\
**Post date:** [July 3, 2019, 6:06pm UTC](https://discourse.mc-stan.org/t/setting-initial-values-for-parameters-when-sampling-with-stan/9439/3 "2019-07-03T18:06:21Z")

</div>

Hi @KariOlson,  
Thanks for the reply.

> [@KariOlson](#):
>
> maybe init is sample 0

I think you might be right.

After trying setting mu=100 as an extreme value the posterior samples are much closer to 100.
