stan-dev / stanc3

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

new compiler on your syntactically correct model #887

Closed simeonqs closed 3 years ago

simeonqs commented 3 years ago

I got this error while compiling a standard test model in the rethinking package:

Semantic error in 'string', line 2, column 8 to column 14: 
Identifier 'reject' clashes with reserved keyword.

code:

library(rethinking)
# prep data
data( UCBadmit )
UCBadmit$male <- as.integer(UCBadmit$applicant.gender=="male")
UCBadmit$dept <- rep( 1:6 , each=2 )
UCBadmit$applicant.gender <- NULL

# varying intercepts model
m_glmm1 <- ulam(
  alist(
    admit ~ binomial(applications,p),
    logit(p) <- a[dept] + b*male,
    a[dept] ~ normal( abar , sigma ),
    abar ~ normal( 0 , 4 ),
    sigma ~ half_normal(0,1),
    b ~ normal(0,1)
  ), data=UCBadmit, chains = 4, cores = 4, iter = 1000, control = list(max_treedepth = 15))
SteveBronder commented 3 years ago

Are you able to print the stan code generated by ulam? Reject is a reserved word in the stan language

rok-cesnovar commented 3 years ago

This code below works:

library(rethinking)
# prep data
data( UCBadmit )
UCBadmit$male <- as.integer(UCBadmit$applicant.gender=="male")
UCBadmit$dept <- rep( 1:6 , each=2 )
UCBadmit$applicant.gender <- NULL
UCBadmit$rejected <- UCBadmit$reject
UCBadmit$reject <- NULL

# varying intercepts model
m_glmm1 <- ulam(
  alist(
    admit ~ binomial(applications,p),
    logit(p) <- a[dept] + b*male,
    a[dept] ~ normal( abar , sigma ),
    abar ~ normal( 0 , 4 ),
    sigma ~ half_normal(0,1),
    b ~ normal(0,1)
  ), data=UCBadmit, chains = 4, cores = 4, iter = 1000, control = list(max_treedepth = 15, adapt_delta = 0.8), cmdstan = TRUE)

The trick is to rename $reject to $rejected in

UCBadmit$rejected <- UCBadmit$reject UCBadmit$reject <- NULL

For reference, the generated code that errors is:

data{
    int reject[12];
    int applications[12];
    int admit[12];
    int male[12];
    int dept[12];
}
parameters{
    vector[6] a;
    real abar;
    real<lower=0> sigma;
    real b;
}
model{
    vector[12] p;
    b ~ normal( 0 , 1 );
    sigma ~ normal( 0 , 1 );
    abar ~ normal( 0 , 4 );
    a ~ normal( abar , sigma );
    for ( i in 1:12 ) {
        p[i] = a[dept[i]] + b * male[i];
        p[i] = inv_logit(p[i]);
    }
    admit ~ binomial( applications , p );
}

Will close, as there is not much that can be done on our end. Reject is a reserved keyword.

simeonqs commented 3 years ago

Thanks a lot. This is an example for Richards McElreath rethinking package, but it's good to know what causes the issue.

rok-cesnovar commented 3 years ago

Thanks. Can you link me to the example?

simeonqs commented 3 years ago

https://github.com/rmcelreath/rethinking

Under Multilevel model formulas. I added a few settings to test if changing treedepth, cores etc. caused any issues.