## Not run:
# library(coda)
# library(survival)
# library(spBayesSurv)
# ## True coeffs
# betaT = c(-1,1);
# ## True cumulative Baseline hazard
# Lambda0 = function(t) {t+log(1+t)};
# ## The Survival function:
# Sioft = function(t,x) exp( -Lambda0(t)*exp(sum(x*betaT)) ) ;
#
# tt=seq(0, 6, 0.01);
# plot(tt, Sioft(tt, c(0,1)), "l");
# lines(tt, Sioft(tt, c(0,0)), "l");
#
# ## The inverse for Sioft
# Sinv = function(u, x) uniroot(function (t) Sioft(t,x)-u, lower=0,
# upper=1e100, tol=.Machine$double.eps^0.5)$root
#
# ##-------------Generate data-------------------##
# ## Generate x
# n = 200;
# x1 = rbinom(n, 1, 0.5);
# x2 = rnorm(n, 0, 0.5)
# X = cbind(x1, x2);
# p = ncol(X); # number of covariates
# ## Generate survival times
# u = runif(n, 0, 1);
# tT = rep(0, n);
# for (i in 1:n){
# tT[i] = Sinv(u[i], X[i,]) + 1e-100;
# }
#
# ### interval-censored data:
# t1 = tT; t2 = tT;
# obs_num = 1+rpois(n, 2);
# for(i in 1:n){
# obs_time = c( NA, cumsum( rexp(obs_num[i]) ), NA);
# jj=2;
# while( (jj<(obs_num[i]+2)) & (obs_time[jj]<tT[i]) ){
# jj=jj+1;
# }
# t1[i] = obs_time[jj-1];
# t2[i] = obs_time[jj];
# }
# sum(is.na(t2))/n
#
# ## make a data frame
# d = data.frame(t1=t1, t2=t2, x1=x1, x2=x2, tT=tT);
#
# # MCMC parameters
# nburn <- 5000
# nsave <- 5000
# nskip <- 0
# ndisplay <- 1000
# mcmc <- list(nburn=nburn,
# nsave=nsave,
# nskip=nskip,
# ndisplay=ndisplay)
#
# # Prior information
# prior = list(a0 = 5, b0 = 1);
# # current state values
# state <- list(cpar=5);
#
# ##-------------PH-------------------##
# fit0=survreg(formula = Surv(t1, t2, type="interval2")~x1+x2, data=d,
# dist="weibull");
# summary(fit0)
# ptm<-proc.time()
# res = survregbayes(formula = Surv(t1, t2, type="interval2")~x1+x2, data=d,
# survmodel="PH", prior=prior, mcmc=mcmc, state=state);
# systime=proc.time()-ptm
# summary(res);
#
# ### trace plots
# par(mfrow = c(3,1));
# traceplot(mcmc(res$beta[1,]), main="beta1");
# traceplot(mcmc(res$beta[2,]), main="beta2");
# traceplot(mcmc(res$cpar));
#
# ####################################################################
# ## Get curves
# ####################################################################
# par(mfrow = c(1,2));
# ygrid = seq(-10,1,length=300); tgrid = exp(ygrid);
# ngrid = length(ygrid);
# xnew = c(0,1)
# xpred = cbind(c(0,0), xnew);
# nxpred = nrow(xpred);
# estimates=plot(res, xpred, tgrid);
# ## plots
# ## survival function when x=(0,0)
# i=1
# par(cex=1.5,mar=c(4.1,4.1,1,1),cex.lab=1.4,cex.axis=1.1)
# plot(tgrid, Sioft(tgrid, c(0,xnew[i])), "l", lwd=3,
# xlim=c(0,3), xlab="time", ylab="survival");
# polygon(x=c(rev(tgrid),tgrid),
# y=c(rev(estimates$Shatlow[,i]),estimates$Shatup[,i]),
# border=NA,col="lightgray");
# lines(tgrid, Sioft(tgrid, c(0,xnew[i])), "l", lwd=3);
# lines(tgrid, estimates$Shat[,i], lty=3, lwd=3, col=1);
# ## survival function when x=(0,0)
# i=2
# par(cex=1.5,mar=c(4.1,4.1,1,1),cex.lab=1.4,cex.axis=1.1)
# plot(tgrid, Sioft(tgrid, c(0,xnew[i])), "l", lwd=3,
# xlim=c(0,3), xlab="time", ylab="survival");
# polygon(x=c(rev(tgrid),tgrid),
# y=c(rev(estimates$Shatlow[,i]),estimates$Shatup[,i]),
# border=NA,col="lightgray");
# lines(tgrid, Sioft(tgrid, c(0,xnew[i])), "l", lwd=3);
# lines(tgrid, estimates$Shat[,i], lty=3, lwd=3, col=1);
# ## End(Not run)
Run the code above in your browser using DataLab