rmcelreath / rethinking

Statistical Rethinking course and book package
2.1k stars 596 forks source link

ulam: save> and log_lik clash with one another #394

Open rxg opened 1 year ago

rxg commented 1 year ago

The example code listed at the bottom produces the following error:

Semantic error in '/var/folders/np/d_865sb11c93jr4g3vdjws8r0000gs/T/RtmpQ4sg49/model-332b1d1c9bf8.stan', line 27, column 16 to column 17:
   -------------------------------------------------
    25:       vector[1934] p;
    26:       vector[61] a;
    27:       vector[61] a;
                         ^
    28:      a = abar + za * sigma_a;
    29:      for ( i in 1:1934 ) {
   -------------------------------------------------

Identifier 'a' is already in use.

The problem I suspect is that both log_lik and save> independently introduce a declaration to the generated_quantities block. Changing save> to transpars> or removing log_lik = TRUE compiles successfully.

Code follows:

library(rethinking)

data(bangladesh)
d <- bangladesh

dat <- list(
    C = d$use.contraception,
    D = as.integer(d$district)
)

m2 <- ulam(
    alist(
        C ~ dbern(p),
        logit(p) <- a[D],
        save > vector[61]:a <<- abar + za * sigma_a,
        vector[61]:za ~ dnorm(0, 1),
        abar ~ dnorm(0, 1),
        sigma_a ~ dexp(1)
    ),
    data = dat, chains = 4, cores = 4, log_lik = TRUE
)
rmcelreath commented 1 year ago

Thanks, Ron. This seems like something I could make an error trap for so the error message is more informative for the user.