setwd("/usr1/home/yili/daiwei/ordinary\ boot") library(survival) library(nnet) ############################################### ##### read in data and specify covariates ##### ############################################### data<-read.table("prosdata.dat", header=T) data <- data[data$RX==1, ] ######## exclude RX = 0 #d: event indicator where 0:censored, 1: Cancer, 2:CVD, 3 Others survtime<-data$X n <- length(survtime) epsilon<-data$d covdata<-data[, 4:10] ############ remove RX from the covariate matrix cov<-data.matrix(covdata) covdim<-length(cov[1,]) tau0=24 w <- (survtime <= tau0)* (epsilon !=0) + (survtime > tau0) candset <- seq(n)[w==1] n.cand <- length(candset) covselect<-cov[candset,] response<- ((survtime<=tau0)*(epsilon==1))[candset] multresp<- ( 2*(survtime<=tau0)*(epsilon==1) +3*(survtime<=tau0)*(epsilon==2) +4 *(survtime<=tau0)*(epsilon==3) +1*(survtime>tau0) )[candset] cen <- (epsilon == 0) ##### kernel function K<-function(x,v,h) { 1/sqrt(2*pi)*1/h* exp( -0.5* (x-v)^2/h^2) } ############## weighted Kaplan-Meier estimate function ######## ############################################################### km.est <- function(t, censor, tau, w) { n <- length(t) fit <- survfit(Surv(t,censor)~1, weights = w) time <- fit$time surv <- fit$surv ind <- function(v) { junktime <- min(v) sum(time < junktime)+1 } surv[apply(cbind(t, rep(tau, n)), 1, ind)] } censurv <- km.est(survtime, cen, tau0, rep(1, n)) weight <- 1/censurv[candset] z0 <- glm(formula = response ~ covselect, family = binomial, weights = weight) coefvec <- data.matrix(z0$coef) indscore <- coefvec[1]+ covselect%*%coefvec[2:(covdim+1)] censurv.select <- censurv[candset] y1 <- ((survtime<=tau0)*(epsilon==1))[candset] y2 <- ((survtime<=tau0)*(epsilon==2))[candset] y3 <- ((survtime<=tau0)*(epsilon==3))[candset] y <- cbind(y1, y2, y3) ###### partition ############### #################################### set.seed(47893079) k <- 10 n.select <- length(candset) interval <- floor(n.select/k) n.interval <- (1+interval)*k - n.select size <- c(rep(interval, n.interval), rep(interval+1, k - n.interval)) end <- cumsum(size) ## starting points of partition start <- c(1, (end + 1)[-length(end)]) ## ending points of partition random.order <- sample(1:n.select, n.select, replace = FALSE) ###### cross-validation ################### ############################################ n.h.10 <- 200 h.seq.10 <- 0.01*c(1:n.h.10)## candidate h values likehd.10 <- rep(0, n.h.10) ## record likelihood sums for (i in 1:n.h.10) { h <- h.seq.10[i] cat(i, "\n") for (j in 1:k) { index <- random.order[start[j]:end[j]] score.train <- indscore[-index] response.train <- multresp[-index] for (m in index) { v0 <- indscore[m] reweight <- K(score.train, v0, h)/censurv.select[-index] scoreselect <- score.train - v0 z2 <- multinom(response.train ~ scoreselect, weights = reweight, maxit = 1000, trace = FALSE) coeff <- coef(z2)[ ,1] l <- (crossprod(y[m, ],coeff) - log(1+sum(exp(coeff))))/censurv.select[m] likehd.10[i] <- likehd.10[i] + l } } } write.table(cbind(h.seq.10, likehd.10), "likelihood 10_fold tau24.txt") pdf("h(tau24 RX1) 10-fold.pdf") par(pch = 20, cex = 0.7) plot(likehd.10 ~ h.seq.10, ylab = "likelihood", xlab = "h") plot(likehd.10[10:100] ~ h.seq.10[10:100], ylab = "likelihood", xlab = "h") plot(likehd.10[90:200] ~ h.seq.10[90:200], ylab = "likelihood", xlab = "h") dev.off() ###### leave-one-out cross-validation ##### ############################################ n.h.l <- 200 h.seq.l <- 0.01*c(1:n.h.l)## candidate h values likehd.l <- rep(0, n.h.l) ## record likelihood sums for (i in 1:n.h.l) { h <- h.seq.l[i] cat(i, "\n") for (j in 1:n.select) { v0 <- indscore[j] reweight <- K(indscore[-j], v0, h)/censurv.select[-j] scoreselect <- indscore[-j] - v0 z2 <- multinom(multresp[-j] ~ scoreselect, weights = reweight, maxit = 1000, trace = FALSE) coeff <- coef(z2)[ ,1] l <- (crossprod(y[j, ],coeff) - log(1+sum(exp(coeff))))/censurv.select[j] likehd.l[i] <- likehd.l[i] + l } } write.table(cbind(h.seq.l, likehd.l), "likelihood leave_one_out tau24.txt") h.seq.l[likehd.l==max(likehd.l)] pdf("h(tau24 RX1) leave_one_out.pdf") par(pch = 20, cex = 0.7) plot(likehd.l ~ h.seq.l, ylab = "likelihood", xlab = "h") plot(likehd.l[20:100] ~ h.seq.l[20:100], ylab = "likelihood", xlab = "h") plot(likehd.l[100:200] ~ h.seq.l[100:200], ylab = "likelihood", xlab = "h") dev.off()