# Standardize data
dat <- scale(mtcars)
x <- as.matrix(dat[, -1])
y <- as.vector(dat[, 1])
n <- nrow(x)
p <- ncol(x)
# Specify estimating function
ufunc <- function(b) {
1/n * crossprod(x, (x %*% b - y) )
}
# Set hyperparameters
tau <- 0.6
alpha <- 0.5
# Set regularization coefficient
lambda1 <- 0
free_R1 <- free_lasso(p = p,
lambda = lambda1,
est_func = ufunc,
par_init = rep(0, p),
alpha = alpha,
tau = tau,
maxit = 10000L,
tol_ee = 1e-20,
tol_par = 1e-10,
verbose = FALSE)
free_R1$coefficients
# Compare with lm() - very close
lm(y~x-1)$coefficients
# Set regularization coefficient
lambda2 <- 0.7
free_R2 <- free_lasso(p = p,
lambda = lambda2,
est_func = ufunc,
par_init = rep(0, p),
alpha = alpha,
tau = tau,
maxit = 10000L,
tol_ee = 1e-20,
tol_par = 1e-10,
verbose = FALSE)
free_R2$coefficients
Run the code above in your browser using DataLab