1

It has been a while that I am trying to find a way in rjags to write a code for a Bayesian Weibull AFT Survival Analysis model with time-varying (time-dependent) covariates.

The data I am working on is about the duration from buying to disposal. Some of the records are right-censored. Also, I have some explanatory variables for each record like age, marital status, education level, and income.

I have got a code that does not contain time-varying covariates. This code correctly runs and its results are reasonable. Now I want to enhance it to include time-varying covariates like age. Here is my simple code.

mod_string_wei <- "model{

for(i in 1 : nn) {
  is.censored[i] ~ dinterval(tt[i], ee[i])
  tt[i] ~ dweib(alpha,lamda[i])
  lamda[i] <- exp(beta[1] + beta[2]*cc[i])
}

for (i in 1:2) {
  beta[i] ~ dnorm(0.0, 1.0/1.0e5)
}
alpha ~ dgamma(1,0.0001)

# Median survival time;
median0 <- log(2)/exp(beta[1])
median1 <- log(2)/exp(beta[1]+beta[2])

#mean survival time;
mean0 <- 1/exp(beta[1])
mean1 <- 1/exp(beta[1]+beta[2])
}"


#data preparation for the model
nn <- nrow(data.s)
tt <- data.s$duration
ee <- data.s$event
cc<- data.s$ageatstart
is.censored <- data.s$is.censored


#initialisation
set.seed(72)

#Parameters
params = c("beta", "alpha")

#initial values of the model
beta.mu = -8.0
beta.sigma = 5.0
beta.num = 2

alpha.shape = 3.0
alpha.rate = 4.0

inits1 = function() {
  inits = list(beta = rnorm(beta.num,beta.mu,beta.sigma), alpha = rgamma(1,alpha.shape,alpha.rate))
}


###############################     3-model Run    ###############################

chain.num = 3
adapt.num = 0

#initial model
mod.syd = jags.model(textConnection(mod_string_wei), data=data, inits=inits1, n.chains=chain.num, n.adapt = adapt.num)

#burn in count
burn.count = 800
update(mod.syd, burn.count) 

#sample model
thin.num = 1
iteration.num = 1000

mod.syd.sim = coda.samples(model=mod.syd,
                        variable.names=params, n.iter=iteration.num, thin = thin.num)


###############################     4-results     ###############################
#Binding results
mod.syd.csim = do.call(rbind, mod.syd.sim)

I would highly appreciate it if someone could help me add time-varying (time-dependent) covariates or find a tutorial/sample code on that.

Mary B
  • 13
  • 3
  • Welcome to SO! Your chances of getting an answer will greatly increase if you provide a simple, self-contained example of your problem. This would include toy input data, your code, any error messages, and expected output. [This post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) may be helpful. In this case, it's impossible to help you without knowing the format of your input data. – Limey Jun 19 '20 at 14:34
  • Thanks @Limey! I edited my question to include more specific info. Could you take a look and let me know what you think? – Mary B Jun 19 '20 at 15:03
  • Well, that's a start, and you're doing what I would do: start simple and build up the complexity. You've given us a `runjags` model string, but no input data, and no link between the dependents in the model (`cc[i]` for example) and you input variables (`age`, `income` etc). Which of them are time dependent? What nodes in the model do you want to monitor? Does your simple model compile and run? Are the results reasonable? You have to help us to help you. – Limey Jun 19 '20 at 15:12
  • Thank you @Limey! I edited my question again to answer all the questions you asked. – Mary B Jun 19 '20 at 15:25

0 Answers0