What is causing the error in compiling cython file?


I just began to learn to Stan. I was using Rstan but it crashes every now and then with R studio so I turned to pystan.

Here I tried to compile my first model code:

data {
int N; // number of counties
int T; // number of time
int Y[N,T]; // data matrix
vector[N] Xpop; // regressor in mu
vector[N] Pop_prop;
vector[N] offset;
matrix[N,N] W; // adjacency matrix
// matrix[N,N] D_w; // diagonal matrix

transformed data{
matrix[N,N] D_w;
for(i in 1:N){
for(j in 1:N){


parameters {
real<lower=0> tau_mu;
real<lower=0> tau_lambda;
real<lower=0> tau_temp;
vector[2] beta_mu;
vector[2] beta_lambda;
real<lower=0,upper=1> p_s;
real<lower=0,upper=1> p_c;
real<lower=0,upper=1> rho1;
real<lower=0, upper=1> rho2;

transformed parameters{
real<lower=0> sigma_mu;
real<lower=0> sigma_lambda;
real<lower=0> sigma_temp;
sigma_mu = 1/tau_mu;
sigma_lambda = 1/tau_lambda;
sigma_temp = 1/ tau_temp;

model {
vector[N] mu;
vector[N] e_mu;
int delta[N,T];
matrix[N,T] lambda;
matrix[N,T] temp;
for(i in 1:N){
e_mu[i] ~ normal(0,sigma_mu);
Y[i,1] ~ poisson_log(mu[i]);
delta[i,1] = 0;
lambda[i,1] = beta_lambda[1]+beta_lambda[2]*Xpop[i];

for(t in 2:T){
    vector[N] poimean;
    vector[N] e_lambda;
    vector[N] xi;
    matrix[N,N] Omega;
    real p_temp;
    real power;
    vector[N] sum_pop_delta;
    Omega = tau_lambda * (D_w- rho1 * W);
    xi ~ multi_normal_prec(rep_vector(0,N),Omega);
    for(i in 1:N){
          for(i0 in 1:N){          
            sum_pop_delta[i0] = W[i,i0]*delta[i0,t-1]*Pop_prop[i]; // accommodate int and real
              delta[i,t] ~ binomial(1,p_s);
            power = Pop_prop[i]+ sum(sum_pop_delta);
            delta[i,t] ~ binomial(1,p_temp);
        temp[i,t] ~ normal(rho2*temp[i,t-1],sigma_temp);
        lambda[i,t] = beta_lambda[1] + beta_lambda[2]*Xpop[i]+temp[i,t]+xi[i];
        poimean[i] = mu[i] + delta[i,t]*lambda[i,t];
        Y[i,t] ~ poisson_log(poimean[i]);


tau_mu ~ gamma(2,1);
tau_lambda ~ gamma(2,1);
tau_temp ~ gamma(2,1);
for(i in 1:2){
beta_mu[i] ~ normal(0,1);
beta_lambda[i] ~ normal(0, 0.01);
p_s ~ lognormal(-6.91,1)T[0,1];
p_c ~ lognormal(-2.99,5)T[0,1];
rho1 ~ lognormal(-.69,1)T[0,1];
rho2 ~ lognormal(-0.69,1)T[0,1];


After fixing syntax errors line by line, I got this message at running StanModel():

as well as,

I have no idea what is going on. Any help is appreciated!!

Remove spaces from module name.

Do you mean the space as in “model {” and changing it into “model{” ? I tried that, but error message remains the same.

In your StanModel call you have probably defined model_name. Remove spaces from it.

1 Like