How to define initial values in stan() in R

Hi all, In my model I want to parse initial values for stan(), so this is what I have written as my stan code:
stan(model_code, data, warmup = 250,iter = 500, chains =4, init = list( list(mu=4.5),list(sigma2=0.05)))

But,I got an error,
Error in config_argss(chains = chains, iter = iter, warmup = warmup, thin = thin, :
initial value list mismatchs number of chains
error in specifying arguments; sampling not done.

My parameter block looks like that
parameters{
real<lower=4.0,upper=4.5> mu;
real<lower=0.01,upper=0.05> sigma2;
}

and model block starts like this,
model{
//prior distributions
mu ~ uniform(4.0,4.5);
sigma2 ~ uniform(0.01,0.05);

}

After having above error I changed “chains=2” and then it gives me another error,
SAMPLING FOR MODEL ‘e7eb143cebe5ca8e6c580f123d99c99e’ NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.
Chain 1: Stan can’t start sampling from this initial value.

Can anyone help me here please?

Thanks

1 Like

You need a list of lists that is equal to the number of chains. But if you want to use the same initial values for all of the chains (which is not a good idea), then it is better to define a function that returns such as list like

init_fun <- function(...) list(mu=4.5, sigma2=0.05)

and then call

stan(model_code, data, iter = 500, init = init_fun)

However, if you feel the need to specify initial values yourself, then you have probably done something else wrong.

2 Likes

Hello Ben, Thanks for the idea, I really want to use initial values like this

init = list(mu=runif(1,4.0,4.5), sigma2=runif(1,0.01,0.05)) and not the same value for each chain.
Is it possible to use?

Thanks

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

You might need

initfun <- function(...) {
  list(mu=runif(1,4.0,4.5), sigma2=runif(1,0.01,0.05))
}

However, messing around with the initial values is usually a waste of time.

1 Like

Thank you very much @bgoodri . I could define it.