fit_lasso_regression.Rd
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
)
A data frame containing the predictor and response variables
A character vector of column names corresponding to the predictor variables
A character string of the column name corresponding to the response variable
The regularization parameter
The mixing parameter between L1 and L2 penalties (default = 1)
The maximum number of iterations for the coordinate descent algorithm (default = 1000)
The convergence threshold for the coordinate descent algorithm (default = 1e-4)
A list containing the fitted coefficients and the intercept
# 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