#install.packages("glmnet")
library(glmnet)
## Warning: package 'glmnet' was built under R version 4.2.3
## Loading required package: Matrix
## Loaded glmnet 4.1-8

1. Generate some example data

set.seed(123)
n <- 100  # Number of samples
p <- 20   # Number of predictors
X <- matrix(rnorm(n * p), nrow = n, ncol = p)  # Predictors
beta_true <- c(0.5, -0.3, rep(0, p - 2))      # True coefficients (only first two are non-zero)
y <- rbinom(n, 1, plogis(X %*% beta_true))    # Response variable

2. Fit Lasso logistic regression model

fit <- glmnet(X, y, family = "binomial", alpha = 1)

# Set the plot size
options(repr.plot.width = 8, repr.plot.height = 6) # Adjust the width and height as needed

# Plot the coefficient paths
plot(fit, xvar = "lambda", label = TRUE)

3. Select the lambda with minimum cross-validated error

cv_fit <- cv.glmnet(X, y, family = "binomial", alpha = 1)
lambda_min <- cv_fit$lambda.min
cat("Selected lambda:", lambda_min, "\n")
## Selected lambda: 0.05452508

4. Refit the model with the selected lambda

lasso_model <- glmnet(X, y, family = "binomial", alpha = 1, lambda = lambda_min)

5. Get non-zero coefficients

non_zero_indices <- which(coef(lasso_model) != 0)
non_zero_coef <- coef(lasso_model)[non_zero_indices]

# Print non-zero coefficients
cat("Non-zero coefficients:")
## Non-zero coefficients:
print(non_zero_coef)
## [1]  0.08886256 -0.23371157  0.09976463  0.12710446  0.02065955 -0.15416749
## [7]  0.05213538 -0.23465997