rmcelreath/rethinking

stan deprecation warning after compilation: bracket array declaration

Closed this issue · 5 comments

I thought I'd give you a heads up that it appears that stan has deprecated the array declaration via brackets:

I get the following warning:
Declaration of arrays by placing brackets after a variable name is deprecated and will be removed in Stan 2.32.0. Instead use the array keyword before the type. This can be changed automatically using the auto-format flag to stanc.

after I run this ulam code:

partial_pool_both_cor_ulam <- ulam(
  alist(
    D47_final ~ dnorm(mu, sigma),
    mu <- a_bins[bins] + b_bins[bins] * d18O_PDB_vit,
    c(a_bins, b_bins)[bins] ~ multi_normal(c(a, b), Rho, sigma_bins),
    a ~ normal(0.6, .1),
    b ~ normal(0, 1),
    sigma_bins ~ exponential(1),
    sigma ~ dexp(1),
    Rho ~ lkj_corr(2)
  ),
  data = dat |>
    filter(!is.na(D47_final)) |> # we don't want to impute for now
    select(d18O_PDB_vit, bins, D47_final),
  cmdstan = TRUE, log_lik = TRUE,
  chains = 4, cores = 4, iter = 3000, warmup = 1000
)

which generates this piece of stan code:

data{
    vector[1336] D47_final;
    vector[1336] d18O_PDB_vit;
    int bins[1336];
}
parameters{
    vector[8] b_bins;
    vector[8] a_bins;
    real a;
    real b;
    vector<lower=0>[2] sigma_bins;
    real<lower=0> sigma;
    corr_matrix[2] Rho;
}
model{
    vector[1336] mu;
    Rho ~ lkj_corr( 2 );
    sigma ~ exponential( 1 );
    sigma_bins ~ exponential( 1 );
    b ~ normal( 0 , 1 );
    a ~ normal( 0.6 , 0.1 );
    {
    vector[2] YY[8];
    vector[2] MU;
    MU = [ a , b ]';
    for ( j in 1:8 ) YY[j] = [ a_bins[j] , b_bins[j] ]';
    YY ~ multi_normal( MU , quad_form_diag(Rho , sigma_bins) );
    }
    for ( i in 1:1336 ) {
        mu[i] = a_bins[bins[i]] + b_bins[bins[i]] * d18O_PDB_vit[i];
    }
    D47_final ~ normal( mu , sigma );
}
generated quantities{
    vector[1336] log_lik;
    vector[1336] mu;
    for ( i in 1:1336 ) {
        mu[i] = a_bins[bins[i]] + b_bins[bins[i]] * d18O_PDB_vit[i];
    }
    for ( i in 1:1336 ) log_lik[i] = normal_lpdf( D47_final[i] | mu[i] , sigma );
}

The warning occurs for line 4, column 4, which is the bins[1336] line.

I get this warning as well but in addition. My stan file created by ulam starts compiling but then errors because of a <lower=0> specification before the brackets.

No clue if this is at all related, and if the deprecation warning has come into effect. Working through the text book on chapter nine, my first encounter with ulam() is a bust. Same code as #342 for model m9.1, but this error stan notes [cid] variable identifier is not in scope. Error reads:

> data(rugged)
> d<-rugged
> d$log_gdp<-log(d$rgdppc_2000)
> dd<-d[complete.cases(d$rgdppc_2000),]
> dd$log_gdp_std<-dd$log_gdp/mean(dd$log_gdp)
> dd$rugged_std<-dd$rugged/max(dd$rugged)
> dd$cid<-ifelse(dd$cont_africa==1,1,2)

> dat_slim<- list(
+   log_gdp_std<-dd$log_gdp_std,
+   rugged_std<-dd$rugged_std,
+   cid<-as.integer(dd$cid)
+ )

> m9.1 <- ulam(
+   alist(
+     log_gdp_std ~ dnorm( mu , sigma ) ,
+     mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) ,
+     a[cid] ~ dnorm( 1 , 0.1 ) ,
+     b[cid] ~ dnorm( 0 , 0.3 ) ,
+     sigma ~ dexp( 1 )
+   ) , data=dat_slim , chains=1,cmdstan = TRUE)
Compiling Stan program...
Semantic error in '/tmp/Rtmp0QkL2I/model-1e4444cbe13df.stan', line 12, column 11 to column 14:
   -------------------------------------------------
    10:      b ~ normal( 0 , 0.3 );
    11:      a ~ normal( 1 , 0.1 );
    12:      mu = a[cid] + b[cid] * (rugged_std - 0.215);
                    ^
    13:      log_gdp_std ~ normal( mu , sigma );
    14:  }
   -------------------------------------------------

Identifier 'cid' not in scope. Did you mean 'add'?

make: *** [make/program:50: /tmp/Rtmp0QkL2I/model-1e4444cbe13df.hpp] Error 1

Error: An error occured during compilation! See the message above for more information.
In addition: Warning messages:
1: In is.na(symbols[[j]]$dims) :
  is.na() applied to non-(list or vector) of type 'symbol'
2: In is.na(symbols[[j]]$dims) :
  is.na() applied to non-(list or vector) of type 'symbol'

Is this to do with stan, or have I made a gross error somewhere in my code?
System info:

> cmdstanr::cmdstan_version()
[1] "2.30.1"
> packageVersion("rethinking")
[1] ‘2.21’
> version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          1.2                         
year           2021                        
month          11                          
day            01                          
svn rev        81115                       
language       R                           
version.string R version 4.1.2 (2021-11-01)
nickname       Bird Hippie `

You have <- instead of = in the dat_slim list. So there are no variable names. Use =.

I don't think that resolves the deprecation of the id, however, so I would leave the thread open until you refactor the ulam -> stan conversion?

So the above error was indeed not related ;-)

Right, the Experimental branch has a candidate deprecation fix. I haven't had problems, but I do want to write some more tests, and there are some advanced functions that still trigger warnings.