#' Soft-thresholding function
#'
#' @param a scalar
#' @param lambda non-negative thresholding parameter
#'
#' @return soft-thresholded a
#' @export
#'
#' @examples
#' soft(2, 1)
#' soft(-2, 1)
#' soft(2, 3)
soft <- function(a, lambda){
return(sign(a) * max(abs(a) - lambda, 0))
}
#' Hard-thresholding function
#'
#' @inheritParams soft
#'
#' @return Hard-thresholded a
#' @export
#'
#' @examples
#' hard(2, 1)
#' hard(-2, 1)
#' hard(2, 3)
hard <- function(a, lambda){
if (abs(a) > lambda){
return(a)
}else{
return(0)
}
}
#' Thresholding function
#'
#' @inheritParams soft
#' @param option One of "soft" or "hard"
#'
#' @return Thresholded a
#' @export
#'
#' @examples
#' threshold(3, 1)
threshold <- function(a, lambda, option = c("soft", "hard")){
option = match.arg(option)
if (option == "soft"){
soft(a, lambda)
}else{
hard(a, lambda)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.