initializationmcmcstan

cmdstanr: how to set initial values


I am having trouble when I want to set initial values for parameters in a Stan program for MCMC using cmdstanr. The relevant guidance in the documentation states that the value passed to the argument initin the cmdstanrinstruction sample should in this case be:

A list of lists containing initial values for all or some parameters. For MCMC the list should contain a sublist for each chain. For optimization and variational inference there should be just one sublist. The sublists should have named elements corresponding to the parameters for which you are specifying initial values. See Examples.

For illustration I am using the following simple Stan program from Rstudio

 data {
  int<lower=0> N;
  vector[N] y;
}

 parameters {
  real mu;
  real<lower=0> sigma;
}

 model {
  y ~ normal(mu, sigma);
}

I read the Stan program into R with mod <- cmdstan_model(file) and then if I run this code using sample thus

N <- as.integer(500) 
y <- rnorm(N)
data_list <- list(
  N = N,
  y = y
)

fit <- mod$sample(  
  data = data_list,
  chains = 1,   
  iter_sampling = 2000
)

I get perfectly reasonable results but if I try to set initial values of mu and sigma thus:

fit1 <- mod$sample( 
  data = data_list,
  chains = 1,    
  iter_sampling = 2000,
  init = list(
    list(mu = 0.5),
    list(sigma = 0.5) 
    ) 
)

I get this error message:

Error: 'init' has the wrong length. See documentation of 'init' argument.

I am very new to cmdstanr and I expect that I am making a silly mistake, but I can't see what I have done wrong.


Solution

  • The issue is with how you are nesting lists. See fixed code below:

    library(cmdstanr)
    
    stan_code <- ' data {
      int<lower=0> N;
      vector[N] y;
    }
    
     parameters {
      real mu;
      real<lower=0> sigma;
    }
    
     model {
      y ~ normal(mu, sigma);
    }'
    
    N <- as.integer(500) 
    y <- rnorm(N)
    data_list <- list(
      N = N,
      y = y
    )
    
    mod <- cmdstan_model(stan_file=write_stan_file(stan_code))
    
    fit1 <- mod$sample( 
      data = data_list,
      chains = 1,    
      iter_sampling = 2000,
      init = list(
        list(
        mu = 0.5,
        sigma = 0.5)
      ) 
    )