library(glmnet)  # Package to fit ridge/lasso/elastic net models
## Loading required package: Matrix
## Loaded glmnet 4.1-8
# Function to generate data and perform model fitting and evaluation
run_model_comparisons <- function(n, p, real_p, seed=42) {
  set.seed(seed)  # Set seed for reproducibility

  # Generate the data
  x <- matrix(rnorm(n * p), nrow = n, ncol = p)
  y <- apply(x[, 1:real_p], 1, sum) + rnorm(n)

  # Split data into training and testing datasets
  train_rows <- sample(1:n, 0.66 * n)
  x.train <- x[train_rows, ]
  x.test <- x[-train_rows, ]

  y.train <- y[train_rows]
  y.test <- y[-train_rows]

  # List to store fit results
  list.of.fits <- list()

  # Fit models with different alphas and store the results
  for (i in 0:10) {
    fit.name <- paste0("alpha", i / 10)
    list.of.fits[[fit.name]] <- cv.glmnet(x.train, y.train, type.measure = "mse", alpha = i / 10, family = "gaussian")
  }

  # Data frame to store MSE results
  results <- data.frame()

  # Evaluate each model on the test set and calculate MSE
  for (i in 0:10) {
    fit.name <- paste0("alpha", i / 10)
    predicted <- predict(list.of.fits[[fit.name]], s = list.of.fits[[fit.name]]$lambda.1se, newx = x.test)
    mse <- mean((y.test - predicted) ^ 2)
    temp <- data.frame(alpha = i / 10, mse = mse, fit.name = fit.name)
    results <- rbind(results, temp)
  }

  return(results)
}

# Run the model comparison for the first scenario
results_scenario1 <- run_model_comparisons(n = 1000, p = 5000, real_p = 15)
print(results_scenario1)
##    alpha       mse fit.name
## 1    0.0 14.884588   alpha0
## 2    0.1  2.202451 alpha0.1
## 3    0.2  1.486889 alpha0.2
## 4    0.3  1.330456 alpha0.3
## 5    0.4  1.303892 alpha0.4
## 6    0.5  1.214926 alpha0.5
## 7    0.6  1.236198 alpha0.6
## 8    0.7  1.187826 alpha0.7
## 9    0.8  1.195830 alpha0.8
## 10   0.9  1.211868 alpha0.9
## 11   1.0  1.172819   alpha1
# Run the model comparison for the second scenario
results_scenario2 <- run_model_comparisons(n = 1000, p = 5000, real_p = 1500)
print(results_scenario2)
##    alpha      mse fit.name
## 1    0.0 1400.375   alpha0
## 2    0.1 1545.035 alpha0.1
## 3    0.2 1545.035 alpha0.2
## 4    0.3 1545.035 alpha0.3
## 5    0.4 1545.035 alpha0.4
## 6    0.5 1545.035 alpha0.5
## 7    0.6 1545.035 alpha0.6
## 8    0.7 1545.035 alpha0.7
## 9    0.8 1545.035 alpha0.8
## 10   0.9 1545.035 alpha0.9
## 11   1.0 1545.035   alpha1