# Generate a sparse covariance matrix
values <- c(160, 50)
n <- values[1]
p <- values[2]
s <- generate_sparse_cov_matrix(n, p, standardize = TRUE, sparse_rho = 0, scale_power = 0)
# Define a sequence of lambda values for the grid search
lambda_find <- c(0.1, 0.2, 0.3, 0.4)
# Perform a grid search to find the lambda value
# that results in a precision matrix with approximately 80% sparsity
lambda_results <- find_lambda_by_sparsity(s, lambda_find, desired_sparsity = 0.8)
# Inspect the optimal lambda value
optimal_lambda <- lambda_results$best_lambda
# Inspect the sparsity levels for each lambda tested
sparsity_levels <- lambda_results$actual_sparsity
Run the code above in your browser using DataLab