Is possible to define a random limit for a loop in JAGS?

396 views Asked by At

I am trying to implement a Weibull proportional hazards model with a cure fraction following the approach outlined by Hui, Ibrahim and Sinha (1999) - A New Bayesian Model for Survival Data with a Surviving Fraction. However, I am not sure if it is possible to define a random limit for a looping in JAGS.

library(R2OpenBUGS)
library(rjags)

set.seed(1234)
censored <- c(1, 1)
time_mod <- c(NA, NA)
time_cens <- c(5, 7)
tau <- 4
design_matrix <- rbind(c(1, 0, 0, 0), c(1, 0.2, 0.2, 0.04))

jfun <- function() {

  for(i in 1:nobs) {
    censored[i] ~ dinterval(time_mod[i], time_cens[i])
    time_mod[i] <- ifelse(N[i] == 0, tau, min(Z))

    for (k in 1:N[i]){
      Z[k] ~ dweib(1, 1)
    }

    N[i] ~ dpois(fc[i])
    fc[i] <- exp(inprod(design_matrix[i, ], beta)) 

  }

  beta[1] ~ dnorm(0, 10)
  beta[2] ~ dnorm(0, 10)
  beta[3] ~ dnorm(0, 10)
  beta[4] ~ dnorm(0, 10)
}

inits <- function() {
  time_init <- rep(NA, length(time_mod))
  time_init[which(!status)] <- time_cens[which(!status)] + 1
  out <- list(beta = rnorm(4, 0, 10),
              time_mod = time_init,
              N = rpois(length(time_mod), 5))
  return(out)
}

data_base <- list('time_mod' = time_mod, 'time_cens' = time_cens,
                  'censored' = censored, 'design_matrix' = design_matrix, 
                  'tau' = tau,
                  'nobs' = length(time_cens[!is.na(time_cens)]))


tc1 <- textConnection("jmod", "w")
write.model(jfun, tc1)
close(tc1)

# Calling JAGS
tc2 <- textConnection(jmod)
j <- jags.model(tc2,
                data = data_base,
                inits = inits(),
                n.chains = 1,
                n.adapt = 1000)

I observed the below error:

Error in jags.model(tc2, data = data_base, inits = inits(), n.chains = 1,  : 
  RUNTIME ERROR:
Compilation error on line 6.
Unknown variable N
Either supply values for this variable with the data
or define it  on the left hand side of a relation.
1

There are 1 answers

2
Marcelo Ventura On BEST ANSWER

I am not entirely certain, but I am pretty sure that you cannot declare a random number of nodes in BUGS in general, so it would not be a specific JAGS' quirk.

Nevertheless, you can get a way around that.

Since BUGS is a declarative language instead of a procedural one, it is enough to declare an arbitrary but deterministic number of nodes (let's say "large enough") and then associate only a random number of them with a distribution and with observed data, leaving the remaining nodes deterministic.

Once you have observed the maximum value of N[i] (let's say N.max), you can pass it as a parameter to JAGS and then change this code of yours:

for (k in 1:N[i]){
  Z[k] ~ dweib(1, 1)
}

into this:

for (k in 1:N.max){
  if (k <= N[i]){
    Z[k] ~ dweib(1, 1)
  } else {
    Z[k] <- 0
  }
}

I hope this will do the trick in your case. So please give feedback latter about it.

Needless to say, if you have some non-zero, observed data associated to a deterministic Z[k], then all hell breaks loose inside Jags...