This function fits a Lasso regression to a dataset using coordinate descent.

fit_lasso_regression(
  data,
  x_vars,
  y_var,
  lambda,
  alpha = 1,
  max_iter = 1000,
  tol = 1e-04
)

Arguments

data

A data frame containing the predictor and response variables

x_vars

A character vector of column names corresponding to the predictor variables

y_var

A character string of the column name corresponding to the response variable

lambda

The regularization parameter

alpha

The mixing parameter between L1 and L2 penalties (default = 1)

max_iter

The maximum number of iterations for the coordinate descent algorithm (default = 1000)

tol

The convergence threshold for the coordinate descent algorithm (default = 1e-4)

Value

A list containing the fitted coefficients and the intercept

Examples

# Generate sample data
set.seed(123)
n <- 100
p <- 5
data <- data.frame(matrix(rnorm(n * p), n, p))
colnames(data) <- paste0("x", 1:p)
data$y <- data$x1 + data$x2 + rnorm(n)

# Fit the Lasso regression using the function
fit <- fit_lasso_regression(data, x_vars=c("x1", "x2", "x3", "x4", "x5"), y_var="y", lambda=0.1)

# Print the coefficients
print(fit$coefficients)
#> [1]  0.90480444  1.10474602  0.01317501 -0.08617756  0.20907601