R/cvGlmnet2.R

Defines functions cv.glmnet2

Documented in cv.glmnet2

cv.glmnet2 <- function(xx, yy, foldid, alpha=seq(0,10,by=2)/10, lambda=exp(seq(log(10^-8), log(5), length.out=100)),...)
{
  fits <- vector("list",length(alpha))
  names(fits) <- alpha

  for(ii in 1:length(alpha))
  {
    fits[[ii]] <- cv.glmnet(x=xx, y=yy, foldid=foldid, alpha=alpha[ii], lambda=lambda, ...)
  }

  if(length(alpha)==1)
  {
    idx <- 1
  }else
  {
    idx <- which.min(sapply(fits, function(xx) xx$cvm[which(xx$lambda==xx$lambda.1se)]))
  }

  fits[[idx]]$alpha <- alpha[idx]
  return(fits[[idx]])
}

Try the MTPS package in your browser

Any scripts or data that you put into this service are public.

MTPS documentation built on July 9, 2023, 7:46 p.m.