Problem with the stan sampler

Dear all,

My name is Alejandro, I am trying to run the stan routine in the archives below, but I am getting the following error:

SAMPLING FOR MODEL 'anon_model' NOW (CHAIN 1).
Chain 1: Unrecoverable error evaluating the log probability at the initial value.
Chain 1: Exception: Found negative dimension size in variable declaration; variable=nei; dimension size expression=N_edges; expression value=-2147483648 (in 'string', line 127, column 11 to column 18)
[1] "Error : Exception: Found negative dimension size in variable declaration; variable=nei; dimension size expression=N_edges; expression value=-2147483648 (in 'string', line 127, column 11 to column 18)"
[1] "error occurred during calling the sampler; sampling not done"

The error is saying that N_edges, an index, is negative with this value, value=-2147483648, which is very weird because I am adding all the elements of a matrix with just zeros and ones. In the reproductible example I tested the function and it was ok. I do not know what else to do to fix this bug, I would appreciate any suggestions.

Thanks in advance,

Att,

Alejandro.

reproductible_example_stan_forums_2.R (3.1 KB)
dagar_poisson_no_covariates_p_random_2_prac.stan (5.0 KB)
sourcegraphs.R (6.0 KB)

The relevant lines are:

  int  N_edges;  // total numbere of edges
  int  nei[N_edges]; //vector stacking up the direected neighbors of each node
  int adjacency_ends[N];//Where adjacency of each node ends in the nei vector
  
  N_nei = row_sums_stan(adjmatinf);
  //N_edges  =  sum_2d_array(adjmatinf);
  N_edges = sum(array_2d_to_vector(adjmatinf));

Stan executes the statements in the order they are written, so N_edges does not have a value until the last line, but it’s already used on the second line. The negative value you see is a placeholder value for uninitialized integers. To fix the problem, assign the value on variable definition:

  int  N_edges = sum(array_2d_to_vector(adjmatinf));  // total numbere of edges
  //N_edges  =  sum_2d_array(adjmatinf);
  int  nei[N_edges]; //vector stacking up the direected neighbors of each node
  int adjacency_ends[N];//Where adjacency of each node ends in the nei vector
  
  N_nei = row_sums_stan(adjmatinf);