# NOT RUN {
# Generate an example data
require(foreach)
n <- 300 # Sample size
p <- 20 # Number of covariates
bet0 <- c(1, -1, 1, -1, rep(0,p-4)) # True values of regression coefficients
f1 <- function(z) sin(2*pi*z) # True effects of z1
f2 <- function(z) cos(2*pi*z) # True effects of z2
set.seed(1)
x.example <- matrix(rnorm(n*p,0,1),n,p) # Generate x covariates matrix
z.example <- cbind(runif(n,0,1),runif(n,0,1)) # Generate z covariates matrix
T.example <- c()
for (i in 1:n){
T.example[i] <- rexp(1,exp(x.example%*%bet0+
f1(z.example[,1])+f2(z.example[,2]))[i]) # Generate true failure times
}
timep <- seq(0,3,,10)
LR.example <- c()
for (i in 1:n){
obsT <- timep*rbinom(10,1,0.5)
if (max(obsT) < T.example[i]) {LR.example <- rbind(LR.example,c(max(obsT), Inf))} else {
LR.example <- rbind(LR.example,c(max(obsT[obsT<T.example[i]]), min(obsT[obsT>=T.example[i]])))
}
} # Generate interval-censored failure times
# Fit Cox's model with penalized estimation
model1 <- CoxICPen.XZ(LR = LR.example, x = x.example, z = z.example, lamb = 100, pen = "RIDGE")
beta.initial <- model1$beta
model2 <- CoxICPen.XZ(LR = LR.example, x = x.example, z = z.example,
beta.initial = beta.initial, pen = "BAR")
model2$beta
# Plots of covariate effects of z
par(mfrow=c(1,2))
plot(model2$f.est.all$z1, model2$f.est.all$f1, type="l", ylim=c(-1,2),
xlab="z1", ylab="f1")
lines(model2$f.est.all$z1, f1(model2$f.est.all$z1), col="blue")
legend("topright", col=c("black","blue"), lty=rep(1,2), c("Estimate", "True"))
plot(model2$f.est.all$z2, model2$f.est.all$f2, type="l", ylim=c(-1,2),
xlab="z2", ylab="f2")
lines(model2$f.est.all$z2, f2(model2$f.est.all$z2), col="blue")
legend("topright", col=c("black","blue"), lty=rep(1,2), c("Estimate", "True"))
# }
Run the code above in your browser using DataLab