I referred the link https://mc-stan.org/rstan/reference/stan.html#examples for having an idea in order to write a function for stan’s argument `init`

, but it is not very much clear for me. Here, it defined two functions with and without arguments and in 2nd function alpha equals to chain_id.

## 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)

}

# function form 2 with an argument named `chain_id`

initf2 <- function(chain_id = 1) {

# cat(“chain_id =”, chain_id, “\n”)

list(mu = 1, sigma = 4, z = array(rnorm(6), dim = c(3,2)), alpha = chain_id)

}

**# 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)

stan(fit = exfit0, init = initf2)

stan(fit = exfit0, init = init_ll, chains = n_chains)

Is this the way we have to define a function for “init” argument? Please explain me little bit.

For my case, is it correct to write function as follows:

initfun <- function(){

list(mu=runif(1,4.0,4.5), sigma2=runif(1,0.01,0.05))

}

stan(model_code, data, warmup = 250,iter = 500, chains =4, init =initfun)

I would be more grateful if someone can leave me suggestions here.

Thanks