# NOT RUN {
## this function is used in generating epsilon from its CDF
rsep= function(u,r)
{
if(r==0)
return( log(-log(1-u)) )
else
return( log((exp(-r*log(1-u))-1)/r) )
}
n= 30 #200 # sample size; small number used for quick demonstration only
rfix= 1.0
m= 3 #10 # imputed datasets for failure time; small number used for quick demonstration only
nrep= 1 # number of repeated measurement of error prone covariate
gridlen= 0.1
sigma2u= 0.5
result= NULL
ah=1
set.seed(ah)
# z1= rnorm(n, mean= 0, sd=1)
z1= (rgamma(n,shape=2,scale=2)-4)/sqrt(8)
z2= rbinom(n,1,0.5)
ugen= runif(n)
ep= rsep(ugen,rfix)
truebeta= c(-1,1)
logt= -truebeta[1]*z1 -truebeta[2]*z2 + ep + 3
ttime= exp(logt)
cen= runif(n,0,0.0001)
## creating tau matrix to locate the actual times in each rows and form the corresponding intervals
len= 0.15
taumat= matrix(0,n,10)
taumat[,10]= 9000000000 # if you're changing this, change rcpos below
taumat[,2]= cen
for(i1 in 3:9)
taumat[,i1]= taumat[,2]+(i1-1)*len
## now forming the intervals
right1= rep(0,n)
left= rep(0,n)
for(i2 in 1:n)
{
lenleft= length(which(taumat[i2,2:9]<ttime[i2]))
leftpot= rep(0,8)
leftpot[1:lenleft]= 1
missvec1= c(rbinom(4,1,0.7),rbinom(4,1,0.5))
left[i2]= max(leftpot*missvec1*taumat[i2,2:9])
if(left[i2]==0)
left[i2]= taumat[i2,2]
lenright= length(which(taumat[i2,2:10]>ttime[i2]))
rightpot= rep(0,9)
rightpot[(9-lenright+1):9]= 1
missvec2= c(rbinom(4,1,0.7),rbinom(4,1,0.5),1)
right1temp= rightpot*missvec2*taumat[i2,2:10]
right1[i2]= min(right1temp[right1temp!=0])
}
rcpos= which(right1==9000000000)
lrcpos= length(rcpos)
notrcpos= (1:n)[-rcpos]
delta_temp= rep(1,n) # del=1 are uncensored observations
delta_temp[rcpos]= 0
k= sum(delta_temp) # number of data points that are not right censored
## measurement error generation
umat= matrix((rgamma(n*nrep,shape=2,scale=2)-4)*0.5/sqrt(8),n,nrep)
wmat= z1+umat
datamat= cbind(left,right1,delta_temp,z2)
ntimp= m
nximp= 5 #20 # number of x imputations; small number used for quick demonstration only
library(icemelt)
out_imw1= imw1(datamat, wmat, rfix, gridlen, ntimp, nximp, sigma2u)
out_imw1
# }
Run the code above in your browser using DataLab