Bym2

i wrote the data part like this and deleted the array[N] int<lower=0> y .
but i still get the same error .
this is the my data … may you see it and say how should i define it ?
sorry to take up your time . i spent a lot of time trying to I’mplement this model . but i didn’t succeed …

sincerely , my data is as below…
iran_data.R (5.4 KB)

the correction of the wrong part was done like this …

**> data {**
**>   int<lower=0> N;**
**>   int<lower=0> N_edges;**
**>   int<lower=1, upper=N> node1[N_edges];  // node1[i] adjacent to node2[i]**
**>   int<lower=1, upper=N> node2[N_edges];  // and node1[i] < node2[i]**
**> **
**>   int<lower=0> y[N];              // count outcomes**
**>   vector<lower=0>[N] E;           // exposure**
**>   int<lower=1> K;                 // num covariates**
**>   matrix[N, K] x;                 // design matrix**
**> **
**>   real<lower=0> scaling_factor; // scales the variance of the spatial effects**
**> }**

You declared y to be an integer, but the data that you are passing as y is not integer data. Removing the declaration of y doesn’t help, because you program uses y; it needs y to exist. I’m not sure what to advise right now, because your model says y ~ poisson_log(log_E + beta0 + x * betas + convolved_re * sigma); , which makes sense only if y is an integer. The fix must be one of two things:

  1. If y is not supposed to consist of integer counts, then you need to modify your Stan program so that y is declared as real rather than int AND so that the likelihood uses a distribution compatible with non-integer outcomes.
  2. If y really is supposed to consist of integer counts, then there is a bug in the way that you are creating your data object, because the data object contains non-integers. In this case, you need to debug your process for generating the data y.
1 Like

As @jsocolar said, the program is expecting integers for y. The problem could be that one of your counts y contains a decimal like 10.1 instead of 10. The Poisson model is designed for counts, so if there’s a 10.1 or whatever then we would say that your data is not count data, and a different model would be better.

Otherwise:

See if all these m equal 1 (they should all be 1; the exception is when y is zero, then they should be NaN):

m <- y / as.integer(y)
all(m == 1)
print(m)

The above should help you see quickly if there are any non-integer values in y. (as.integer is like round but it also makes sure it is the result is the correct type.) If they look good, then I think it might be at least possible that this will fix the issue:

y <- as.integer(y)

before passing y to the data list for Stan.

dear @jsocolar and dear @cmcd
I managed to fix the previous error. But this time the same error occurs about scaling_factor
How should I defeat it?

Error in new_CppObject_xp(fields$.module, fields$.pointer, …) : **
** Exception: variable does not exist; processing stage=data initialization; variable name=scaling_factor; base type=double (in ‘string’, line 12, column 2 to column 31)

In addition: Warning message:
In readLines(file, warn = TRUE) :
** incomplete final line found on ‘C:\Users\Uaer\Desktop\Data hyperglysemia_files\bym\bym2_predictor_plus_offset.stan’**
failed to create the sampler; sampling not done

You were right. I had decimals

But what should be done about scales the variance of the spatial effects?

I suggest doing this:

data_list <- list()
data_list$y <- y
data_list$scaling_factor = 1
... [creating your list of data for Stan]

That’s just a placeholder value. After you get it running, you can return to the scale factor, read up on it again if needed, and decide if you want to use it.

1 Like

I was finally able to fit the model with the help of you my deaaarrrr :)
really thanks .
@cmcd
@jsocolar
You are wonderful
thanks , thanks , thanks …

2 Likes