Errors when doing compilation in RStan model with integration

For all Stan experts and users
@bgoodri @ScottAlder @bbbales2 @stemangiola
when compiling following code, I am getting errors:
SAMPLING FOR MODEL ‘a3fc5f3f8314f35eb844526c8bbadc9d’ NOW (CHAIN 1).
Chain 1: Rejecting initial value:
Chain 1: Log probability evaluates to log(0), i.e. negative infinity.
Chain 1: Stan can’t start sampling from this initial value.
Chain 1:
Chain 1: Initialization between (-2, 2) failed after 1 attempts.

I can’t understand how to solve these. Could you please tell me why is this error getting?

I am copying my stan file for your reference,

   real fun_integral(real x, 
                    real xc, 
                    real[] theta, 
                    real[] x_r, 
                    int[] x_i){
      real mu = theta[1];
      real sigma2 = theta[2];
      real fun;
      fun = (0.3*exp(-square(log(x)-mu)/2*sigma2)/sqrt(2*pi()*sigma2)*x);

data{ //variables in the likelihood function
   int n;
   int m;
   matrix[n,m] MT;
   int K;

transformed data {
  real x_r[0];
  int x_i[0];

   real<lower=4.0,upper=4.5> mu; 
   real<lower=0.01,upper=0.05> sigma2; 

   mu ~ uniform(4.0,4.5);
   sigma2 ~ uniform(0.1,0.5);

for(i in 1:5){
vector[3] V; 
vector[5] t;
for(j in 2:5){
     t[j] = t[j-1]+1;
V[1] = integrate_1d(fun_integral,t[1],t[2],{mu,sigma2},{0.0}, x_i, 1e-8);
target += pow(V[1],MT[i,1]);

for(k in 2:K){
V[k] = integrate_1d(fun_integral,t[k],t[k+1],{mu,sigma2},{0.0}, x_i, 1e-8);
target += pow(V[k],MT[i,k]);

Here, MT is a matrix and


Thanks in advance

Can you post the code that you are using to run the model?

1 Like

I think the way to debug stuff like this is to try to break things down into a small a piece as possible.

I gues I’d start with:

  1. If you don’t have the integrals, do things work?
  2. Are the integrals returning the values you expect? (add print statements to your code to debug this)

Hi Ara, @Ara_Winter

Please find this;

n_chains <- 2
for (chain in 1:n_chains) inits[[chain]]<-initializer()
fit<-stan(model_code = data_code, data = file_data,  warmup = 150,
          iter = 300, chains =n_chains,thin = 1,init=inits)```

@bbbales2 Thanks for the reply, I don’t how to check whether each line gives me an output or not using c++, but I did it in R, so have to change this integral_1d() to integration(), Am I correct? So then, I could have results. But, I didn’t check this code without the integration,I ll try it now.

This is how I defined initializers for my code and stan() line.

initializer <- function() list("mu"=runif(1,4.0,4.5),"sigma2"=runif(1,0.01,0.05))
n_chains <- 2
for (chain in 1:n_chains) inits[[chain]]<-initializer()
fit<-stan(model_code = file_code, data =file_data,  warmup = 150,
          iter = 300, chains =n_chains,thin = 1,init=inits)

This is a part of my data matrix,
Age n1 s1 r1 n2 s2 r2
[1,] 50 3993 7 1 3985 6 1
[2,] 51 4020 9 1 3983 7 1
[3,] 52 4009 10 1 3982 8 1
[4,] 53 4025 12 2 3979 10 2
[5,] 54 4059 14 2 3977 11 2
[6,] 55 3939 17 2 3974 13 2

Your corporation is highly appreciated.

print debugging works in Stan, so I might add prints around:

V[1] = integrate_1d(fun_integral,t[1],t[2],{mu,sigma2},{0.0}, x_i, 1e-8);

as a start.

Like, print(V[1]) and print(t[1], t[2], mu, sigma2) and see if everything makes sense (and you can verify the integrals externally in R).

You can use rstan’s expose_stan_functions ( to expose functions you write in Stan to R to test them as well.

I added the commands,


and it printed for all i (looping over i = 1:5)

Chain 1: 46.0932

Ooo, maybe add commas and it’ll be more readable. For instance,

print(mu, ", ", sigma2);

Then once you have that you can decide if the integrals make sense or not

I can do printing V[1] and t[1], t[2], mu, sigma2 with looping over i, but not the stan(), then I updated my rstan package version as 2.21.1, then it is giving me such errors,

Why is that? I am struggling to have an output.

Compilation ERROR, function(s)/method(s) not created! C:/rtools40/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/8.3.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: file22a8da71e8b.o:file22a8da71e8b.cpp:(.text+0x9ac): undefined reference to tbb::internal::task_scheduler_observer_v3::observe(bool)' C:/rtools40/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: file22a8da71e8b.o:file22a8da71e8b.cpp:(.text+0x18b7): undefined reference to rstan::stan_fit::stan_fit(SEXPREC*, int)’
C:/rtools40/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/8.3.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: file22a8da71e8b.o:file22a8da71e8b.cpp:(.text+0x9ee): undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)’
C:/rtools40/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/8.3.0/…/…/…/…/x86_64-w64-mingw32/bin/ld.exe: file22a8da71e8b.o:file22a8da71e8b.cpp:(.text$_ZN3tbb8internal26task_scheduler_observer_v3D1Ev[_ZN3tbb8internal26task_scheduler_observe
In addition: Warning message:
In file.remove(c(unprocessed, processed)) :
cannot remove file ‘C:\Users\erandi\AppData\Local\Temp\RtmpmE49kQ\file22a8bbc54de.stan’, reason ‘No such file or directory’
Error in sink(type = “output”) : invalid connection

Thanks a lot.

Check here for the latest install instructions for Windows: Rstan on Windows, there’s been some difficulties lately. There should be build links for R 4.0 in there in the first post.