在JAGS中逐步进行右删失生存分析

时间:2015-05-03 22:33:10

标签: survival-analysis mcmc jags

这是对SE早期帖子的一种跟进:https://stats.stackexchange.com/questions/70858/right-censored-survival-fit-with-jags

但是在这里,我希望看到一个完整的R脚本(从开始到结束)对JAGS中的右删失数据进行生存分析。我发现的所有网站都需要非常高的JAGS熟练程度,所以我很难理解如何从一行代码到另一行代码。我知道这要问很多......

无论如何,这里有一些生存数据的例子。组是t1,t2,t3。 NA指的是右删失数据(检查截止= 3)。

t1 <- c(1.73, NA, NA, NA, NA,0.77, NA, NA, NA, NA, NA, NA,0.5,1.06, NA, NA, NA, NA, NA,0.46, NA)
t2 <- c(1.42, NA, NA, NA, NA, NA,0.69,1.84, NA, NA, NA,1.47,1.6,1.8, NA, NA, NA, NA, NA,0.73, NA,1.28,3,2.97)
t3 <- c(0.88, NA, NA,1.65,1.75, NA, NA,1.01,1.46,1.95, NA, NA,2.93, NA,0.78,1.05,1.52, NA)


#Specify model in BUGS language


sink("model.txt")
cat("
model
{




}
",fill = TRUE)
sink()

#Bundle data
data<- list()

#Parameters monitored
parameters<-c()

#Initial values
inits <- list(

# MCMC settings
ni <-  
nt <- 
nb <- 
nc <- 


fit <- jags(data, inits, parameters, "model.txt", n.iter=ni, n.thin=nt, n.burnin=nb, n.chains=nc, working.directory = getwd())

我知道要提出这个问题很多,但我花了好几天时间试图将某些东西拼凑在一起而且我一直迷路/迷茫。我知道现在有包来运行这种分析,但我真的想学习如何从头开始自己构建它!谢谢读者!

1 个答案:

答案 0 :(得分:4)

我没有进行大量的生存分析(并且您没有说明您希望在此部分使用哪个发行版 - 有不同的选项),但是此代码应该让您开始进行间隔审查部分:

    library("runjags")

    # Your data
    t1 <- c(1.73, NA, NA, NA, NA,0.77, NA, NA, NA, NA, NA, NA,0.5,1.06, NA, NA, NA, NA, NA,0.46, NA)
    t2 <- c(1.42, NA, NA, NA, NA, NA,0.69,1.84, NA, NA, NA,1.47,1.6,1.8, NA, NA, NA, NA, NA,0.73, NA,1.28,3,2.97)
    t3 <- c(0.88, NA, NA,1.65,1.75, NA, NA,1.01,1.46,1.95, NA, NA,2.93, NA,0.78,1.05,1.52, NA)

    # Combine these into a single vector to make the code cleaner
    alldata <- rbind(cbind(t1, 1), cbind(t2, 2), cbind(t3, 3))
    T.obs <- alldata[,1]
    Group <- alldata[,2]
    N <- length(T.obs)

    # The censoring setup - in this case 0 for T.obs < 3 and 1 for T.obs > 3
    Censoring <- as.numeric(is.na(T.obs))
    Breakpoint <- 3

    # A simple JAGS model:
    m <- "
    model{
        for(i in 1:N){
            # The censoring part:
            Censoring[i] ~ dinterval(T.obs[i], Breakpoint)
            # The regression part - you may well want to change dexp to something else:
            T.obs[i] ~ dexp(rate[Group[i]])
        }   
        rate[1] ~ dgamma(0.01, 0.01)
        rate[2] ~ dgamma(0.01, 0.01)
        rate[3] ~ dgamma(0.01, 0.01)

        #data# N, Censoring, Breakpoint, T.obs, Group
        #monitor# rate, T.obs
    }
    "

    # One of the things we need to do is help JAGS initialise T.obs:
    T.obs.init <- ifelse(is.na(T.obs), 4, NA)

    # The function call:
    results <- run.jags(m, n.chains=2, inits=list(T.obs=T.obs.init))

    # Look at results:
    results

这使用runjags包进行一些自动收敛等诊断,并允许在模型代码中而不是R代码中简单使用#data#和#monitor# - 有关此包的更多信息,请参阅http://runjags.sourceforge.net/quickjags.html

[编辑:没有必要监控T.obs,但这表明T.obs中的缺失值都被估计为&gt; 3,观察值与期望值一样是非随机的]