stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

Internal compiler error: TypeError: not a function #1434

Open fornaeffe opened 1 week ago

fornaeffe commented 1 week ago

rstan asked me to report a bug, so I'm reporting it:

Error text:

Errore in stanc(file = file, model_code = model_code, model_name = model_name,  : 
  0
Internal compiler error:
TypeError: not a function

This should never happen. Please file a bug at https://github.com/stan-dev/stanc3/issues/new
and include this message and the model that caused this issue.

Model:

data {
    int<lower=1> L; // Numero di liste
    int<lower=1> E; // Numero di elezioni
    int<lower=1> P; // Numero di province

    // 1 = la lista è presente alle elezioni
    // 2 = la lista non si è presentata alle elezioni
    array[E, L] int<lower=0, upper=1> presente;

    // Voti ricevuti da ciascuna lista in ciascuna provincia in ciascuna 
    // elezione
    array[E, P, L] int<lower=0> voti; 

    // Prior parameters

    // Varianza del fattore che influenza la probabilità di passaggio
    // tra un'elezione e l'altra
    real sigma2_e;

    real mu_stesso;
    real sigma2_stesso;
    real mu_diverso;
    real sigma2_diverso;
}

transformed data {
   array[E, P] int elettori;

   // Calcolo il numero di aventi diritto al voto ad ogni elezione
   for (e in 1:E) {
        for (p in 1:P) {
            elettori[e, p] = sum(voti[e, p]);
        }
   }

   // Conversione dei parametri prior
   real alpha_stesso = mu_stesso^2 / sigma2_stesso;
   real beta_stesso = mu_stesso / sigma2_stesso;
   real alpha_diverso = mu_diverso^2 / sigma2_diverso;
   real beta_diverso = mu_diverso / sigma2_diverso;
}

parameters {
    // Per ogni provincia ed ogni elezione,
    // proporzione di persone vicine a ciascuna lista
    array[P] simplex[L] frazione_base;
    array[E - 1, P, L] simplex[L] passaggi;
    array[E] matrix<lower=0>[L, L] matrice_passaggi;
    matrix<lower=0>[L, L] matrice_base;
}

transformed parameters {
   array[E, P] simplex[L] frazione;

   // Calcolo la frazione per ciascuna elezione partendo dalla prima
   // e calcolandola come somma dei passaggi di elettori dalle liste
   // dell'elezione precedente
   frazione[1] = frazione_base;
   for (e in 2:E) {
        for (p in 1:P) {
            for (l in 1:L) {
                frazione[e, p, l] = 0;

                for (l2 in 1:L) {
                    frazione[e, p, l] += 
                        frazione[e-1, p, l2] * passaggi[e-1, p, l2, l];
                }
            }
        }

   }
}

model {
    // I voti devono rispecchiare le frazioni di elettori.
    // Evito di considerare le liste che non si presentano,
    // ed evito di considerare l'astensione visto che comprenderà
    // sia la frazione legata all'astensione sia la frazione
    // legata alle liste che non si presentano.
    // TODO: esplicitare questa somma
    // TODO2: ampliare questo concetto alle liste che si presentano insieme.
    for (e in 1:E) {
        for (p in 1:P) {
            for (l in 1:(L-1)) {
                if (presente[e, l] == 1) {
                    voti[e, p, l] ~ binomial(elettori[e, p], frazione[e, p, l]);
                }
            }
        }
    }

    for (e in 1:(E-1)) {
        for (p in 1:P) {
            for (l in 1:L) {
                passaggi[e, p, l] ~ dirichlet(matrice_passaggi[e, ,l]);
            }
        }

        for (l in 1:L) {
            for (l2 in 1:L) {
                real alpha = matrice_base[l2, l]^2 / sigma2_e;
                real beta = matrice_base[l2, l] / sigma2_e;

                matrice_passaggi[e, l2, l] ~ gamma(alpha, beta);
            }
        }
    }

    for (l in 1:L) {
        for (l2 in 1:L) {
            if (l == l2) {
                matrice_base[l2, l] ~ gamma(alpha_stesso, beta_stesso);
            } else {
                matrice_base[l2, l] ~ gamma(alpha_diverso, beta_diverso);
            }
        }
    }

}

Lines of code that launched the model:

...

dati <- list(
  L = nrow(liste),
  E = nrow(df_elezioni),
  P = nrow(province),
  presente = table(lista_elezione$DATA, lista_elezione$LISTA),
  voti = prepara_voti(),
  sigma2_e = 1,
  mu_stesso = 50,
  sigma2_stesso = 10,
  mu_diverso = 1,
  sigma2_diverso = 1
)

options(mc.cores = parallel::detectCores())

stan(
  "model.stan",
  dati,
  chains = 1,
  iter = 10
)

R session info:

R version 4.3.3 (2024-02-29 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default

locale:
[1] LC_COLLATE=Italian_Italy.utf8  LC_CTYPE=Italian_Italy.utf8    LC_MONETARY=Italian_Italy.utf8
[4] LC_NUMERIC=C                   LC_TIME=Italian_Italy.utf8    

time zone: Europe/Rome
tzcode source: internal

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rstan_2.35.0.9000       StanHeaders_2.35.0.9000 abind_1.4-5             extraDistr_1.10.0      
[5] zoo_1.8-12              kableExtra_1.4.0        readxl_1.4.3            stringr_1.5.1          
[9] data.table_1.15.4      

loaded via a namespace (and not attached):
 [1] utf8_1.2.4         generics_0.1.3     xml2_1.3.6         lattice_0.22-5     stringi_1.8.4     
 [6] digest_0.6.35      magrittr_2.0.3     evaluate_0.24.0    grid_4.3.3         pkgload_1.3.4     
[11] fastmap_1.2.0      cellranger_1.1.0   pkgbuild_1.4.4     formattable_0.2.1  gridExtra_2.3     
[16] fansi_1.0.6        viridisLite_0.4.2  QuickJSR_1.2.2     scales_1.3.0       codetools_0.2-19  
[21] cli_3.6.2          rlang_1.1.4        munsell_0.5.1      tools_4.3.3        inline_0.3.19     
[26] dplyr_1.1.4        colorspace_2.1-0   ggplot2_3.5.1      vctrs_0.6.5        R6_2.5.1          
[31] matrixStats_1.3.0  stats4_4.3.3       lifecycle_1.0.4    htmlwidgets_1.6.4  pkgconfig_2.0.3   
[36] RcppParallel_5.1.7 pillar_1.9.0       gtable_0.3.5       loo_2.7.0          glue_1.7.0        
[41] Rcpp_1.0.12        systemfonts_1.1.0  xfun_0.45          tibble_3.2.1       tidyselect_1.2.1  
[46] rstudioapi_0.16.0  knitr_1.47         htmltools_0.5.8.1  svglite_2.1.3      rmarkdown_2.27    
[51] compiler_4.3.3
fornaeffe commented 1 week ago

Edit: found the problem, I simply forgot "data =" in the stan() function call...

Maybe a better error description could suggest where to look?

WardBrian commented 1 week ago

@andrjohns this was the error caused by the quickJS bug right?