Skip to content

Instantly share code, notes, and snippets.

@arraytools
Created August 27, 2020 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arraytools/f79480f6523e33d77e6d0e9e41c278c9 to your computer and use it in GitHub Desktop.
Save arraytools/f79480f6523e33d77e6d0e9e41c278c9 to your computer and use it in GitHub Desktop.
test glmnet with random data
library(glmnet)
# Binary data
n = 1000
p = 100
nzc = trunc(p/10)
for(i in 1:100) {
cat(i, " ")
if (i %% 10 == 0) cat("\n")
x = matrix(rnorm(n * p), n, p)
ly = rbinom(n = n, prob = .5, size = 1)
cvfit = cv.glmnet(x, ly, family = "binomial")
# plot(cvfit)
if (sum(coef(cvfit, s = "lambda.min") != 0) ==0)
cat("Got you, ", i, "\n") # note that se is very small around max(lambda)
}
# Survival data
data(CoxExample) # load x, y
nr <- 30; nc <- 100
y1 <- y[1:nr, ]
for(i in 1:100) {
x1 <- matrix(rnorm(nr*nc), nr=nr)
cvfit = cv.glmnet(x1, y1, family = "cox")
# plot(cvfit)
if (sum(coef(cvfit, s = "lambda.min") != 0) ==0)
cat("Got you, ", i, "\n")
}
# Continuous response data
nr=20; nc=100
for(i in 1:100) {
cat(i, " ")
if (i %% 10 == 0) cat("\n")
y <- as.matrix(rnorm(nr))
X <- matrix(rnorm(nr*nc), nr=nr); colnames(X) <- paste0("x", 1:ncol(X))
cvfit <- cv.glmnet(X, y)
# plot(cvfit)
if (sum(coef(cvfit, s = "lambda.min") != 0) ==0)
cat("Got you, ", i, "\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment