# Error: Chain 1: Rejecting initial value: mu\[1\] is nan, but must be greater than or equal to 0

**URL:** <https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261>\
**Category:** Modeling\
**Created:** [June 28, 2020, 3:20pm UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261 "2020-06-28T15:20:11Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [June 30, 2020, 6:59pm UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/21 "2020-06-30T18:59:06Z")

</div>

> [@Nazanin](#):
>
> and also for “sai” the prior is:
> 
> ```no-highlight
> target += -0.5 * dot_self(sai[node1] - sai[node2]);
> 
> ```

That’s the ICAR prior - pairwise distance - and it’s not identifiable - you can add any constant you want to `sai` and it washes out of the pairwise difference. adding a sum-to-zero constraint fixes this problem. we recommend a soft sum-to-zero constraint.

You can code up the ICAR prior as a function in Stan as follows:

```no-highlight
functions {
  real icar_normal_lpdf(vector phi, int N, int[] node1, int[] node2) {
    return -0.5 * dot_self(phi[node1] - phi[node2])
      + normal_lpdf(sum(phi) | 0, 0.001 * N);
 }
}

```

When used in a sampling statement, you call it as follows:

```no-highlight
sai ~ icar_normal(N, node1, node2)

```

---

<div class="post-metadata">

**Author:** ![Nazanin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nazanin/32/8724_2.png) [@Nazanin](https://discourse.mc-stan.org/u/Nazanin)\
**Post date:** [June 30, 2020, 8:32pm UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/22 "2020-06-30T20:32:53Z")

</div>

Many thanks for your comments @mitsimorris. Yes, I added it to the codes before but forgot to write it here. soft sum-to-zero constraint on sai in codes is:

```stan
target += -0.5 * dot_self(sai[node1] - sai[node2]); print("target = ", target());
  sum(sai) ~ normal(0, 0.001 * N);

```

But I think the problem is something else and I wonder what it is.

---

<div class="post-metadata">

**Author:** ![mitzimorris](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mitzimorris/32/29_2.png) [@mitzimorris](https://discourse.mc-stan.org/u/mitzimorris)\
**Post date:** [June 30, 2020, 9:21pm UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/23 "2020-06-30T21:21:39Z")

</div>

it looks like the BYM2 part is OK.  
the complexity of your model and the fact that I haven’t yet implemented a spatio-temporal model  
make it hard to see what’s wrong.  
the standard debugging advice is to simplify - could you set up a basic spatio-temporal regression based on your code and share?

---

<div class="post-metadata">

**Author:** ![Nazanin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nazanin/32/8724_2.png) [@Nazanin](https://discourse.mc-stan.org/u/Nazanin)\
**Post date:** [July 1, 2020, 6:26am UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/24 "2020-07-01T06:26:26Z")

</div>

Thanks a lot for your reply @mitsimorris. I wrote the code more simply by distributing gamma. The result is the same as before! The codes:

```stan
data {
  
  int<lower=0> N;     
  int <lower=0> K;  
  
  int<lower=1> N_edges;
  int<lower=1, upper=N> node1[N_edges];  
  int<lower=1, upper=N> node2[N_edges];  
  real<lower=0.00001> scaling_factor; 

  vector [N] y;    
  matrix [N, K] X; 

 int <lower=1> S; // Spaces
  int <lower=1> TJ; // Times
}

transformed data {
  for (i in 1:N_edges) {
    if (node1[i] >= node2[i])
       reject("edge pair i=", i, ", node1[i] must be less than node2[i]");
  }
}

parameters {
  
  //distribution
  real<lower=0> inverse_phi; //the variance parameter

  //BYM2 model
  vector[N] theta;       
  vector[N] sai;         
  real<lower=0> sigma;        
  real<lower=0, upper=1> rho; 
  
	//Reg model
	vector[K] beta;
  
  // Random walk
  real<lower=0> sigmaRw;
  real<lower=0> sigmaR;
}

transformed parameters {
  
  vector[N] convolved_re;
  vector<lower=0>[N] mu;
  vector[N] gama; //rate parameter for the gamma distribution
	vector [TJ] Rw1;
	vector [TJ] R2; 
	
  convolved_re = sqrt(1 - rho) * theta + sqrt(rho / scaling_factor) * sai;
	
	for (i in 1:S) {
		for (j in 1:TJ){
		                               
      mu[i*TJ-TJ+j]= exp(X[i*TJ-TJ+j,] * beta + convolved_re[i] * sigma + Rw1[j] + R2[j] );
			}
   	}
   	
  gama = rep_vector(inverse_phi, N) ./ mu;
}  

model {

  inverse_phi ~ exponential(1); // prior on inverse dispersion parameter
  beta[1] ~ cauchy(0,10); 
  beta[2:] ~ cauchy(0,2.5);  
  theta ~ normal(0, 1);
  sigma ~ normal(0, 1);
  rho ~ beta(3, 3);  
  sigmaRw ~ gamma (1, 0.001);
    Rw1[1] ~ uniform(0,1) ;
  for (j in 2:TJ) {
   Rw1[j] ~ normal(Rw1[j-1], 1/sigmaRw);
}
  sigmaR ~ gamma (1, 0.001);
  R2 ~ normal(0, 1/sigmaR);

  
  target += -0.5 * dot_self(sai[node1] - sai[node2]); 
 sum(sai) ~ normal(0, 0.001 * N);
  
  y ~ gamma(inverse_phi,gama);
  
}

```

the error is at line 49 which is a constraint on mu:

```stan
vector<lower=0>[N] mu;

```

this is the error (as the same):

```stan
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: validate transformed params: mu[1] is nan, but must be greater than or equal to 0 (in 'model2d587ac1394c_spatialtemporal_gamma' at line 49)

Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1: 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"

```

Thank you, I really appreciate your help.

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [July 1, 2020, 7:39am UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/25 "2020-07-01T07:39:54Z")

</div>

> [@Nazanin](#):
>
> What’s noticeable is that in the previous case,
> 
> ```no-highlight
> mu[1] is nan, but must be greater...
> 
> ```
> 
> but now:
> 
> ```no-highlight
> mu[156] is nan, but must be greater...
> 
> ```

I see that `mu` has length `N` but the loops assign to the first `S*TJ` places. I don’t know if those are equal but the error is what I’d expect if `S*TJ=155` and `N>155`.

---

<div class="post-metadata">

**Author:** ![Nazanin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nazanin/32/8724_2.png) [@Nazanin](https://discourse.mc-stan.org/u/Nazanin)\
**Post date:** [July 1, 2020, 8:21am UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/26 "2020-07-01T08:21:05Z")

</div>

Tank you @nhuurre. No it’s not true. S=31 and TJ=8 and N=S\*TJ=248 which is the sample size. It is still unknown where the number 155 came from!

---

<div class="post-metadata">

**Author:** ![nhuurre](https://avatars.discourse-cdn.com/v4/letter/n/ad7895/32.png) [@nhuurre](https://discourse.mc-stan.org/u/nhuurre)\
**Post date:** [July 1, 2020, 8:31am UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/27 "2020-07-01T08:31:49Z")

</div>

Well, 155=31\times 5 so first `NaN` at index 156 looks a bit like `j=6` is failing–but the loops are the other way around so 156 actually corresponds to `i=20,j=4` and all `i<20` must work for any `j`.

---

<div class="post-metadata">

**Author:** ![Nazanin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nazanin/32/8724_2.png) [@Nazanin](https://discourse.mc-stan.org/u/Nazanin)\
**Post date:** [July 1, 2020, 9:18am UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/28 "2020-07-01T09:18:11Z")

</div>

Great, many thanks @nhuurre. It really helped me find the problem. I had a mistake in specifying data in R before running the code, and set TJ=5! I modified the code to TJ=8, and also I changed constraint on lambda from 0.1 to:

```stan
vector<lower=0.000001>[N] lambda;

```

the error changed to this:

```stan
...
target = -inf
target = -inf
target = -inf
target = -inf
target = -inf
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.
Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: validate transformed params: gama[193] is 6.93183e-007, but must be greater than or equal to 1e-006 (in 'model2d585aa36987_BESAG_temporal_RW' at line 75)

Chain 1: Rejecting initial value:
Chain 1: Error evaluating the log probability at the initial value.
Chain 1: Exception: validate transformed params: lambda[40] is 1.97203e-007, but must be greater than or equal to 1e-006 (in 'model2d585aa36987_BESAG_temporal_RW' at line 73)

Chain 1: target = -inf
target = -inf
target = -inf
target = -inf
target = -inf
target = -inf
target = -inf
...
target = -inf
target = -inf
target = -inf
target = -inf
target = -inf

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.
Chain 1: 
Chain 1: Initialization between (-2, 2) failed after 100 attempts. 
Chain 1: 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"

```

Lambda, alpha and gama are parameters of the distribution. The problem seems to be the constraint on lambda and gama. The constraint for these parameters are:

```stan
transformed parameters {
  
  vector[N] convolved_re;
  vector<lower=0>[N] mu;
  vector<lower=0.000001>[N] lambda;
  vector<lower=0.000001>[N] alpha;
  vector<lower=0.000001>[N] gama;
...

```

---

<div class="post-metadata">

**Author:** ![Nazanin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/nazanin/32/8724_2.png) [@Nazanin](https://discourse.mc-stan.org/u/Nazanin)\
**Post date:** [July 1, 2020, 9:35am UTC](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261/29 "2020-07-01T09:35:21Z")

</div>

lambda is parameter of Poisson distribution, alpha and gama are parameters of Gamma distribution. for:

```stan
vector<lower=0>[N] lambda;

```

the code still didn’t work

[Previous page](https://discourse.mc-stan.org/t/error-chain-1-rejecting-initial-value-mu-1-is-nan-but-must-be-greater-than-or-equal-to-0/16261.md?page=1)
