简体   繁体   中英

Having trouble understanding this syntax for R

I have been given following code for R, but I am having trouble understanding what it is doing. In fact I can not even run it in R because of its syntax. I assume the syntax is for lower level code behind R. If someone could help explain what's happening here and translate this into executable R code that would be very helpful.

soft_thresholding = function(x,a){
  result  a)]  a)] - a
  result[which(x < -a)] = x[which(x < -a)] + a
  return(result)}

Here is a summary of the findings. This is not a definite answer but could help the questioner.

  1. If one uses wordpress, then x <- a will look like x < -a. Check this URL that confirms this assumption
  2. Upon further online search with the function name in the question "soft_thresholding", shows that this function is probably attempting to do soft thresholding defined here .
  3. Some more online searching about soft thresholding lands on a CRAN package that is present here .
  4. Further deepdive into the r folder in the package binaries shows the following.
soft.threshold <- function(x,sumabs=1) 
return(soft(x, BinarySearch(x,sumabs)))
  1. The function above seems very close to the code in the question.
  2. Furthermore, the soft.threshold function uses another internal function BinarySearch that looks like this.
BinarySearch <-
function(argu,sumabs){
  if(norm2(argu)==0 || sum(abs(argu/norm2(argu)))<=sumabs) return(0)
  lam_max = max(abs(argu))
  lam1      <- 0
  lam2      <- lam_max
  iter <- 1
  while(iter < 500){
    su <- soft(argu,(lam1+lam2)/2)
    if(sum(abs(su/norm2(su)))<sumabs){
      lam2 <- (lam1+lam2)/2
    } else {
      lam1 <- (lam1+lam2)/2
    }
    if((lam2-lam1)/lam1 < 1e-10){
      if (lam2 != lam_max){
        return(lam2)
      }else{
        return(lam1)
      }
    }
    iter <- iter+1
  }
  warning("Didn't quite converge")
  return((lam1+lam2)/2)
}
  1. This recursive research leads one to believe that the function is perhaps attempting to mimic the function soft.threshold in the CRAN package "RGCCA"

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM