Survival Analysis Stan

Hi everyone,

I’m trying to implement the Cox model in stan, but it throws me the following error
Error in path.expand(path) : invalid ‘path’ argument

This is my code:

data {

  int<lower=0> n;

  int<lower=0> T;

  real<lower=0> obst[n]; 

  real<lower=0> t[T + 1]; 

  int<lower=0> fail[n]; 

  real x[n]; 
  real media[n];


transformed data {

  int Y[n, T];

  int dN[n, T]; 

  real c;

  real r; 

  for(i in 1:n) {

    for(j in 1:T) {

      Y[i, j] <- int_step(obst[i] - t[j] + .000001);

      dN[i, j] <- Y[i, j] * fail[i] * int_step(t[j + 1] - obst[i] - .000001);



  c <- 0.01; 

  r <- 0.1; 


parameters {

  real betaS; 
  real alpha;

  real<lower=0> dL0[T]; 


model {

  betaS ~ normal(0, 100);
  alpha ~ normal(0, 100);

  for(j in 1:T) {

    dL0[j] ~ gamma(r * (t[j + 1] - t[j]) * c, c);

    for(i in 1:n) {

      if (Y[i, j] != 0)  

        increment_log_prob(poisson_log(dN[i, j], Y[i, j] * exp(betaS * x[i]+alpha*media[i]) * dL0[j])); 




I hope you can help me!


That suggests a problem with how you are calling stan from R. Make sure the Stan program that you want to use is in your working directory. If that doesn’t work, post the line of code you are using to call stan.

Thanks for your answer, I’m using the following code:

fit <- stan(stan(model_code =, 
             data   = list(n=nrow(Survdata), T = T, t =t,media = media, obst = obst, 
                           x=Survdata$X1, fail = fail),                        
             iter   = 2000,                
             chains = 1))

I’m going to try your observation.

Sorry, the code is

fit <- stan(model_code =,
data = list(n=nrow(Survdata), T = T, t =t,media = media, obst = obst,
x=Survdata$X1, fail = fail),
iter = 2000,
chains = 1)

It is recommended to save your Stan code to a separate file, which will help yield better diagnostic messages when something goes wrong. So, try

fit <- stan("cox_new.stan", ...)

where cox_new.stan is the file in your working directory where you saved that Stan program.

Thanks a lot! is working!..Now I have problem with the arguments in the gamma distribution, because algorithm tell me that my values are negative.


Do you know how to incorporate time dependent covariates in cox using stan?


I don’t see how the arguments to dL0[j] ~ gamma(r * (t[j + 1] - t[j]) * c, c); can be negative unless the difference in times is negative, which would be weird. Are you now calling the gamma (log)-PDF differently. Time-dependent covariates are not a problem syntax-wise, but you need to be a lot more explicit about what you are trying to do.