Skip to content

Instantly share code, notes, and snippets.

@grantbrown
Created January 15, 2015 20:22
Show Gist options
  • Save grantbrown/a8cbd3c77a3d6ee9e2c1 to your computer and use it in GitHub Desktop.
Save grantbrown/a8cbd3c77a3d6ee9e2c1 to your computer and use it in GitHub Desktop.
pa.cv.ncvreg timings
library(ncvreg)
generateData <- function(n,p,nNonzeroBeta, binomial=FALSE){
trueBetaIndex <- (1:p)[order(runif(p,0,1))][1:nNonzeroBeta]
X <- matrix(rnorm(n*p), nrow=n,ncol=p)
trueBeta <- rep(0,p)
trueBeta[trueBetaIndex] <- rnorm(nNonzeroBeta)
if (binomial){
eEta <- exp(X %*% trueBeta)
Y <- rbinom(rep(1, n), 1, eEta/(1+eEta))
}
else{
Y <- rnorm(rep(1, n),X %*% trueBeta, 1)
}
list(Y = Y,
X = X,
trueBeta = trueBeta,
trueBetaIndex = trueBetaIndex)
}
compareTimes <- function(n,p,nNonzeroBeta,binomial=FALSE, seed=123123){
dat <- generateData(n,p,nNonzeroBeta,binomial)
cl <- makeCluster(8)
startTime <- Sys.time()
result <- cv.ncvreg(dat$X, dat$Y,
family=ifelse(binomial, "binomial", "gaussian"),
penalty="MCP", nfolds = 10, seed=seed)
serialEndTime <- as.numeric(Sys.time() - startTime)
startTime <- Sys.time()
result <- pa.cv.ncvreg(cl, dat$X, dat$Y,
family=ifelse(binomial, "binomial", "gaussian"),
penalty="MCP", nfolds = 10, seed=seed)
parallelEndTime <- as.numeric(Sys.time() - startTime )
stopCluster(cl)
out <- matrix(c(n,p,nNonzeroBeta,binomial,serialEndTime, parallelEndTime), nrow = 1)
colnames(out) <- c("n", "p", "Nonzero Beta", "Is Binomial?", "Serial Time", "Parallel Time")
out
}
rbind(compareTimes(1000, 100, 10, seed=123123),
compareTimes(1000, 100, 10, binomial = TRUE, seed=123123),
compareTimes(10000, 100, 10, seed=123123),
compareTimes(10000, 100, 10, binomial = TRUE, seed=123123)
)
# n p Nonzero Beta Is Binomial? Serial Time Parallel Time
# [1,] 1000 100 10 0 2.108429 1.108614
# [2,] 1000 100 10 1 3.470646 1.533820
# [3,] 10000 100 10 0 10.027343 5.848476
# [4,] 10000 100 10 1 39.150664 17.808448
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment