library(lars) library(elasticnet) library(survival) enet.mod <- function (x, y, lambda, max.steps, normalize = TRUE, intercept = TRUE, trace = FALSE, eps = .Machine$double.eps) { ### Adapted from software packages provided by Hui Zou, Trevor Hastie, Brad Efron call <- match.call() nm <- dim(x) n <- nm[1] m <- nm[2] im <- seq(m) one <- rep(1, n) vn <- dimnames(x)[[2]] meanx <- drop(one %*% x)/n if (intercept == FALSE) { meanx <- rep(0, m) } x <- scale(x, meanx, FALSE) normx <- sqrt(drop(one %*% (x^2))) if (normalize == FALSE) { normx <- rep(1, m) } if (any(normx < eps * sqrt(n))) stop("Some of the columns of x have zero variance") names(normx) <- NULL x <- scale(x, FALSE, normx) mu <- mean(y) if (intercept == FALSE) { mu <- 0 } y <- drop(y - mu) d1 <- sqrt(lambda) d2 <- 1/sqrt(1 + lambda) Cvec <- drop(t(y) %*% x) * d2 ssy <- sum(y^2) residuals <- c(y, rep(0, m)) if (lambda > 0) { maxvars <- m } if (lambda == 0) { maxvars <- min(m, n - 1) } if (missing(max.steps)) { max.steps <- 50 * maxvars } L1norm <- 0 penalty <- max(abs(Cvec)) beta <- rep(0, m) betactive <- list(NULL) first.in <- integer(m) active <- NULL Actset <- list(NULL) df <- 0 if (lambda != 0) { Cp <- ssy } ignores <- NULL actions <- as.list(seq(max.steps)) drops <- FALSE Sign <- NULL R <- NULL k <- 0 beta.full <- array(0,dim=c(max.steps,m)) while ((k < max.steps) & (length(active) < maxvars)) { action <- NULL k <- k + 1 if (k == 1) inactive <- im if (k != 1) inactive <- im[-c(active, ignores)] C <- Cvec[inactive] Cmax <- max(abs(C)) if (any(drops)==F) { new <- abs(C) == Cmax C <- C[new==F] new <- inactive[new] for (inew in new) { R <- updateRR(x[, inew], R, x[, active], lambda) if (attr(R, "rank") == length(active)) { nR <- seq(length(active)) R <- R[nR, nR, drop = FALSE] attr(R, "rank") <- length(active) ignores <- c(ignores, inew) action <- c(action, -inew) if (trace) cat("LARS-EN Step", k, ":\t Variable", inew, "\tcollinear; dropped for good\n") } if (attr(R, "rank") != length(active)) { if (first.in[inew] == 0) first.in[inew] <- k active <- c(active, inew) Sign <- c(Sign, sign(Cvec[inew])) action <- c(action, inew) if (trace) cat("LARS-EN Step", k, ":\t Variable", inew, "\tadded\n") } } } if (any(drops)==T) action <- -dropid Gi1 <- backsolve(R, backsolvet(R, Sign)) A <- 1/sqrt(sum(Gi1 * Sign)) w <- A * Gi1 u1 <- drop(x[, active, drop = FALSE] %*% w * d2) u2 <- rep(0, m) u2[active] <- d1 * d2 * w u <- c(u1, u2) if (lambda > 0) { maxvars <- m - length(ignores) } if (lambda == 0) { maxvars <- min(m - length(ignores), n - 1) } if (length(active) >= maxvars) { gamhat <- Cmax/A } if (length(active) < maxvars) { a <- (drop(u1 %*% x[, -c(active, ignores)]) + d1 * u2[-c(active, ignores)]) * d2 gam <- c((Cmax - C)/(A - a), (Cmax + C)/(A + a)) gamhat <- min(gam[gam > eps], Cmax/A) Cdrop <- c(C - gamhat * a, -C + gamhat * a) - (Cmax - gamhat * A) } dropid <- NULL b1 <- beta[active] z1 <- -b1/w zmin <- min(z1[z1 > eps], gamhat) if (zmin < gamhat) { gamhat <- zmin drops <- z1 == zmin } if (zmin >= gamhat) drops <- FALSE beta[active] <- beta[active] + gamhat * w betactive[[k]] <- beta[active] Actset[[k]] <- active residuals <- residuals - (gamhat * u) Cvec <- (drop(t(residuals[1:n]) %*% x) + d1 * residuals[-(1:n)]) * d2 L1norm <- c(L1norm, sum(abs(beta[active]))/d2) penalty <- c(penalty, penalty[k] - abs(gamhat * A)) if (any(drops)) { dropid <- seq(drops)[drops] for (id in rev(dropid)) { if (trace) cat("LARS-EN Step", k, ":\t Variable", active[id], "\tdropped\n") R <- downdateR(R, id) } dropid <- active[drops] beta[dropid] <- 0 active <- active[drops==F] Sign <- Sign[drops==F] } if (is.null(vn)==F) names(action) <- vn[abs(action)] actions[[k]] <- action beta.full[k,] <- beta } beta.0 <- 0 for (jjj in 1:nrow(beta.full)) { if (sum(beta.full[jjj,] == 0) == ncol(beta.full)) { beta.0 <- beta.0 + 1 } } beta.red <- rbind(rep(0,m),beta.full[1:(nrow(beta.full)-beta.0),]) allset <- Actset[[1]] for (i in 2:k) { allset <- union(allset, Actset[[i]]) } allset <- sort(allset) max.p <- length(allset) beta.pure <- matrix(0, k + 1, max.p) for (i in 2:(k + 1)) { for (j in 1:length(Actset[[i - 1]])) { l <- c(1:max.p)[allset == Actset[[i - 1]][j]] beta.pure[i, l] <- betactive[[i - 1]][j] } } beta.red <- beta.red/d2 beta.pure <- beta.pure/d2 dimnames(beta.red) <- list(paste(0:k), vn) dimnames(beta.pure) <- list(paste(0:k), vn[allset]) k <- dim(beta.pure)[1] df <- 1:k for (i in 1:k) { a <- drop(beta.pure[i, ]) df[i] <- 1 + length(a[a != 0]) } residuals <- y - x[, allset, drop = FALSE] %*% t(beta.pure) beta.red <- scale(beta.red, FALSE, normx) beta.pure <- scale(beta.pure, FALSE, normx[allset]) RSS <- apply(residuals^2, 2, sum) R2 <- 1 - RSS/RSS[1] Cp <- ((n - m - 1) * RSS)/rev(RSS)[1] - n + 2 * df object <- list(call = call, actions = actions[seq(k)], allset = allset, beta.pure = beta.red, vn = vn, mu = mu, normx = normx, meanx = meanx, lambda = lambda, L1norm = L1norm, penalty = penalty * 2/d2, df = df, Cp = Cp, sigma2 = rev(RSS)[1]/(n - m - 1)) class(object) <- "enet" object } if.R <- function(r, s) { if(exists("is.R") && is.R()) r else s } solvet <- function (a, b, tol = 1e-09) { if (!is.list(a)) a <- qr(a, tol = tol) if (a$rank < ncol(a$qr)) stop("apparently singular matrix") if (missing(b)) { b <- a$qr db <- dim(b) if (diff(db)) stop("matrix inverse only for square matrices") b[] <- rep(c(1, rep(0, db[1])), length = prod(db)) } qr.coef(a, b) } control <- NULL library(survival) aft.m <- function(x, y, control = NULL, lambda, max.steps=max(ss), normalize=F, intercept=F, trace=F, ss) { if(ncol(y) != 2) stop("y is not a right-censored Surv object") status <- y[, 2] yy <- y[, 1] yy <- ifelse(yy==0,.0001,yy) yy <- log(yy) iter.max <- control$iter.max eps <- control$eps trace <- control$trace tol <- control$tol max.cycle <- control$max.cycle if(length(iter.max) == 0) iter.max <- 20 if(length(eps) == 0) eps <- 0.01 if(length(trace) == 0) trace <- TRUE if(length(tol) == 0) tol <- 1e-007 if(length(max.cycle) == 0) max.cycle <- 30 x <- as.matrix(x) if(all(x[, 1] == 1)) x <- x[, -1, drop = FALSE] d <- dim(x) nvar <- d[2] if(length(nvar) == 0) nvar <- 0 N <- length(yy) if(nvar > 0) { xbar <- apply(x, 2, mean) xm <- x - rep(xbar, rep(N, nvar)) } else { xm <- 0 } timeorig <- yy order.orig <- 1:N dummystrat <- factor(rep(1, N)) betahat <- rep(0, max(nvar, 1)) betamatrix <- NULL sse <- 0 n <- 0 nonconv <- FALSE oldbeta <- betahat oldsse <- sse one <- rep(1, N) normx <- sqrt(drop(one %*% (x^2))) normalize = T if (normalize==F) { normx <- rep(1, nvar) } ttt <- ss x <- scale(x, FALSE, normx) state <- status state[yy == max(yy)] <- 1 m2 <- order(yy) yy.order <- yy[m2] surv.obj <- Surv(yy,state) KM.hat <- KM.ehat <- survfit(surv.obj,type="kaplan-meier",conf.type = "none", se.fit = FALSE) n.risk <- KM.ehat$n.risk surv <- KM.ehat$surv repeats <- c(diff( - n.risk), n.risk[length(n.risk)]) surv <- rep(surv, repeats) w <- - diff(c(1, surv)) m2 <- order(y[,1]) log.y.order <- yy[m2] bla <- NA for(iiii in 1:length(log.y.order)) { bla[iiii] <- sum((log.y.order*w)[-(1:iiii)]) } bla2 <- bla/surv bl <- bla2 bl[(1:N)[m2]] <- bla2 yhat <- bl yy.new <- yy yy.new[state == 0] <- yhat[state == 0] yy.old <- yy yy <- yy.new n <- n - 1 fit.aften <- enet.mod(xm, yy, lambda=lambda, normalize=normalize, intercept=intercept, trace=trace, max.steps=max.steps) betahat <- fit.aften$beta[ttt,] ypred <- x %*% betahat alphahat <- mean(yy - ypred) sse <- sum((yy - ypred)^2) razlika <- oldsse/sse if(trace) cat("iteration = ", n, " sse ratio = ", format(razlika), "\n") n <- n + 1 if(trace) cat(" alpha = ", format(alphahat), " beta = ", format(betahat), "\n\n") ## ehat <- timeorig - ypred f <- list(fail = FALSE, iter = n) cof <- if(nvar == 0) alphahat else c(alphahat, betahat) dx <- dimnames(x)[[2]] if(length(dx) == 0 && nvar > 0) dx <- paste("x", 1:nvar, sep = "") names(cof) <- c("Intercept", dx) f$coefficients <- cof ehat.u <- ehat[status == 1] edf <- sum(status) - nvar - 1 sigma <- sqrt(sum((ehat.u - mean(ehat.u))^2)/edf) stats <- c(N, sum(status), nvar, edf, sigma) names(stats) <- c("Obs", "Events", "d.f.", "error d.f.", "sigma") f$stats <- stats if(any(status == 0)) yy <- structure(yy, class = "impute", imputed = (1:N)[status == 0]) f$y.imputed <- yy f$km.steps <- w f }