## Simulated data
rm(list=ls())
library(survival)
library(spBayesSurv)
library(coda)
## True conditional density
fiofy_x = function(y, x){
0.5*dnorm(y, -x, 1)+0.5*dnorm(y, x, 1);
}
## Generate data
n = 200;
x = runif(n, 0, 3)
y = rep(0, n);
uu = runif(n);
for(i in 1:n){
if(uu[i]<0.5){
y[i] = rnorm(1, -x[i], 1);
}else{
y[i] = rnorm(1, x[i], 1);
}
}
## right censored
y1=y;y2=y;
Centime = runif(n, 2, 4);
delta = (y<=Centime) +0 ;
length(which(delta==0))/n; ## censoring rate
rcen = which(delta==0);
y1[rcen] = Centime[rcen];
y2[rcen] = NA;
## make a data frame
## Method 1: in the interval-censoring notation:
## y1 is the left endpoint and y2 is the right endpoint.
## This way we could use Surv(y1, y2, type="interval2")
## Method 2: Because we have right-censored data,
## we could use y1 as the observed survival times and delta as the indicator.
## This way we could use Surv(y1, delta). This is the same as above.
d = data.frame(y1=y1, y2=y2, x=x, delta=delta);
##-------------fit the model-------------------##
# MCMC parameters
nburn=50; nsave=50; nskip=0;
# Note larger nburn, nsave and nskip should be used in practice.
mcmc=list(nburn=nburn, nsave=nsave, nskip=nskip, ndisplay=50);
prior = list(maxL=4, phiq0=0);
# Note please set 0
Run the code above in your browser using DataLab