dir = 'YOUR DIRECTORY HERE' source(analysis_jointcure_KIM_func) ########################################################## #### #### Define variables #### ########################################################## ### Longitudinal Data Input YE <- cbind(ID, VISIT.NO, Y_t, TIME) ZZ1 <- cbind(COVARIATES TO FIT LONGITUDINAL MODEL) ### Cure-survival Data Input TE <- cbind(DELTA, Xi, ID, CURE) Z2 <- cbind(COVARIATES TO FIT CURE-SURVIVAL MODEL) ### DELTA, N DELTA <- TE[ ,1] N <- nrow(TE) # ni = vector of #of longitudinal outcomes for each subject ni <- rep(0, N) for(i in 1:N){ ni[i] <- length(which(YE[ ,1] == TE[i,3])) } # # of Y/terminal events my <- dim(YE)[1]; mc <- sum(DELTA) # indIDY = Index matrix of the ith subject's longitudinal outcomes indIDY <- matrix(0, N, my) for (i in 1:N){ indIDY[i, which(YE[ ,1]==TE[i,3])] <- 1 } # I(Xk <= Xi ) ind_F <- ifelse(matrix(TE[ ,2],N,N,byrow=TRUE) <= matrix(TE[ ,2],N,N), 1, 0) # I(Xj= Xi) ind_XjXi <- ifelse(matrix(TE[ ,2],N,N,byrow=TRUE) >= matrix(TE[ ,2],N,N), 1, 0) ind_XjXi[ ,which(CURE==1)] <- 0 ########################################################## # Set summary points for F(t) ########################################################## # I(Xi <= testpt) testpt <- c(0.25, 0.5, 0.75)*max(DELTA*TE[ ,2]) npt <- length(testpt) ind_testpt <- ifelse(matrix(TE[which(DELTA==1),2],npt,mc,byrow=TRUE) <= matrix(testpt,npt,mc), 1, 0) # # of params nA<- ncol(ZZ1); nB<- ncol(Z2); nY <- nA+1 ; nC <- nB+1+N ; nb <- 1; npar <- nY + nC + nb; # # of params for covariance matrix nCf = nB+1+mc; ################################################################### # # Main Analysis # ################################################################### maxiter = 300; epsilon= .005 report <- NULL ################################################################### # Initials ################################################################### sigma2_e0=0.5; psi0= 0 ; f0 <- rep(0, N); f0[DELTA==1] <- 1/mc r0=0.5 alpha0= rep(0 , nA); beta0= rep(0, nB); wt = TE[which(DELTA==1) ,2] indT_Xi <- ifelse(matrix(TE[ ,2],N,mc) >= matrix(wt,N,mc, byrow=TRUE), 1, 0) f00 <- 1/apply(indT_Xi,2,sum) f0[DELTA==1] = f00/sum(f00) par_Y <- c(alpha0, sigma2_e0) par_C <- c(beta0, psi0, f0) par_b <- r0 n.conv <- NULL newpar_Yall <- NULL newpar_Call <- NULL newpar_ball <- NULL uplambda_all <- NULL iter_all <- NULL for (iter in 1:maxiter){ ################################################################### # E-step ################################################################### bbv <- Tbb(par_Y, par_C, par_b) wwN <- GetWeight(bbv, par_C, par_b) ################################################################### # M-step ################################################################### Mstep <- M_step(bbv, wwN, par_Y, par_C, par_b) newpar_Y <- Mstep[1:nY] newpar_C <- Mstep[(nY+1):(nY+nC)] newpar_b <- Mstep[(nY+nC+1):npar] newf <- newpar_C[(nB+2):nC] uplambda <- Mstep[npar+1] newpar_Yall <- rbind(newpar_Yall, newpar_Y) newpar_Call <- rbind(newpar_Call, newpar_C) newpar_ball <- rbind(newpar_ball, newpar_b) uplambda_all <- rbind(uplambda_all, uplambda) error <- c(newpar_Y-par_Y,newpar_C-par_C,newpar_b-par_b) n.conv <- rbind(n.conv, c(iter, max(abs(error)) )) if(max(abs(error))< epsilon){ iter_all <- rbind(iter_all, iter) break } par_Y = newpar_Y; par_C = newpar_C; par_b = newpar_b; } # end of 'iter' # EndT <- date() loglik <- GetLogLik(newpar_Y, newpar_C, newpar_b, ZZ1, Z2) loglik ####################################################### # Summarize results ####################################################### COV <- covest(newpar_Y, newpar_C, newpar_b) errCOV= COV[[2]]; COV=COV[[1]]; var_Y <- COV[1:nY, 1:nY] var_C <- COV[(nY+1):(nY+nCf-1), (nY+1):(nY+nCf-1)] # -1 comes from removing fN=1-f1-f2-... var_b <- COV[(nY+nCf-1+1), (nY+nCf-1+1)] dg_lastf <- rbind(diag(nCf-1), c(rep(0, nB+1), rep(-1, mc-1))) var_CC <- dg_lastf %*% var_C %*% t(dg_lastf) gC= rbind(cbind(diag(nB+1), matrix(0, nB+1, mc)), cbind(matrix(0, npt, nB+1), ind_testpt)) estpar_C <- gC %*% c(newpar_C[1:(nB+1)], newf[which(DELTA==1)]) estvar_C <- gC %*% var_CC %*% t(gC) ### dgC = gC estvar <- c(diag(var_Y), diag(estvar_C), var_b) min(estvar) if (min(estvar)>0) { estsd <- sqrt(estvar) estpar <- c(newpar_Y, estpar_C, newpar_b) ### Coverage Probability ### estpar == << nA, sigma2_e, nB, psi, F(0.5), F(1.0), F(1.5), sigma2_b >> NPidx <- (nA+1+nB+1+1) : (nA+1+nB+1+npt) VARidx <- c(nA+1, nA+1+nB+1+npt+1) Test <- estpar; Tsd <- estsd; #Test[NPidx] <- log(estpar[NPidx]) #Tsd[NPidx] <- estsd[NPidx]/estpar[NPidx] Z_value = Test/Tsd P_value = 2*pnorm(abs(Z_value), 0, 1, lower.tail=FALSE) tempreport <- cbind(estpar, estsd, Z_value, P_value) report <- rbind(Model, loglik, tempreport) err.info <- t(c(iter, max(abs(error)), errCOV)) colnames(err.info) <- c('iter', 'error', 'errCOV') } cn <- c("PAR", "SE", "Z_value", "P_value") colnames(report) <- cn round(report, digits=4) ################################################## #### function of codes #################### pwd #dir = 'YOUR DIRECTORY HERE' INDEX1 <- 1 # transformation INDEX2 <- 1 # transformation param Tr.f <- c('LogTr', 'BoxTr') H.f <- (0:10)/10 if (INDEX1==1){ rHL <- H.f[INDEX2] } else if (INDEX1==2){ rHB <- H.f[INDEX2] } ################################################################### # # Gauss-Hermite quadrature # ################################################################### GHQ <- read.table("GHQ_node_weight.txt",header=TRUE,sep = " ") NGHQ <- nrow(GHQ) node <- GHQ[ ,2] weight <- GHQ[, 3] ww <- weight Nnode <- NGHQ library(MASS) ################################################################### # "H" functions ################################################################### ### (Log transformation) if (INDEX1 == 1){ if (rHL == 0){ H <- function(x) {x} Hinv <- function(x) {x} H1d <- function(x) {1} H2d <- function(x) {0} H3d <- function(x) {0} } if (rHL > 0){ H <- function(x) {log(1+rHL*x)/rHL} Hinv <- function(x) {(exp(rHL*x)-1)/rHL} H1d <- function(x) {1/(1+rHL*x)} H2d <- function(x) {-rHL/(1+rHL*x)^2} H3d <- function(x) {2*rHL*rHL/(1+rHL*x)^3} } Model <- 10*rHL Modelname <- Tr.f[INDEX1] } ###(Box-Cox transformation) if (INDEX1 == 2){ if (rHB == 0){ H <- function(x) {log(1+x)} Hinv <- function(x) {(exp(x)-1)} H1d <- function(x) {1/(1+x)} H2d <- function(x) {-1/(1+x)^2} H3d <- function(x) {2/(1+x)^3} } if (rHB > 0){ H <- function(x) {((1+x)^rHB-1)/rHB} Hinv <- function(x) {(rHB*x+1)^(1/rHB)-1} H1d <- function(x) {(1+x)^(rHB-1)} H2d <- function(x) {(rHB-1)*(1+x)^(rHB-2)} H3d <- function(x) {(rHB-1)*(rHB-2)*(1+x)^(rHB-3)} } Model <- 10*rHB Modelname <- Tr.f[INDEX1] } Modelname Model ################################################################### # # E-step func # ################################################################### GetWeight <- function(bbv, par_C, par_b){ beta= par_C[1:nB]; psi= par_C[nB+1]; f= par_C[(nB+2): nC] Z2B <- c(Z2%*%beta); F_Xi <- c(ind_F %*% f) F_Xi[which(CURE==1)] <- 1 exptp<- exp(Z2B + psi*bbv) tempC<- (1-CURE)*(DELTA*log(H1d(exptp*F_Xi))-H(exptp*F_Xi))-CURE*H(exptp) out<- exp(tempC); out; weights <- out*matrix(ww, N, Nnode, byrow=TRUE) wwN <- weights/ rowSums(weights); wwN ; } ################################################################### # # M-step func # ################################################################### ################################################################### # GetLambda ################################################################### GetLambda <- function(Eqe, tol){ cj <- c(ind_XjXi%*%Eqe) DELTA1 <- which(DELTA==1) lower <- max(-cj[DELTA1]) upper <- mc #M n.convL <- NULL for (iterL in 1:100){ mm <- (lower+upper)/2 Qm <- sum(1/(cj+mm)[DELTA1])- 1 if (Qm == 0){ upper <- mm lower <- mm } else if (Qm < 0){ upper <- mm } else { lower <- mm } err <- abs(upper-lower) n.convL <- rbind(n.convL, c(iterL, Qm, lower, upper, err)) if (err < tol){ break } } mm <- (lower+upper)/2; mm } ################################################################### # M_step ################################################################### M_step <- function(bbv, wwN, par_Y, par_C, par_b){ alpha= par_Y[1:nA]; sigma2_e= par_Y[nY]; beta= par_C[1:nB]; psi= par_C[nB+1]; f= par_C[(nB+2): nC] r = par_b; ZZ1A <- c(ZZ1%*%alpha); Z2B <- c(Z2%*%beta); YZZ1A <- YE[ ,3]- ZZ1A F_Xi <- c(ind_F %*% f) F_Xi[which(CURE==1)] <- 1 exptp <- exp(Z2B + psi*bbv) q=(1-CURE)*(-DELTA*H2d(exptp*F_Xi)/H1d(exptp*F_Xi)+H1d(exptp*F_Xi))+CURE*H1d(exptp) ################################################################# # Expectations ################################################################# Eb <- rowSums(bbv*wwN) Ebsq <- rowSums(bbv^2*wwN) Eqe <- rowSums(q*exptp*wwN) Eqeb <-rowSums(q*exptp*bbv*wwN) Eqebsq <-rowSums(q*exptp*bbv^2*wwN) ################################################################### # Update param set 1 - alpha, sigma2_e, f, r ################################################################### newalpha <- solve(t(ZZ1)%*%ZZ1)%*%t(ZZ1)%*%(YE[ ,3]- rep(Eb, ni)) newsigma2_e <- sum(YZZ1A^2-2*rep(Eb, ni)*YZZ1A+ rep(Ebsq, ni))/sum(ni) newr <- sum(Ebsq)/N ################################################################# # Update param set 2 - beta, psi # - 1. Estimate lambda using the full Newton-Raphson # - 2. Update beta, psi using One-step Newton-Raphson ################################################################# #### 1. Estimate lambda using the full Newton-Raphson #### uplambda <- GetLambda(Eqe, 0.0005) LsEqe <- uplambda + c(ind_XjXi%*%Eqe) sZEqe <- ind_XjXi%*%(Z2*Eqe)/LsEqe sEqeb <- c(ind_XjXi%*%Eqeb/LsEqe) ##### Score functions: ##### g1 = dElc/dbeta, g2 = dElc/dpsi ##### g3(uplambda) = dElc/dlambda = 1-sum(f) g1 <- colSums((1-CURE)*DELTA*(Z2-sZEqe) - CURE*Z2*Eqe) g2 <- sum((1-CURE)*DELTA*(Eb-sEqeb)- CURE*Eqeb) ##### derivative of Scores: ##### dg1/dbeta, dg1/dpsi, dg1/dlambda, etc... ZEqe <- Z2*Eqe/LsEqe # ZEqe for non-cure; dg1dB<-0 tempB<- array(0, c(nB,nB,N)) for (ii in 1:N){ tempB[,,ii]<-Z2[ii, ] %*% t(ZEqe[ii, ]) } idxD <- which(((1-CURE)*DELTA)==1) for (kk in idxD){ comp1 <- apply(tempB[,,which(ind_XjXi[kk, ]==1)], c(1,2), sum) dg1dB <- dg1dB - (comp1- sZEqe[kk, ]%*%t(sZEqe[kk, ])) } dg1dB <- dg1dB - t(Z2)%*%(CURE*Z2*Eqe) dg1dP <- colSums(-(1-CURE)*DELTA*(ind_XjXi%*%(Z2*Eqeb)/LsEqe - sZEqe*sEqeb)) dg1dP <- dg1dP - c(CURE%*%(Z2*Eqeb)) dg1dL <- colSums((1-CURE)*DELTA*(sZEqe/LsEqe)) dg2dB <- t(dg1dP) dg2dP <- sum(-(1-CURE)*DELTA*(ind_XjXi%*%Eqebsq/LsEqe-sEqeb^2)) dg2dP <- dg2dP - CURE%*%Eqebsq dg2dL <- sum((1-CURE)*DELTA*(sEqeb/LsEqe)) dg3dB <- t(dg1dL) dg3dP <- dg2dL dg3dL <- sum((1-CURE)*DELTA/LsEqe^2) dLdB <- - dg3dB/dg3dL ### Dg3DB = dg3dL*dLdB + dg3dB = 0 dLdP <- - dg3dP/dg3dL Dg1DB <- dg1dL%*%dLdB + dg1dB Dg1DP <- dg1dL*dLdP + dg1dP Dg2DB <- dg2dL*dLdB + dg2dB Dg2DP <- dg2dL*dLdP + dg2dP HH <- rbind( cbind(Dg1DB,Dg1DP), cbind(Dg2DB,Dg2DP)) ##### Update param - beta, psi param <- c(beta, psi) newparam <- param - solve(HH)%*%c(g1, g2) newbeta <- newparam[1:nB]; newpsi <- newparam[nB+1]; ################################################################### # Update param set 3 - f (as a function of (uplambda) ################################################################### newf <- (1-CURE)*DELTA/LsEqe newpar_Y <- c(newalpha, newsigma2_e) newpar_C <- c(newbeta, newpsi, newf) newpar_b <- newr c(newpar_Y, newpar_C, newpar_b, uplambda) } # end of "M_step" # ################################################################### # # covest # ################################################################### covest <- function(newpar_Y, newpar_C, newpar_b){ alpha= newpar_Y[1:nA]; sigma2_e= newpar_Y[nY]; beta= newpar_C[1:nB]; psi= newpar_C[nB+1]; f= newpar_C[(nB+2): nC] r = newpar_b; ################################################################### # E-step in "covest" ################################################################### bbv <- Tbb(newpar_Y, newpar_C, newpar_b) wwN <- GetWeight(bbv, newpar_C, newpar_b) ################################################################### # Update in "covest" ################################################################### ZZ1A <- c(ZZ1%*%alpha); Z2B <- c(Z2%*%beta); YZZ1A <- YE[ ,3]- ZZ1A F_Xi <- c(ind_F %*% f) F_Xi[which(CURE==1)] <- 1 exptp <- exp(Z2B + psi*bbv) expF <- exptp*F_Xi ################################################################### # Hfun ################################################################### H1=H1d(expF); H2=H2d(expF); H3=H3d(expF); gH2H1 = H2/H1*exptp gdH2H1= H3/H1*exptp*exptp-(H2/H1*exptp)^2 gH1 = H1*exptp gH2 = H2*exptp*exptp Eb <- rowSums(bbv*wwN) Ebsq <- rowSums(bbv^2*wwN) EH2H1 <- rowSums(gH2H1*wwN) EdH2H1 <- rowSums(gdH2H1*wwN) EH1 <- rowSums(gH1*wwN) EH2 <- rowSums(gH2*wwN) EH2H1b <- rowSums(gH2H1*bbv*wwN) EdH2H1b <- rowSums(gdH2H1*bbv*wwN) EH1b <- rowSums(gH1*bbv*wwN) EH2b <- rowSums(gH2*bbv*wwN) EH2H1bsq <- rowSums(gH2H1*bbv^2*wwN) EdH2H1bsq <- rowSums(gdH2H1*bbv^2*wwN) EH1bsq <- rowSums(gH1*bbv^2*wwN) EH2bsq <- rowSums(gH2*bbv^2*wwN) ED2LY <- matrix(0, nY, nY) ED2LC <- matrix(0, nCf, nCf) ED2Lb <- matrix(0, nb, nb) E_DLDL <- matrix(0, (nY+nCf+1), (nY+nCf+1)) EDLEDL <- matrix(0, (nY+nCf+1), (nY+nCf+1)) for (ii in 1:N){ IDXY <- which(YE[ ,1]==TE[ii,3]) D1A <- matrix(0, nA, Nnode) D1sigma2_e <- matrix(0, 1, Nnode) D2A <- matrix(0, nA, nA) D2sigma2_e <- 0 D2Asigma2_e <- matrix(0, nA, 1) for (k in IDXY){ ### Y D1A <- D1A + ZZ1[k, ]%*%t((YZZ1A[k]-bbv[ii, ])/sigma2_e) D1sigma2_e <- D1sigma2_e + t((YZZ1A[k]-bbv[ii, ])^2/(2*sigma2_e^2)-1/(2*sigma2_e)) D2A <- D2A -ZZ1[k, ]%*%t(ZZ1[k, ])/sigma2_e D2Asigma2_e <- D2Asigma2_e -(YZZ1A[k]-Eb[ii])/sigma2_e^2*ZZ1[k, ] D2sigma2_e<- D2sigma2_e + 1/2/sigma2_e^2-(YZZ1A[k]^2-2*YZZ1A[k]*Eb[ii]+Ebsq[ii])/sigma2_e^3 } if (CURE[ii]==1){ D1B <- -Z2[ii, ]%*%t(gH1[ii, ]) D1psi <- -t(gH1[ii, ]*bbv[ii, ]) D1f <- matrix(0,mc,Nnode) D2B <- -(EH1[ii]+EH2[ii])*Z2[ii, ]%*%t(Z2[ii, ]) D2Bpsi <- -matrix((EH1b[ii]+EH2b[ii])*Z2[ii, ]) D2Bf <- matrix(0, nB, mc) D2psi <- -(EH1bsq[ii]+EH2bsq[ii]) D2psif <- matrix(0, 1, mc) D2f <- matrix(0, mc, mc) } else{ PositiveJump <- TE[which(DELTA==1),2] RISK <- which(PositiveJump <= TE[ii,2]) indX <- ifelse(PositiveJump <= TE[ii,2], 1, 0) idxwj <- which(PositiveJump == TE[ii,2]) coef2 <- t(DELTA[ii]*gH2H1[ii, ]-gH1[ii, ]) coef4 <- DELTA[ii]*(EH2H1[ii]+EdH2H1[ii]*F_Xi[ii])-(EH1[ii]+EH2[ii]*F_Xi[ii]) coef5 <- DELTA[ii]*(EH2H1b[ii]+EdH2H1b[ii]*F_Xi[ii])-(EH1b[ii]+EH2b[ii]*F_Xi[ii]) D1B <- DELTA[ii]*Z2[ii, ] + Z2[ii, ]%*%(coef2*F_Xi[ii]) D1psi <- DELTA[ii]*t(bbv[ii, ]) + (coef2*F_Xi[ii])*t(bbv[ii, ]) D1f <- matrix(0,mc,Nnode) D1f[RISK, ] <- coef2 if (DELTA[ii]==1) D1f[idxwj, ] <- t(D1f[idxwj, ]) + 1/f[ii] D2B <- coef4*F_Xi[ii]*Z2[ii, ]%*%t(Z2[ii, ]) D2Bpsi <- matrix(coef5*F_Xi[ii]*Z2[ii, ]) D2Bf <- matrix(0, nB, mc) D2Bf[ ,RISK] <- coef4*Z2[ii, ] D2psi <- -(EH1bsq[ii]+EH2bsq[ii]*F_Xi[ii])*F_Xi[ii] if (DELTA[ii]==1) D2psi <- D2psi+(EH2H1bsq[ii]+EdH2H1bsq[ii]*F_Xi[ii])*F_Xi[ii] D2psif <- matrix(0, 1, mc) D2psif[ ,RISK] <- coef5 D2f <- indX%*%t(indX)*(DELTA[ii]*EdH2H1[ii]-EH2[ii]) if (DELTA[ii]==1) D2f[idxwj, idxwj] <- D2f[idxwj, idxwj] - 1/f[ii]^2 } #random effect D1r <- -1/2/r + t(bbv[ii, ]^2/r^2)/2 D2r <- 1/2/r^2 - Ebsq[ii]/r^3 ED2LY <- ED2LY + rbind(cbind(D2A, D2Asigma2_e), cbind(t(D2Asigma2_e), D2sigma2_e)) ED2LC <- ED2LC + rbind(cbind(D2B, D2Bpsi, D2Bf), cbind(t(D2Bpsi), D2psi, D2psif), cbind(t(D2Bf), t(D2psif), D2f)) ED2Lb <- ED2Lb + D2r DL <- rbind(D1A,D1sigma2_e, D1B,D1psi,D1f, D1r) EDL <- t(DL)*wwN[ii,] E_DLDL <- E_DLDL + DL%*%(EDL) EDL <- colSums(EDL) EDLEDL <- EDLEDL + EDL%*%t(EDL) } ED2L <- rbind(cbind(ED2LY, matrix(0,nY,nCf), matrix(0,nY,nb)), cbind(matrix(0,nCf,nY), ED2LC, matrix(0,nCf,nb)), cbind(matrix(0,nb,nY), matrix(0,nb,nCf), ED2Lb)) HS <- -ED2L - (E_DLDL-EDLEDL) J <- rbind(cbind(diag(nY+nCf-1), c(rep(0, nY+nB+1), rep(-1, mc-1)), rep(0, nY+nCf-1)), c(rep(0,nY+nCf), 1)) HS <- J %*% HS %*% t(J) out.eigen<-eigen(HS) values<-out.eigen$values temp1<-which(values < 0.001) errCOV=0; if (length(temp1) > 0){ values[-temp1]<-1/values[-temp1] values[temp1]<-0 errCOV=1; } else { values <- 1/values } COV <- (out.eigen$vectors) %*% diag(values) %*% t(out.eigen$vectors); outCOV <- list() outCOV[[1]]= COV ; outCOV[[2]]= errCOV ; outCOV; } ################################################################### # # LogLik # ################################################################### GetLogLik <- function(newpar_Y, newpar_C, newpar_b, ZZ1, Z2){ alpha= newpar_Y[1:nA]; sigma2_e= newpar_Y[nY]; beta= newpar_C[1:nB]; psi= newpar_C[nB+1]; f= newpar_C[(nB+2): nC] r = newpar_b; ################################################################### # Update in "covest" ################################################################### ZZ1A <- c(ZZ1%*%alpha); Z2B <- c(Z2%*%beta); YZZ1A <- YE[ ,3]- ZZ1A F_Xi <- c(ind_F %*% f) F_Xi[which(CURE==1)] <- 1 ################################################################### # E-step in "GetLogLik" ################################################################### bbv <- Tbb(newpar_Y, newpar_C, newpar_b) bbvY <- bbv[rep(1:N, ni), ] wwN <- GetWeight(bbv, newpar_C, newpar_b) f_Oi <- matrix(0, N, Nnode) for (k in 1: ncol(bbv)){ Ycmp <- indIDY%*%(-log(2*pi*sigma2_e)/2 -(YZZ1A-bbvY[ ,k])^2/(2*sigma2_e)) logf <- rep(0, length(TE[ ,1])) logf[which(DELTA==1)] <- log(f[which(DELTA==1)]) inexp <- Z2B + psi*bbv[ ,k] Ttemp <- logf + TE[ ,1]*(inexp+log(H1d(exp(inexp)*F_Xi))) Tcmp <- Ttemp - H(exp(inexp)*F_Xi) f_Oi[ ,k] <- exp(Ycmp+Tcmp) } loglik <- sum(log(rowSums(f_Oi*wwN))); loglik; }