Initialization error

I made a multilevel model which looks like below. I was able to run it on a small dataset but now I am using a larger dataset and suddenly I get the following error messages:

Rejecting initial value:
Error evaluating the log probability at the initial value.
Exception: normal_lpdf: Location parameter is inf, but must be finite! (in 'model at line 120)

Initialization between (-2, 2) failed after 100 attempts.
Try specifying initial values, reducing ranges of constrained values, or reparameterizing the model.
[1] “Error in sampler$call_sampler(args_list[[i]]) : Initialization failed.”
[1] “error occurred during calling the sampler; sampling not done”

Line 120 is the following line: activations[i] ~ normal(mu[i], sigma_e0);
I also tried to change the initial values by setting init_r to a different value, but without success.

Does anyone know what I am doing wrong?

data {
  // Define variables in data
  // Number of level-1 observations (an integer)
  int<lower=0> Ni;
  // Number of level-2 clusters
  int<lower=0> Nj;
  // Number of level-3 clusters
  int<lower=0> Nk;
  // Numer of models x countries
  int<lower = 0> Njk;
  // Number of fixed effect parameters
  int<lower=0> p;
  // Number of random effect parameters
  //int<lower=0> q;
  int<lower=0> Npars;
  // Variables with fixed coefficient
  matrix[Ni,p] fixedEffectVars;
  // Cluster IDs
  int<lower=1> modelid[Ni];
  int<lower=1> countryid[Ni];
  // Level 3 look up vector for level 2
  int<lower=1> countryLookup[Npars];
  int<lower=1> countryMonthLookup[Njk];
  // Continuous outcome
  int activations[Ni];

parameters {
  // Define parameters to estimate
  // Population intercept (a real number)
  real beta_0;
  // Fixed effects
  vector[p] beta;
  // Level-1 errors
  real<lower=0> sigma_e0;
  // Level-2 random effect
  real u_0jk[Npars];
  real<lower=0> sigma_u0jk;
  // Level-3 random effect
  real u_0k[Nk];
  real<lower=0> sigma_u0k;

transformed parameters  {
  // Varying intercepts
  real beta_0jk[Npars];
  real beta_0k[Nk];
  // Individual mean
  real mu[Ni];
  // Varying intercepts definition
  // Level-3 (10 level-3 random intercepts)
  for (k in 1:Nk) {
    beta_0k[k] = beta_0 + u_0k[k];
  // Level-2 (100 level-2 random intercepts)
  for (j in 1:Npars) {
    beta_0jk[j] = beta_0k[countryLookup[j]] + u_0jk[j];
  // Individual mean
  for (i in 1:Ni) {
    mu[i] = beta_0jk[countryMonthLookup[i]]  + fixedEffectVars[i,]*beta;

model {
  // Prior part of Bayesian inference
  // Flat prior for mu (no need to specify if non-informative)
  // Random effects distribution
  u_0k  ~ normal(0, sigma_u0k);
  u_0jk ~ normal(0, sigma_u0jk);
  beta[1] ~ normal(-0.25, 1);
    // Likelihood part of Bayesian inference
  // Outcome model N(mu, sigma^2) (use SD rather than Var)
  for (i in 1:Ni) {
    activations[i] ~ normal(mu[i], sigma_e0);

as a first check, make sure that your dataset doesn’t contain any missing values.

I checked this, but my dataset does not contain any NA/missing values

Add priors on different sigmas? Now they are 0-inf, so 1e300 is not good.

Edit. Or not, might be something else

I haven’t really tried to grok your model - not knowing the data or intent, I would try debug by print statement - specifically, put a print statement in the transformed parameters block to see what’s causing your mu[i] to run off the rails -

in the for loop:

// Individual mean
for (i in 1:Ni) {
  mu[i] = beta_0jk[countryMonthLookup[i]] + fixedEffectVars[i,]*beta;
  print("i: ", i, " mu[i] ", mu[i]);

I think that’s the correct syntax - haven’t actually tried it myself. good luck.

@mitzimorris thanks for your reply!

I printed the mu[i] values as you said, but these values seem to be ok. Only values in the range of roughly -50 to 50 and no Inf or -Inf values or anything like that. The error message indicates that one of the mu values should be infinite right? Do you have any other idea where the problem could be?

I tried this, nut unfortunately this doesn’t make a difference

why do you only initialize 1 element of vector beta?

I changed this and set a prior on all elements of beta. Unfortunately, without any difference.

again, not sure I understand your model, but from looking at the code I see a whole lot of different sizes for your data structures - Ni, …, Npars, and a whole lot of loops. perhaps an indexing error - the small dataset had correct sizes, the larger one something was off somewhere?

programming is tricky (that’s putting it politely),

I think that your problem is somewhere with countryMonthLookup.

You define it as Njk long but call it over 1:Ni

Maybe that is on purpose but it seems strange.

I would also recommend rewriting this without the for loops. I was in the process of doing that when I figured out that those lengths don’t match. You’ll have to remake your arrays into vectors.

1 Like