Getting more details on the error message

I have encountered the ‘Error in sampler$call_sampler(args_list[[i]]) : Initialization failed’. But not sure how to figure out exactly what went wrong. How can I get more details on the error? Any help is appreciated!

data {
  int<lower=0> J;
  real<lower=0> t[J];
  
  int<lower=0> n;
  real adt[n];
  real evt[n];
  
  real alpha0;
  real beta0;
}
transformed data{
  real<lower=0> delta_t[J];
  delta_t[1] = t[1];
  for (j in 2:J){
    delta_t[j]=t[j]-t[j-1];
  }
}
parameters {
  vector[J] lambda;
}
model {
  for (j in 1:J) lambda[j] ~ gamma(alpha0, beta0); 
  for (i in 1:n){
    target += -lambda[1]*delta_t[1]*(adt[i] >= t[1]) +
          log1m_exp(-lambda[1]*delta_t[1])*(adt[i]<=t[1] && evt[i]==1);
  }
  for (j in 2:J){
    for (i in 1:n){
      target += -lambda[j]*delta_t[j]*(adt[i] >= t[j]) +
            log1m_exp(-lambda[j]*delta_t[j])*(t[j-1]<adt[i] && adt[i]<=t[j] && evt[i]==1);
      }
    }
}

R code:

mydata_mod ← list(
J = J,
t = myt,
n = n,
adt = adt,
evt = evt,
alpha0 = 1/1000,
beta0 = 48/log(2)/1000
)

fit ← sampling(sm,
data = mydata_mod, # named list of data
# seed=1234,
chains = 1, # number of Markov chains
warmup = 1000, # number of warmup iterations per chain
iter = 2500, # total number of iterations per chain
cores = 2, # number of cores (could use one per chain)
refresh = 0 # no progress shown
)

I was able to run this with some made up data:

library(cmdstanr)

mod = cmdstan_model("test5.stan")

fit = mod$sample(data = list(J = 2,
                             t = c(1,2),
                             n = 2,
                             adt = c(1, 2),
                             evt = c(1, 2),
                             alpha0 = 1,
                             beta0 = 2))

I suspect there is a problem with one of the variables J, myt, n, adt, or evt. They should match the types defined in the Stan program. J and n should be integers, and myt, adt, and evt should be vectors (lists might work too but I’m not sure).

Thanks! You are right. The issue is the type of variables and parameters. I tried to update the lambda to real lambda[J], it seems solve the problem. I am always confused when to the difference between the two below. Any insight?

vector[J] x
real x[J]

As far as input goes, I don’t think there is. Are you seeing a difference?

Using them in the code, a vector should be thought of as one object (like the think you do vector math on and such). An array of reals is an array of little objects – it is not a mathematical vector. You can convert between the two with to_array_1d and to_vector.

For my case here, I had to change vecotr[J] lambda to real lambda[J]. Using the former, I have the error messages, while after i change, the code run without problems.

Oh that’s surprising. This sounds like something that we should fix.

Do you know what type the input argument was?

Here is the stan code

data {
  int<lower=0> J;
  real<lower=0> t[J];
  
  int<lower=0> n;
  real adt[n];
  real evt[n];
  
  real alpha0;
  real beta0;
}
transformed data{
  real<lower=0> delta_t[J];
  delta_t[1] = t[1];
  for (j in 2:J){
    delta_t[j]=t[j]-t[j-1];
  }
}
parameters {
  // for piecewise hazard;
  real<lower=0> lambda[J];
}
model {
  for (j in 1:J){
    lambda[j] ~ gamma(alpha0, beta0);
    if (j==1){
      for (i in 1:n){
        target += -lambda[1]*delta_t[1]*(adt[i] >= t[1] && !(adt[i]<=t[1] && evt[i]==1)) +
          log1m_exp(-lambda[1]*delta_t[1])*(adt[i]<=t[1] && evt[i]==1);
          }
    }else{
      // lambda[j] ~ gamma(alpha0, beta0);
      for (i in 1:n){
        target += -lambda[j]*delta_t[j]*(adt[i] >= t[j] && !(t[j-1]<adt[i] && adt[i]<=t[j] && evt[i]==1)) +
              log1m_exp(-lambda[j]*delta_t[j])*(t[j-1]<adt[i] && adt[i]<=t[j] && evt[i]==1);
        }
      }
  }
}

when I change the parameter to

vector[J] lambda;

I have issues. Not sure if it matters, the data used to fit the model have J smaller than n.

Oh sorry, I meant the r-code that calls the Stan model. What type are the things you pass in to the data argument? I think this would be a problem with how IO is handled at the interface.

I kept updating my model, but I think below is the one I used to call this:

mydata_mod <- list(
  J = J,
  t = myt,
  n = n,
  adt = adt,
  evt = evt
)

fit <- sampling(sm,
                data = mydata_mod,      # named list of data
                # seed=1234,
                chains = 4,             # number of Markov chains
                warmup = 1000,          # number of warmup iterations per chain
                iter = 3500,            # total number of iterations per chain
                cores = 2,              # number of cores (could use one per chain)
                refresh = 0             # no progress shown
)

Oh oh oh sorry, yes lambda is a parameter my apologies, I thought it was data.

Looking at this model more closely, I’m worried there is a segfault or something weird happening here.

I do not think there should be a difference in using a vector or an array in the code you posted.

Do you mind posting the data you used to run the model? That way I can look at this more closely with the two different things and see if I can reproduce the error on my computer.

You can dump your data to a Stan-friendly format using:

stan_rdump(names(mydata_mod), "mydata.dat", env = list2env(mydata_mod))

Here is the data. Part of the reason could also be my J is the number of unique values in adt. Once I reduce it, whether I use vector[J] lambda or real lambda[J], doesn’t matter any more. Looking for more insights in thismydata.txt|attachment (480 Bytes)

Try running again with cores=1 – that will turn off parallelization and should let you see the full error message from Stan.

The attachment isn’t working for me (it’s showing up like the link got corrupted somehow). Can you try again?

Copied below directly

J <- 12
t <- 
c(3, 7, 8, 11, 17, 18, 25, 27, 32, 42, 56, 57)
n <- 30
b <- 1
cov <- 
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
.Dim = c(30, 1))
adt <- 
c(57, 25, 8, 25, 57, 7, 18, 18, 57, 57, 11, 42, 57, 57, 8, 17, 27, 57, 3, 57, 57, 57, 57, 56, 57, 57, 57, 32, 57, 57)
evt <- 
c(0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0)
alpha0 <- 0.01
beta0 <- 0.692493619626703
B0 <- 5
1 Like

Thanks for the data! I just tried this out.

I think the vector is missing a lower bound.

So:

real<lower=0> lambda[J];

should be:

vector<lower=0>[J] lambda;

The problem is just that the constraints on the parameters should match the support of the distribution.

So with this line:

for (j in 1:J) lambda[j] ~ gamma(alpha0, beta0);

You need to make sure and constrain lambda to be positive to keep the sampler from moving it into an invalid range.